@@ -77,6 +77,7 @@ public void init() throws Exception {
7777 @ After
7878 public void shutdown () throws Exception {
7979 webServer .close ();
80+ httpClient .close ();
8081 }
8182
8283 public void testBasics () throws Exception {
@@ -184,17 +185,18 @@ public void testHttps() throws Exception {
184185 .setSecureSettings (secureSettings )
185186 .build ();
186187 }
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 ();
195196
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+ }
198200 }
199201
200202 public void testHttpsDisableHostnameVerification () throws Exception {
@@ -217,18 +219,19 @@ public void testHttpsDisableHostnameVerification() throws Exception {
217219 .setSecureSettings (secureSettings )
218220 .build ();
219221 }
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 ();
229231
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+ }
232235 }
233236
234237 public void testHttpsClientAuth () throws Exception {
@@ -241,19 +244,20 @@ public void testHttpsClientAuth() throws Exception {
241244 .build ();
242245
243246 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+ }
246250 }
247251
248- private void testSslMockWebserver (SSLContext sslContext , boolean needClientAuth ) throws IOException {
252+ private void testSslMockWebserver (HttpClient client , SSLContext sslContext , boolean needClientAuth ) throws IOException {
249253 try (MockWebServer mockWebServer = new MockWebServer (sslContext , needClientAuth )) {
250254 mockWebServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody ("body" ));
251255 mockWebServer .start ();
252256
253257 HttpRequest .Builder request = HttpRequest .builder ("localhost" , mockWebServer .getPort ())
254258 .scheme (Scheme .HTTPS )
255259 .path ("/test" );
256- HttpResponse response = httpClient .execute (request .build ());
260+ HttpResponse response = client .execute (request .build ());
257261 assertThat (response .status (), equalTo (200 ));
258262 assertThat (response .body ().utf8ToString (), equalTo ("body" ));
259263
@@ -288,14 +292,14 @@ public void testHttpResponseWithAnyStatusCodeCanReturnBody() throws Exception {
288292
289293 @ Network
290294 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+ }
299303 }
300304
301305 public void testThatProxyCanBeConfigured () throws Exception {
@@ -307,15 +311,16 @@ public void testThatProxyCanBeConfigured() throws Exception {
307311 .put (HttpSettings .PROXY_HOST .getKey (), "localhost" )
308312 .put (HttpSettings .PROXY_PORT .getKey (), proxyServer .getPort ())
309313 .build ();
310- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
311314
312315 HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
313316 .method (HttpMethod .GET )
314317 .path ("/" );
315318
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+ }
319324
320325 // ensure we hit the proxyServer and not the webserver
321326 assertThat (webServer .requests (), hasSize (0 ));
@@ -386,16 +391,16 @@ public void testProxyCanHaveDifferentSchemeThanRequest() throws Exception {
386391 .setSecureSettings (secureSettings )
387392 .build ();
388393
389- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
390-
391394 HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
392395 .method (HttpMethod .GET )
393396 .scheme (Scheme .HTTP )
394397 .path ("/" );
395398
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+ }
399404
400405 // ensure we hit the proxyServer and not the webserver
401406 assertThat (webServer .requests (), hasSize (0 ));
@@ -413,16 +418,17 @@ public void testThatProxyCanBeOverriddenByRequest() throws Exception {
413418 .put (HttpSettings .PROXY_PORT .getKey (), proxyServer .getPort () + 1 )
414419 .put (HttpSettings .PROXY_HOST .getKey (), "https" )
415420 .build ();
416- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (settings , environment ));
417421
418422 HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ())
419423 .method (HttpMethod .GET )
420424 .proxy (new HttpProxy ("localhost" , proxyServer .getPort (), Scheme .HTTP ))
421425 .path ("/" );
422426
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+ }
426432
427433 // ensure we hit the proxyServer and not the webserver
428434 assertThat (webServer .requests (), hasSize (0 ));
@@ -535,12 +541,13 @@ public void testMaxHttpResponseSize() throws Exception {
535541 Settings settings = Settings .builder ()
536542 .put (HttpSettings .MAX_HTTP_RESPONSE_SIZE .getKey (), new ByteSizeValue (randomBytesLength - 1 , ByteSizeUnit .BYTES ))
537543 .build ();
538- HttpClient httpClient = new HttpClient (settings , authRegistry , new SSLService (environment .settings (), environment ));
539544
540545 HttpRequest .Builder requestBuilder = HttpRequest .builder ("localhost" , webServer .getPort ()).method (HttpMethod .GET ).path ("/" );
541546
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+ }
544551 }
545552
546553 public void testThatGetRedirectIsFollowed () throws Exception {
0 commit comments