[TEST] add a lot of forgotten try with resources to wrap ElasticsearchResponses

This commit is contained in:
javanna 2016-06-03 16:42:07 +02:00 committed by Luca Cavanna
parent 13a27a34f8
commit 4fa824f891
6 changed files with 78 additions and 62 deletions

View File

@ -62,24 +62,26 @@ 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));
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());
}
}
}
public void testUncompressedResponseByDefault() throws Exception {
ensureGreen();
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);
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null)) {
assertEquals(200, response.getStatusLine().getStatusCode());
assertFalse(headerExtractor.hasContentEncodingHeader());
}
}
}
public void testCanInterpretUncompressedRequest() throws Exception {
ensureGreen();
@ -87,22 +89,26 @@ 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);
try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/1",
Collections.emptyMap(), SAMPLE_DOCUMENT)) {
assertEquals(201, response.getStatusLine().getStatusCode());
assertFalse(headerExtractor.hasContentEncodingHeader());
}
}
}
public void testCanInterpretCompressedRequest() throws Exception {
ensureGreen();
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);
try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/2",
Collections.emptyMap(), SAMPLE_DOCUMENT)) {
assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
}
}
}
private static class ContentEncodingHeaderExtractor implements HttpResponseInterceptor {
private Header contentEncodingHeader;

View File

@ -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"));
try (ElasticsearchResponse authResponse = client.performRequest("GET", "/_protected", Collections.emptyMap(), null,
new BasicHeader("Secret", "password"))) {
assertThat(authResponse, hasStatus(OK));
assertThat(authResponse.getFirstHeader("Secret"), equalTo("granted"));
}
}
}
}

View File

@ -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));
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);
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());
}
}
}
}

View File

@ -63,17 +63,18 @@ 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));
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));
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"));
}
}
}
public void testThatRegularExpressionReturnsForbiddenOnNonMatch() throws Exception {
try (RestClient client = restClient()) {
@ -90,31 +91,34 @@ 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"));
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);
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"));
new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET"));) {
assertResponseWithOriginheader(response, corsValue);
assertNotNull(response.getFirstHeader("Access-Control-Allow-Methods"));
}
}
}
public void testThatPreFlightRequestReturnsNullOnNonMatch() throws Exception {
try (RestClient client = restClient()) {

View File

@ -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);
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);
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"));
}
}
}
}

View File

@ -221,8 +221,8 @@ 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));
try (ElasticsearchResponse response = client.performRequest("GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null,
new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue))) {
assertThat(response, hasStatus(OK));
List<RequestAndHeaders> searchRequests = getRequests(SearchRequest.class);
assertThat(searchRequests, hasSize(greaterThan(0)));
@ -233,6 +233,7 @@ public class ContextAndHeaderTransportIT extends ESIntegTestCase {
}
}
}
}
private List<RequestAndHeaders> getRequests(Class<?> clazz) {
List<RequestAndHeaders> results = new ArrayList<>();