diff --git a/core/src/test/java/org/elasticsearch/http/netty/NettyHttpCompressionIT.java b/core/src/test/java/org/elasticsearch/http/netty/NettyHttpCompressionIT.java index 9e995f0a010..32a331d61f7 100644 --- a/core/src/test/java/org/elasticsearch/http/netty/NettyHttpCompressionIT.java +++ b/core/src/test/java/org/elasticsearch/http/netty/NettyHttpCompressionIT.java @@ -62,11 +62,12 @@ public class NettyHttpCompressionIT extends ESIntegTestCase { // we need to intercept early, otherwise internal logic in HttpClient will just remove the header and we cannot verify it ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); try (RestClient client = restClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, - new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING)); - assertEquals(200, response.getStatusLine().getStatusCode()); - assertTrue(headerExtractor.hasContentEncodingHeader()); - assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue()); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, + new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING))) { + assertEquals(200, response.getStatusLine().getStatusCode()); + assertTrue(headerExtractor.hasContentEncodingHeader()); + assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue()); + } } } @@ -75,9 +76,10 @@ public class NettyHttpCompressionIT extends ESIntegTestCase { ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build(); try (RestClient client = restClient(httpClient)) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null); - assertEquals(200, response.getStatusLine().getStatusCode()); - assertFalse(headerExtractor.hasContentEncodingHeader()); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null)) { + assertEquals(200, response.getStatusLine().getStatusCode()); + assertFalse(headerExtractor.hasContentEncodingHeader()); + } } } @@ -87,9 +89,11 @@ public class NettyHttpCompressionIT extends ESIntegTestCase { // this disable content compression in both directions (request and response) CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build(); try (RestClient client = restClient(httpClient)) { - ElasticsearchResponse response = client.performRequest("POST", "/company/employees/1", Collections.emptyMap(), SAMPLE_DOCUMENT); - assertEquals(201, response.getStatusLine().getStatusCode()); - assertFalse(headerExtractor.hasContentEncodingHeader()); + try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/1", + Collections.emptyMap(), SAMPLE_DOCUMENT)) { + assertEquals(201, response.getStatusLine().getStatusCode()); + assertFalse(headerExtractor.hasContentEncodingHeader()); + } } } @@ -98,9 +102,11 @@ public class NettyHttpCompressionIT extends ESIntegTestCase { ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); // we don't call #disableContentCompression() hence the client will send the content compressed try (RestClient client = restClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) { - ElasticsearchResponse response = client.performRequest("POST", "/company/employees/2", Collections.emptyMap(), SAMPLE_DOCUMENT); - assertEquals(201, response.getStatusLine().getStatusCode()); - assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue()); + try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/2", + Collections.emptyMap(), SAMPLE_DOCUMENT)) { + assertEquals(201, response.getStatusLine().getStatusCode()); + assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue()); + } } } diff --git a/core/src/test/java/org/elasticsearch/plugins/ResponseHeaderPluginIT.java b/core/src/test/java/org/elasticsearch/plugins/ResponseHeaderPluginIT.java index 4ae9e3912b2..4b905d7b1a8 100644 --- a/core/src/test/java/org/elasticsearch/plugins/ResponseHeaderPluginIT.java +++ b/core/src/test/java/org/elasticsearch/plugins/ResponseHeaderPluginIT.java @@ -66,10 +66,11 @@ public class ResponseHeaderPluginIT extends ESIntegTestCase { assertThat(response.getFirstHeader("Secret"), equalTo("required")); } - ElasticsearchResponse authResponse = client.performRequest("GET", "/_protected", Collections.emptyMap(), null, - new BasicHeader("Secret", "password")); - assertThat(authResponse, hasStatus(OK)); - assertThat(authResponse.getFirstHeader("Secret"), equalTo("granted")); + try (ElasticsearchResponse authResponse = client.performRequest("GET", "/_protected", Collections.emptyMap(), null, + new BasicHeader("Secret", "password"))) { + assertThat(authResponse, hasStatus(OK)); + assertThat(authResponse.getFirstHeader("Secret"), equalTo("granted")); + } } } } diff --git a/core/src/test/java/org/elasticsearch/rest/CorsNotSetIT.java b/core/src/test/java/org/elasticsearch/rest/CorsNotSetIT.java index e407bccf872..c6c23da102f 100644 --- a/core/src/test/java/org/elasticsearch/rest/CorsNotSetIT.java +++ b/core/src/test/java/org/elasticsearch/rest/CorsNotSetIT.java @@ -48,20 +48,22 @@ public class CorsNotSetIT extends ESIntegTestCase { public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception { String corsValue = "http://localhost:9200"; try (RestClient restClient = restClient()) { - ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null, - new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue)); - assertThat(response.getStatusLine().getStatusCode(), is(200)); - assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); - assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue()); + try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null, + new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) { + assertThat(response.getStatusLine().getStatusCode(), is(200)); + assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue()); + } } } public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception { try (RestClient restClient = restClient()) { - ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null); - assertThat(response.getStatusLine().getStatusCode(), is(200)); - assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); - assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue()); + try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null)) { + assertThat(response.getStatusLine().getStatusCode(), is(200)); + assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue()); + } } } } diff --git a/core/src/test/java/org/elasticsearch/rest/CorsRegexIT.java b/core/src/test/java/org/elasticsearch/rest/CorsRegexIT.java index 729fed8e96d..49bfd82e613 100644 --- a/core/src/test/java/org/elasticsearch/rest/CorsRegexIT.java +++ b/core/src/test/java/org/elasticsearch/rest/CorsRegexIT.java @@ -63,15 +63,16 @@ public class CorsRegexIT extends ESIntegTestCase { public void testThatRegularExpressionWorksOnMatch() throws Exception { String corsValue = "http://localhost:9200"; try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, - new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue)); - assertResponseWithOriginheader(response, corsValue); - + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, + new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) { + assertResponseWithOriginheader(response, corsValue); + } corsValue = "https://localhost:9200"; - response = client.performRequest("GET", "/", Collections.emptyMap(), null, - new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue)); - assertResponseWithOriginheader(response, corsValue); - assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), is("true")); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, + new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));) { + assertResponseWithOriginheader(response, corsValue); + assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), is("true")); + } } } @@ -90,29 +91,32 @@ public class CorsRegexIT extends ESIntegTestCase { public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception { try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, - new BasicHeader("User-Agent", "Mozilla Bar")); - assertThat(response.getStatusLine().getStatusCode(), is(200)); - assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, + new BasicHeader("User-Agent", "Mozilla Bar"))) { + assertThat(response.getStatusLine().getStatusCode(), is(200)); + assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + } } } public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception { try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null); - assertThat(response.getStatusLine().getStatusCode(), is(200)); - assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null)) { + assertThat(response.getStatusLine().getStatusCode(), is(200)); + assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue()); + } } } public void testThatPreFlightRequestWorksOnMatch() throws Exception { String corsValue = "http://localhost:9200"; try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("OPTIONS", "/", Collections.emptyMap(), null, + try (ElasticsearchResponse response = client.performRequest("OPTIONS", "/", Collections.emptyMap(), null, new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue), - new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET")); - assertResponseWithOriginheader(response, corsValue); - assertNotNull(response.getFirstHeader("Access-Control-Allow-Methods")); + new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET"));) { + assertResponseWithOriginheader(response, corsValue); + assertNotNull(response.getFirstHeader("Access-Control-Allow-Methods")); + } } } diff --git a/core/src/test/java/org/elasticsearch/rest/action/main/RestMainActionIT.java b/core/src/test/java/org/elasticsearch/rest/action/main/RestMainActionIT.java index 9c2b0284ef7..0723a9573b4 100644 --- a/core/src/test/java/org/elasticsearch/rest/action/main/RestMainActionIT.java +++ b/core/src/test/java/org/elasticsearch/rest/action/main/RestMainActionIT.java @@ -42,18 +42,20 @@ public class RestMainActionIT extends ESIntegTestCase { public void testHeadRequest() throws IOException { try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("HEAD", "/", Collections.emptyMap(), null); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); - assertNull(response.getEntity()); + try (ElasticsearchResponse response = client.performRequest("HEAD", "/", Collections.emptyMap(), null)) { + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + assertNull(response.getEntity()); + } } } public void testGetRequest() throws IOException { try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); - assertNotNull(response.getEntity()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name")); + try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null)) { + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + assertNotNull(response.getEntity()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name")); + } } } } diff --git a/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportIT.java b/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportIT.java index aae15dc778a..e2139d73218 100644 --- a/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportIT.java +++ b/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportIT.java @@ -221,15 +221,16 @@ public class ContextAndHeaderTransportIT extends ESIntegTestCase { } try (RestClient client = restClient()) { - ElasticsearchResponse response = client.performRequest("GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null, - new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue)); - assertThat(response, hasStatus(OK)); - List searchRequests = getRequests(SearchRequest.class); - assertThat(searchRequests, hasSize(greaterThan(0))); - for (RequestAndHeaders requestAndHeaders : searchRequests) { - assertThat(requestAndHeaders.headers.containsKey(relevantHeaderName), is(true)); - // was not specified, thus is not included - assertThat(requestAndHeaders.headers.containsKey(randomHeaderKey), is(false)); + try (ElasticsearchResponse response = client.performRequest("GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null, + new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue))) { + assertThat(response, hasStatus(OK)); + List searchRequests = getRequests(SearchRequest.class); + assertThat(searchRequests, hasSize(greaterThan(0))); + for (RequestAndHeaders requestAndHeaders : searchRequests) { + assertThat(requestAndHeaders.headers.containsKey(relevantHeaderName), is(true)); + // was not specified, thus is not included + assertThat(requestAndHeaders.headers.containsKey(randomHeaderKey), is(false)); + } } } }