@@ -77,6 +77,7 @@ public void init() throws Exception {
77
77
@ After
78
78
public void shutdown () throws Exception {
79
79
webServer .close ();
80
+ httpClient .close ();
80
81
}
81
82
82
83
public void testBasics () throws Exception {
@@ -184,17 +185,18 @@ public void testHttps() throws Exception {
184
185
.setSecureSettings (secureSettings )
185
186
.build ();
186
187
}
187
- httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
188
- secureSettings = new MockSecureSettings ();
189
- // We can't use the client created above for the server since it is only a truststore
190
- secureSettings .setString ("xpack.ssl.keystore.secure_password" , "testnode" );
191
- Settings settings2 = Settings .builder ()
192
- .put ("xpack.ssl.keystore.path" , getDataPath ("/org/elasticsearch/xpack/security/keystore/testnode.jks" ))
193
- .setSecureSettings (secureSettings )
194
- .build ();
188
+ try ( HttpClient client = new HttpClient (settings , authRegistry , new SSLService (settings , environment ))) {
189
+ secureSettings = new MockSecureSettings ();
190
+ // We can't use the client created above for the server since it is only a truststore
191
+ secureSettings .setString ("xpack.ssl.keystore.secure_password" , "testnode" );
192
+ Settings settings2 = Settings .builder ()
193
+ .put ("xpack.ssl.keystore.path" , getDataPath ("/org/elasticsearch/xpack/security/keystore/testnode.jks" ))
194
+ .setSecureSettings (secureSettings )
195
+ .build ();
195
196
196
- TestsSSLService sslService = new TestsSSLService (settings2 , environment );
197
- testSslMockWebserver (sslService .sslContext (), false );
197
+ TestsSSLService sslService = new TestsSSLService (settings2 , environment );
198
+ testSslMockWebserver (client , sslService .sslContext (), false );
199
+ }
198
200
}
199
201
200
202
public void testHttpsDisableHostnameVerification () throws Exception {
@@ -217,18 +219,19 @@ public void testHttpsDisableHostnameVerification() throws Exception {
217
219
.setSecureSettings (secureSettings )
218
220
.build ();
219
221
}
220
- httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
221
- MockSecureSettings secureSettings = new MockSecureSettings ();
222
- // We can't use the client created above for the server since it only defines a truststore
223
- secureSettings .setString ("xpack.ssl.keystore.secure_password" , "testnode-no-subjaltname" );
224
- Settings settings2 = Settings .builder ()
225
- .put ("xpack.ssl.keystore.path" ,
226
- getDataPath ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-no-subjaltname.jks" ))
227
- .setSecureSettings (secureSettings )
228
- .build ();
222
+ try ( HttpClient client = new HttpClient (settings , authRegistry , new SSLService (settings , environment ))) {
223
+ MockSecureSettings secureSettings = new MockSecureSettings ();
224
+ // We can't use the client created above for the server since it only defines a truststore
225
+ secureSettings .setString ("xpack.ssl.keystore.secure_password" , "testnode-no-subjaltname" );
226
+ Settings settings2 = Settings .builder ()
227
+ .put ("xpack.ssl.keystore.path" ,
228
+ getDataPath ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-no-subjaltname.jks" ))
229
+ .setSecureSettings (secureSettings )
230
+ .build ();
229
231
230
- TestsSSLService sslService = new TestsSSLService (settings2 , environment );
231
- testSslMockWebserver (sslService .sslContext (), false );
232
+ TestsSSLService sslService = new TestsSSLService (settings2 , environment );
233
+ testSslMockWebserver (client , sslService .sslContext (), false );
234
+ }
232
235
}
233
236
234
237
public void testHttpsClientAuth () throws Exception {
@@ -241,19 +244,20 @@ public void testHttpsClientAuth() throws Exception {
241
244
.build ();
242
245
243
246
TestsSSLService sslService = new TestsSSLService (settings , environment );
244
- httpClient = new HttpClient (settings , authRegistry , sslService );
245
- testSslMockWebserver (sslService .sslContext (), true );
247
+ try (HttpClient client = new HttpClient (settings , authRegistry , sslService )) {
248
+ testSslMockWebserver (client , sslService .sslContext (), true );
249
+ }
246
250
}
247
251
248
- private void testSslMockWebserver (SSLContext sslContext , boolean needClientAuth ) throws IOException {
252
+ private void testSslMockWebserver (HttpClient client , SSLContext sslContext , boolean needClientAuth ) throws IOException {
249
253
try (MockWebServer mockWebServer = new MockWebServer (sslContext , needClientAuth )) {
250
254
mockWebServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody ("body" ));
251
255
mockWebServer .start ();
252
256
253
257
HttpRequest .Builder request = HttpRequest .builder ("localhost" , mockWebServer .getPort ())
254
258
.scheme (Scheme .HTTPS )
255
259
.path ("/test" );
256
- HttpResponse response = httpClient .execute (request .build ());
260
+ HttpResponse response = client .execute (request .build ());
257
261
assertThat (response .status (), equalTo (200 ));
258
262
assertThat (response .body ().utf8ToString (), equalTo ("body" ));
259
263
@@ -288,14 +292,14 @@ public void testHttpResponseWithAnyStatusCodeCanReturnBody() throws Exception {
288
292
289
293
@ Network
290
294
public void testHttpsWithoutTruststore () throws Exception {
291
- HttpClient httpClient = new HttpClient (Settings .EMPTY , authRegistry , new SSLService (Settings .EMPTY , environment ));
292
-
293
- // Known server with a valid cert from a commercial CA
294
- HttpRequest . Builder request = HttpRequest . builder ( "www.elastic.co" , 443 ). scheme ( Scheme . HTTPS );
295
- HttpResponse response = httpClient . execute ( request . build ( ));
296
- assertThat (response .status (), equalTo ( 200 ));
297
- assertThat (response .hasContent (), is ( true ));
298
- assertThat ( response . body (), notNullValue ());
295
+ try ( HttpClient client = new HttpClient (Settings .EMPTY , authRegistry , new SSLService (Settings .EMPTY , environment ))) {
296
+ // Known server with a valid cert from a commercial CA
297
+ HttpRequest . Builder request = HttpRequest . builder ( "www.elastic.co" , 443 ). scheme ( Scheme . HTTPS );
298
+ HttpResponse response = client . execute ( request . build () );
299
+ assertThat ( response . status (), equalTo ( 200 ));
300
+ assertThat (response .hasContent (), is ( true ));
301
+ assertThat (response .body (), notNullValue ( ));
302
+ }
299
303
}
300
304
301
305
public void testThatProxyCanBeConfigured () throws Exception {
@@ -307,15 +311,16 @@ public void testThatProxyCanBeConfigured() throws Exception {
307
311
.put (HttpSettings .PROXY_HOST .getKey (), "localhost" )
308
312
.put (HttpSettings .PROXY_PORT .getKey (), proxyServer .getPort ())
309
313
.build ();
310
- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
311
314
312
315
HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
313
316
.method (HttpMethod .GET )
314
317
.path ("/" );
315
318
316
- HttpResponse response = httpClient .execute (requestBuilder .build ());
317
- assertThat (response .status (), equalTo (200 ));
318
- assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
319
+ try (HttpClient client = new HttpClient (settings , authRegistry , new SSLService (settings , environment ))) {
320
+ HttpResponse response = client .execute (requestBuilder .build ());
321
+ assertThat (response .status (), equalTo (200 ));
322
+ assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
323
+ }
319
324
320
325
// ensure we hit the proxyServer and not the webserver
321
326
assertThat (webServer .requests (), hasSize (0 ));
@@ -386,16 +391,16 @@ public void testProxyCanHaveDifferentSchemeThanRequest() throws Exception {
386
391
.setSecureSettings (secureSettings )
387
392
.build ();
388
393
389
- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
390
-
391
394
HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
392
395
.method (HttpMethod .GET )
393
396
.scheme (Scheme .HTTP )
394
397
.path ("/" );
395
398
396
- HttpResponse response = httpClient .execute (requestBuilder .build ());
397
- assertThat (response .status (), equalTo (200 ));
398
- assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
399
+ try (HttpClient client = new HttpClient (settings , authRegistry , new SSLService (settings , environment ))) {
400
+ HttpResponse response = client .execute (requestBuilder .build ());
401
+ assertThat (response .status (), equalTo (200 ));
402
+ assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
403
+ }
399
404
400
405
// ensure we hit the proxyServer and not the webserver
401
406
assertThat (webServer .requests (), hasSize (0 ));
@@ -413,16 +418,17 @@ public void testThatProxyCanBeOverriddenByRequest() throws Exception {
413
418
.put (HttpSettings .PROXY_PORT .getKey (), proxyServer .getPort () + 1 )
414
419
.put (HttpSettings .PROXY_HOST .getKey (), "https" )
415
420
.build ();
416
- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
417
421
418
422
HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
419
423
.method (HttpMethod .GET )
420
424
.proxy (new HttpProxy ("localhost" , proxyServer .getPort (), Scheme .HTTP ))
421
425
.path ("/" );
422
426
423
- HttpResponse response = httpClient .execute (requestBuilder .build ());
424
- assertThat (response .status (), equalTo (200 ));
425
- assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
427
+ try (HttpClient client = new HttpClient (settings , authRegistry , new SSLService (settings , environment ))) {
428
+ HttpResponse response = client .execute (requestBuilder .build ());
429
+ assertThat (response .status (), equalTo (200 ));
430
+ assertThat (response .body ().utf8ToString (), equalTo ("fullProxiedContent" ));
431
+ }
426
432
427
433
// ensure we hit the proxyServer and not the webserver
428
434
assertThat (webServer .requests (), hasSize (0 ));
@@ -535,12 +541,13 @@ public void testMaxHttpResponseSize() throws Exception {
535
541
Settings settings = Settings .builder ()
536
542
.put (HttpSettings .MAX_HTTP_RESPONSE_SIZE .getKey (), new ByteSizeValue (randomBytesLength - 1 , ByteSizeUnit .BYTES ))
537
543
.build ();
538
- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (environment .settings (), environment ));
539
544
540
545
HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ()).method (HttpMethod .GET ).path ("/" );
541
546
542
- IOException e = expectThrows (IOException .class , () -> httpClient .execute (requestBuilder .build ()));
543
- assertThat (e .getMessage (), startsWith ("Maximum limit of" ));
547
+ try (HttpClient client = new HttpClient (settings , authRegistry , new SSLService (environment .settings (), environment ))) {
548
+ IOException e = expectThrows (IOException .class , () -> client .execute (requestBuilder .build ()));
549
+ assertThat (e .getMessage (), startsWith ("Maximum limit of" ));
550
+ }
544
551
}
545
552
546
553
public void testThatGetRedirectIsFollowed () throws Exception {
0 commit comments