29
29
import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
30
30
import com .github .tomakehurst .wiremock .common .FatalStartupException ;
31
31
import com .github .tomakehurst .wiremock .http .RequestMethod ;
32
+ import com .github .tomakehurst .wiremock .http .trafficlistener .WiremockNetworkTrafficListener ;
32
33
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
33
34
import com .github .tomakehurst .wiremock .matching .RequestPatternBuilder ;
34
35
import java .io .ByteArrayInputStream ;
35
36
import java .io .IOException ;
36
37
import java .net .HttpURLConnection ;
38
+ import java .net .Socket ;
37
39
import java .net .URI ;
40
+ import java .nio .ByteBuffer ;
38
41
import java .nio .charset .StandardCharsets ;
39
42
import java .util .Random ;
43
+ import java .util .concurrent .atomic .AtomicInteger ;
40
44
import javax .net .ssl .SSLHandshakeException ;
41
45
import javax .net .ssl .TrustManager ;
42
46
import javax .net .ssl .TrustManagerFactory ;
57
61
public abstract class SdkHttpClientTestSuite {
58
62
private static final Logger LOG = Logger .loggerFor (SdkHttpClientTestSuite .class );
59
63
64
+ private static final ConnectionCountingTrafficListener CONNECTION_COUNTER = new ConnectionCountingTrafficListener ();
65
+
60
66
@ Rule
61
67
public WireMockRule mockServer = createWireMockRule ();
62
68
63
69
private final Random rng = new Random ();
64
70
71
+
65
72
@ Test
66
73
public void supportsResponseCode200 () throws Exception {
67
74
testForResponseCode (HttpURLConnection .HTTP_OK );
@@ -113,6 +120,30 @@ public void validatesHttpsCertificateIssuer() throws Exception {
113
120
.isInstanceOf (SSLHandshakeException .class );
114
121
}
115
122
123
+ @ Test
124
+ public void connectionPoolingWorks () throws Exception {
125
+ int initialOpenedConnections = CONNECTION_COUNTER .openedConnections ();
126
+
127
+ SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions ();
128
+ httpClientOptions .trustAll (true );
129
+ SdkHttpClient client = createSdkHttpClient (httpClientOptions );
130
+
131
+ stubForMockRequest (200 );
132
+
133
+ for (int i = 0 ; i < 5 ; i ++) {
134
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), SdkHttpMethod .POST );
135
+ HttpExecuteResponse response =
136
+ client .prepareRequest (HttpExecuteRequest .builder ()
137
+ .request (req )
138
+ .contentStreamProvider (req .contentStreamProvider ().orElse (null ))
139
+ .build ())
140
+ .call ();
141
+ response .responseBody ().ifPresent (IoUtils ::drainInputStream );
142
+ }
143
+
144
+ assertThat (CONNECTION_COUNTER .openedConnections ()).isEqualTo (initialOpenedConnections + 1 );
145
+ }
146
+
116
147
@ Test
117
148
public void testCustomTlsTrustManager () throws Exception {
118
149
WireMockServer selfSignedServer = HttpTestUtils .createSelfSignedServer ();
@@ -279,7 +310,9 @@ private WireMockRule createWireMockRule() {
279
310
int maxAttempts = 5 ;
280
311
for (int i = 0 ; i < maxAttempts ; ++i ) {
281
312
try {
282
- return new WireMockRule (wireMockConfig ().dynamicPort ().dynamicHttpsPort ());
313
+ return new WireMockRule (wireMockConfig ().dynamicPort ()
314
+ .dynamicHttpsPort ()
315
+ .networkTrafficListener (CONNECTION_COUNTER ));
283
316
} catch (FatalStartupException e ) {
284
317
int attemptNum = i + 1 ;
285
318
LOG .debug (() -> "Was not able to start WireMock server. Attempt " + attemptNum , e );
@@ -298,4 +331,29 @@ private WireMockRule createWireMockRule() {
298
331
299
332
throw new RuntimeException ("Unable to setup WireMock rule" );
300
333
}
334
+
335
+ private static class ConnectionCountingTrafficListener implements WiremockNetworkTrafficListener {
336
+ private final AtomicInteger openedConnections = new AtomicInteger (0 );
337
+
338
+ @ Override
339
+ public void opened (Socket socket ) {
340
+ openedConnections .incrementAndGet ();
341
+ }
342
+
343
+ @ Override
344
+ public void incoming (Socket socket , ByteBuffer bytes ) {
345
+ }
346
+
347
+ @ Override
348
+ public void outgoing (Socket socket , ByteBuffer bytes ) {
349
+ }
350
+
351
+ @ Override
352
+ public void closed (Socket socket ) {
353
+ }
354
+
355
+ public int openedConnections () {
356
+ return openedConnections .get ();
357
+ }
358
+ }
301
359
}
0 commit comments