|
15 | 15 | */
|
16 | 16 | package org.asynchttpclient;
|
17 | 17 |
|
18 |
| -import static org.asynchttpclient.Dsl.*; |
19 |
| -import static org.asynchttpclient.test.EventCollectingHandler.*; |
| 18 | +import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE; |
| 19 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 20 | +import static org.asynchttpclient.Dsl.config; |
20 | 21 | import static org.asynchttpclient.test.TestUtils.*;
|
21 | 22 | import static org.testng.Assert.*;
|
22 | 23 | import io.netty.handler.codec.http.HttpRequest;
|
23 | 24 | import io.netty.handler.codec.http.HttpResponse;
|
24 | 25 |
|
25 |
| -import java.net.ConnectException; |
26 | 26 | import java.util.Arrays;
|
27 | 27 | import java.util.concurrent.ExecutionException;
|
28 |
| -import java.util.concurrent.TimeUnit; |
29 | 28 | import java.util.concurrent.atomic.AtomicBoolean;
|
30 | 29 |
|
31 | 30 | import javax.net.ssl.SSLHandshakeException;
|
32 | 31 | import javax.servlet.http.HttpServletResponse;
|
33 | 32 |
|
34 | 33 | import org.asynchttpclient.channel.KeepAliveStrategy;
|
35 | 34 | import org.asynchttpclient.test.EventCollectingHandler;
|
| 35 | +import org.asynchttpclient.testserver.HttpServer; |
| 36 | +import org.asynchttpclient.testserver.HttpTest; |
| 37 | +import org.testng.annotations.AfterClass; |
| 38 | +import org.testng.annotations.BeforeClass; |
36 | 39 | import org.testng.annotations.Test;
|
37 | 40 |
|
38 |
| -public class BasicHttpsTest extends AbstractBasicHttpsTest { |
| 41 | +public class BasicHttpsTest extends HttpTest { |
39 | 42 |
|
40 |
| - protected String getTargetUrl() { |
41 |
| - return String.format("https://localhost:%d/foo/test", port1); |
| 43 | + private static HttpServer server; |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void start() throws Throwable { |
| 47 | + server = new HttpServer(); |
| 48 | + server.start(); |
42 | 49 | }
|
43 | 50 |
|
44 |
| - @Test(groups = "standalone") |
45 |
| - public void zeroCopyPostTest() throws Exception { |
46 |
| - |
47 |
| - try (AsyncHttpClient client = asyncHttpClient(config().setSslEngineFactory(createSslEngineFactory(new AtomicBoolean(true))))) { |
48 |
| - Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(); |
49 |
| - assertNotNull(resp); |
50 |
| - assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); |
51 |
| - assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); |
52 |
| - } |
| 51 | + @AfterClass |
| 52 | + public static void stop() throws Throwable { |
| 53 | + server.close(); |
53 | 54 | }
|
54 | 55 |
|
55 |
| - @Test(groups = "standalone") |
56 |
| - public void multipleSSLRequestsTest() throws Exception { |
57 |
| - try (AsyncHttpClient c = asyncHttpClient(config().setSslEngineFactory(createSslEngineFactory(new AtomicBoolean(true))))) { |
58 |
| - String body = "hello there"; |
| 56 | + private static String getTargetUrl() { |
| 57 | + return server.getHttpsUrl() + "/foo/bar"; |
| 58 | + } |
59 | 59 |
|
60 |
| - // once |
61 |
| - Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); |
| 60 | + @Test |
| 61 | + public void postBodyOverHttps() throws Throwable { |
| 62 | + withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client -> { |
| 63 | + withServer(server).run(server -> { |
| 64 | + server.enqueueEcho(); |
| 65 | + |
| 66 | + Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader(CONTENT_TYPE, "text/html").execute().get(); |
| 67 | + assertNotNull(resp); |
| 68 | + assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); |
| 69 | + assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void multipleSequentialPostRequestsOverHttps() throws Throwable { |
| 76 | + withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client -> { |
| 77 | + withServer(server).run(server -> { |
| 78 | + server.enqueueEcho(); |
| 79 | + server.enqueueEcho(); |
62 | 80 |
|
63 |
| - assertEquals(response.getResponseBody(), body); |
| 81 | + String body = "hello there"; |
64 | 82 |
|
65 |
| - // twice |
66 |
| - response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); |
| 83 | + Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); |
| 84 | + assertEquals(response.getResponseBody(), body); |
67 | 85 |
|
68 |
| - assertEquals(response.getResponseBody(), body); |
69 |
| - } |
| 86 | + response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); |
| 87 | + assertEquals(response.getResponseBody(), body); |
| 88 | + }); |
| 89 | + }); |
70 | 90 | }
|
71 | 91 |
|
72 |
| - @Test(groups = "standalone") |
73 |
| - public void multipleSSLWithoutCacheTest() throws Exception { |
| 92 | + @Test |
| 93 | + public void multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy() throws Throwable { |
74 | 94 |
|
75 | 95 | KeepAliveStrategy keepAliveStrategy = new KeepAliveStrategy() {
|
76 |
| - |
77 | 96 | @Override
|
78 | 97 | public boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse) {
|
79 | 98 | return !ahcRequest.getUri().isSecured();
|
80 | 99 | }
|
81 | 100 | };
|
82 | 101 |
|
83 |
| - try (AsyncHttpClient c = asyncHttpClient(config().setSslEngineFactory(createSslEngineFactory(new AtomicBoolean(true))).setKeepAliveStrategy(keepAliveStrategy))) { |
84 |
| - String body = "hello there"; |
85 |
| - c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute(); |
| 102 | + withClient(config().setSslEngineFactory(createSslEngineFactory()).setKeepAliveStrategy(keepAliveStrategy)).run(client -> { |
| 103 | + withServer(server).run(server -> { |
| 104 | + server.enqueueEcho(); |
| 105 | + server.enqueueEcho(); |
| 106 | + server.enqueueEcho(); |
86 | 107 |
|
87 |
| - c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute(); |
| 108 | + String body = "hello there"; |
88 | 109 |
|
89 |
| - Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(); |
| 110 | + client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute(); |
| 111 | + client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute(); |
90 | 112 |
|
91 |
| - assertEquals(response.getResponseBody(), body); |
92 |
| - } |
| 113 | + Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(); |
| 114 | + assertEquals(response.getResponseBody(), body); |
| 115 | + }); |
| 116 | + }); |
93 | 117 | }
|
94 | 118 |
|
95 |
| - @Test(groups = "standalone") |
96 |
| - public void reconnectsAfterFailedCertificationPath() throws Exception { |
97 |
| - |
98 |
| - AtomicBoolean trust = new AtomicBoolean(false); |
99 |
| - try (AsyncHttpClient client = asyncHttpClient(config().setSslEngineFactory(createSslEngineFactory(trust)))) { |
100 |
| - String body = "hello there"; |
101 |
| - |
102 |
| - // first request fails because server certificate is rejected |
103 |
| - Throwable cause = null; |
104 |
| - try { |
105 |
| - client.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); |
106 |
| - } catch (final ExecutionException e) { |
107 |
| - cause = e.getCause(); |
108 |
| - } |
109 |
| - assertNotNull(cause); |
| 119 | + @Test |
| 120 | + public void reconnectAfterFailedCertificationPath() throws Throwable { |
| 121 | + |
| 122 | + AtomicBoolean trust = new AtomicBoolean(); |
| 123 | + |
| 124 | + withClient(config().setSslEngineFactory(createSslEngineFactory(trust))).run(client -> { |
| 125 | + withServer(server).run(server -> { |
| 126 | + server.enqueueEcho(); |
| 127 | + server.enqueueEcho(); |
110 | 128 |
|
111 |
| - // second request should succeed |
112 |
| - trust.set(true); |
113 |
| - Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); |
| 129 | + String body = "hello there"; |
114 | 130 |
|
115 |
| - assertEquals(response.getResponseBody(), body); |
116 |
| - } |
| 131 | + // first request fails because server certificate is rejected |
| 132 | + Throwable cause = null; |
| 133 | + try { |
| 134 | + client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); |
| 135 | + } catch (final ExecutionException e) { |
| 136 | + cause = e.getCause(); |
| 137 | + } |
| 138 | + assertNotNull(cause); |
| 139 | + |
| 140 | + // second request should succeed |
| 141 | + trust.set(true); |
| 142 | + Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); |
| 143 | + |
| 144 | + assertEquals(response.getResponseBody(), body); |
| 145 | + }); |
| 146 | + }); |
117 | 147 | }
|
118 | 148 |
|
119 |
| - @Test(groups = "standalone", timeOut = 2000) |
| 149 | + @Test(timeOut = 2000, expectedExceptions = SSLHandshakeException.class) |
120 | 150 | public void failInstantlyIfNotAllowedSelfSignedCertificate() throws Throwable {
|
121 |
| - |
122 |
| - try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(2000))) { |
123 |
| - try { |
124 |
| - client.prepareGet(getTargetUrl()).execute().get(TIMEOUT, TimeUnit.SECONDS); |
125 |
| - } catch (ExecutionException e) { |
126 |
| - assertTrue(e.getCause() instanceof ConnectException, "Expecting a ConnectException"); |
127 |
| - assertTrue(e.getCause().getCause() instanceof SSLHandshakeException, "Expecting SSLHandshakeException cause"); |
128 |
| - } |
129 |
| - } |
| 151 | + withClient(config().setRequestTimeout(2000)).run(client -> { |
| 152 | + withServer(server).run(server -> { |
| 153 | + server.enqueueEcho(); |
| 154 | + try { |
| 155 | + client.prepareGet(getTargetUrl()).execute().get(TIMEOUT, SECONDS); |
| 156 | + } catch (ExecutionException e) { |
| 157 | + throw e.getCause().getCause(); |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
130 | 161 | }
|
131 | 162 |
|
132 | 163 | @Test(groups = "standalone")
|
133 |
| - public void testNormalEventsFired() throws Exception { |
134 |
| - try (AsyncHttpClient client = asyncHttpClient(config().setSslEngineFactory(createSslEngineFactory(new AtomicBoolean(true))))) { |
135 |
| - EventCollectingHandler handler = new EventCollectingHandler(); |
136 |
| - client.preparePost(getTargetUrl()).setBody("whatever").execute(handler).get(3, TimeUnit.SECONDS); |
137 |
| - handler.waitForCompletion(3, TimeUnit.SECONDS); |
138 |
| - |
139 |
| - Object[] expectedEvents = new Object[] { // |
140 |
| - CONNECTION_POOL_EVENT,// |
141 |
| - HOSTNAME_RESOLUTION_EVENT,// |
142 |
| - HOSTNAME_RESOLUTION_SUCCESS_EVENT,// |
143 |
| - CONNECTION_OPEN_EVENT,// |
144 |
| - CONNECTION_SUCCESS_EVENT,// |
145 |
| - TLS_HANDSHAKE_EVENT,// |
146 |
| - TLS_HANDSHAKE_SUCCESS_EVENT,// |
147 |
| - REQUEST_SEND_EVENT,// |
148 |
| - HEADERS_WRITTEN_EVENT,// |
149 |
| - STATUS_RECEIVED_EVENT,// |
150 |
| - HEADERS_RECEIVED_EVENT,// |
151 |
| - CONNECTION_OFFER_EVENT,// |
152 |
| - COMPLETED_EVENT }; |
153 |
| - |
154 |
| - assertEquals(handler.firedEvents.toArray(), expectedEvents, "Got " + Arrays.toString(handler.firedEvents.toArray())); |
155 |
| - } |
| 164 | + public void testNormalEventsFired() throws Throwable { |
| 165 | + withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client -> { |
| 166 | + withServer(server).run(server -> { |
| 167 | + EventCollectingHandler handler = new EventCollectingHandler(); |
| 168 | + |
| 169 | + server.enqueueEcho(); |
| 170 | + client.preparePost(getTargetUrl()).setBody("whatever").execute(handler).get(3, SECONDS); |
| 171 | + handler.waitForCompletion(3, SECONDS); |
| 172 | + |
| 173 | + Object[] expectedEvents = new Object[] { // |
| 174 | + CONNECTION_POOL_EVENT,// |
| 175 | + HOSTNAME_RESOLUTION_EVENT,// |
| 176 | + HOSTNAME_RESOLUTION_SUCCESS_EVENT,// |
| 177 | + CONNECTION_OPEN_EVENT,// |
| 178 | + CONNECTION_SUCCESS_EVENT,// |
| 179 | + TLS_HANDSHAKE_EVENT,// |
| 180 | + TLS_HANDSHAKE_SUCCESS_EVENT,// |
| 181 | + REQUEST_SEND_EVENT,// |
| 182 | + HEADERS_WRITTEN_EVENT,// |
| 183 | + STATUS_RECEIVED_EVENT,// |
| 184 | + HEADERS_RECEIVED_EVENT,// |
| 185 | + CONNECTION_OFFER_EVENT,// |
| 186 | + COMPLETED_EVENT }; |
| 187 | + |
| 188 | + assertEquals(handler.firedEvents.toArray(), expectedEvents, "Got " + Arrays.toString(handler.firedEvents.toArray())); |
| 189 | + }); |
| 190 | + }); |
156 | 191 | }
|
157 | 192 | }
|
0 commit comments