[TEST] add a lot of forgotten try with resources to wrap ElasticsearchResponses
This commit is contained in:
parent
13a27a34f8
commit
4fa824f891
|
@ -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
|
// we need to intercept early, otherwise internal logic in HttpClient will just remove the header and we cannot verify it
|
||||||
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
||||||
try (RestClient client = restClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) {
|
try (RestClient client = restClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) {
|
||||||
ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||||
new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING));
|
new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING))) {
|
||||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
assertTrue(headerExtractor.hasContentEncodingHeader());
|
assertTrue(headerExtractor.hasContentEncodingHeader());
|
||||||
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
|
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,9 +76,10 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
|
||||||
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
||||||
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
|
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
|
||||||
try (RestClient client = restClient(httpClient)) {
|
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());
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
assertFalse(headerExtractor.hasContentEncodingHeader());
|
assertFalse(headerExtractor.hasContentEncodingHeader());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,9 +89,11 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
|
||||||
// this disable content compression in both directions (request and response)
|
// this disable content compression in both directions (request and response)
|
||||||
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
|
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
|
||||||
try (RestClient client = restClient(httpClient)) {
|
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",
|
||||||
assertEquals(201, response.getStatusLine().getStatusCode());
|
Collections.emptyMap(), SAMPLE_DOCUMENT)) {
|
||||||
assertFalse(headerExtractor.hasContentEncodingHeader());
|
assertEquals(201, response.getStatusLine().getStatusCode());
|
||||||
|
assertFalse(headerExtractor.hasContentEncodingHeader());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,9 +102,11 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
|
||||||
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
|
||||||
// we don't call #disableContentCompression() hence the client will send the content compressed
|
// we don't call #disableContentCompression() hence the client will send the content compressed
|
||||||
try (RestClient client = restClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) {
|
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",
|
||||||
assertEquals(201, response.getStatusLine().getStatusCode());
|
Collections.emptyMap(), SAMPLE_DOCUMENT)) {
|
||||||
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
|
assertEquals(201, response.getStatusLine().getStatusCode());
|
||||||
|
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,10 +66,11 @@ public class ResponseHeaderPluginIT extends ESIntegTestCase {
|
||||||
assertThat(response.getFirstHeader("Secret"), equalTo("required"));
|
assertThat(response.getFirstHeader("Secret"), equalTo("required"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ElasticsearchResponse authResponse = client.performRequest("GET", "/_protected", Collections.emptyMap(), null,
|
try (ElasticsearchResponse authResponse = client.performRequest("GET", "/_protected", Collections.emptyMap(), null,
|
||||||
new BasicHeader("Secret", "password"));
|
new BasicHeader("Secret", "password"))) {
|
||||||
assertThat(authResponse, hasStatus(OK));
|
assertThat(authResponse, hasStatus(OK));
|
||||||
assertThat(authResponse.getFirstHeader("Secret"), equalTo("granted"));
|
assertThat(authResponse.getFirstHeader("Secret"), equalTo("granted"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,20 +48,22 @@ public class CorsNotSetIT extends ESIntegTestCase {
|
||||||
public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception {
|
public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception {
|
||||||
String corsValue = "http://localhost:9200";
|
String corsValue = "http://localhost:9200";
|
||||||
try (RestClient restClient = restClient()) {
|
try (RestClient restClient = restClient()) {
|
||||||
ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||||
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));
|
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) {
|
||||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception {
|
public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception {
|
||||||
try (RestClient restClient = restClient()) {
|
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.getStatusLine().getStatusCode(), is(200));
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), nullValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,15 +63,16 @@ public class CorsRegexIT extends ESIntegTestCase {
|
||||||
public void testThatRegularExpressionWorksOnMatch() throws Exception {
|
public void testThatRegularExpressionWorksOnMatch() throws Exception {
|
||||||
String corsValue = "http://localhost:9200";
|
String corsValue = "http://localhost:9200";
|
||||||
try (RestClient client = restClient()) {
|
try (RestClient client = restClient()) {
|
||||||
ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||||
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));
|
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) {
|
||||||
assertResponseWithOriginheader(response, corsValue);
|
assertResponseWithOriginheader(response, corsValue);
|
||||||
|
}
|
||||||
corsValue = "https://localhost:9200";
|
corsValue = "https://localhost:9200";
|
||||||
response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||||
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));
|
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));) {
|
||||||
assertResponseWithOriginheader(response, corsValue);
|
assertResponseWithOriginheader(response, corsValue);
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), is("true"));
|
assertThat(response.getFirstHeader("Access-Control-Allow-Credentials"), is("true"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,29 +91,32 @@ public class CorsRegexIT extends ESIntegTestCase {
|
||||||
|
|
||||||
public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception {
|
public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception {
|
||||||
try (RestClient client = restClient()) {
|
try (RestClient client = restClient()) {
|
||||||
ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||||
new BasicHeader("User-Agent", "Mozilla Bar"));
|
new BasicHeader("User-Agent", "Mozilla Bar"))) {
|
||||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception {
|
public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception {
|
||||||
try (RestClient client = restClient()) {
|
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.getStatusLine().getStatusCode(), is(200));
|
||||||
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
assertThat(response.getFirstHeader("Access-Control-Allow-Origin"), nullValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatPreFlightRequestWorksOnMatch() throws Exception {
|
public void testThatPreFlightRequestWorksOnMatch() throws Exception {
|
||||||
String corsValue = "http://localhost:9200";
|
String corsValue = "http://localhost:9200";
|
||||||
try (RestClient client = restClient()) {
|
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("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);
|
assertResponseWithOriginheader(response, corsValue);
|
||||||
assertNotNull(response.getFirstHeader("Access-Control-Allow-Methods"));
|
assertNotNull(response.getFirstHeader("Access-Control-Allow-Methods"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,18 +42,20 @@ public class RestMainActionIT extends ESIntegTestCase {
|
||||||
|
|
||||||
public void testHeadRequest() throws IOException {
|
public void testHeadRequest() throws IOException {
|
||||||
try (RestClient client = restClient()) {
|
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));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
assertNull(response.getEntity());
|
assertNull(response.getEntity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetRequest() throws IOException {
|
public void testGetRequest() throws IOException {
|
||||||
try (RestClient client = restClient()) {
|
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));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
assertNotNull(response.getEntity());
|
assertNotNull(response.getEntity());
|
||||||
assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name"));
|
assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,15 +221,16 @@ public class ContextAndHeaderTransportIT extends ESIntegTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
try (RestClient client = restClient()) {
|
try (RestClient client = restClient()) {
|
||||||
ElasticsearchResponse response = client.performRequest("GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null,
|
try (ElasticsearchResponse response = client.performRequest("GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null,
|
||||||
new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue));
|
new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue))) {
|
||||||
assertThat(response, hasStatus(OK));
|
assertThat(response, hasStatus(OK));
|
||||||
List<RequestAndHeaders> searchRequests = getRequests(SearchRequest.class);
|
List<RequestAndHeaders> searchRequests = getRequests(SearchRequest.class);
|
||||||
assertThat(searchRequests, hasSize(greaterThan(0)));
|
assertThat(searchRequests, hasSize(greaterThan(0)));
|
||||||
for (RequestAndHeaders requestAndHeaders : searchRequests) {
|
for (RequestAndHeaders requestAndHeaders : searchRequests) {
|
||||||
assertThat(requestAndHeaders.headers.containsKey(relevantHeaderName), is(true));
|
assertThat(requestAndHeaders.headers.containsKey(relevantHeaderName), is(true));
|
||||||
// was not specified, thus is not included
|
// was not specified, thus is not included
|
||||||
assertThat(requestAndHeaders.headers.containsKey(randomHeaderKey), is(false));
|
assertThat(requestAndHeaders.headers.containsKey(randomHeaderKey), is(false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue