Search in sources :

Example 41 with Response

use of org.elasticsearch.client.Response in project elasticsearch by elastic.

the class CreatedLocationHeaderIT method locationTestCase.

private void locationTestCase(Response response) throws IOException {
    assertEquals(201, response.getStatusLine().getStatusCode());
    String location = response.getHeader("Location");
    assertThat(location, startsWith("/test/test/"));
    Response getResponse = client().performRequest("GET", location);
    assertEquals(singletonMap("test", "test"), entityAsMap(getResponse).get("_source"));
}
Also used : Response(org.elasticsearch.client.Response)

Example 42 with Response

use of org.elasticsearch.client.Response in project elasticsearch by elastic.

the class DetailedErrorsDisabledIT method testThatErrorTraceParamReturns400.

public void testThatErrorTraceParamReturns400() throws IOException {
    ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true")));
    Response response = e.getResponse();
    assertThat(response.getHeader("Content-Type"), is("application/json; charset=UTF-8"));
    assertThat(EntityUtils.toString(e.getResponse().getEntity()), containsString("\"error\":\"error traces in responses are disabled.\""));
    assertThat(response.getStatusLine().getStatusCode(), is(400));
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException)

Example 43 with Response

use of org.elasticsearch.client.Response in project elasticsearch by elastic.

the class HttpCompressionIT method testCompressesResponseIfRequested.

public void testCompressesResponseIfRequested() throws IOException {
    RestClient client = client();
    Response response = client.performRequest("GET", "/", new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING));
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(GZIP_ENCODING, response.getHeader(HttpHeaders.CONTENT_ENCODING));
}
Also used : Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient) BasicHeader(org.apache.http.message.BasicHeader)

Example 44 with Response

use of org.elasticsearch.client.Response in project elasticsearch by elastic.

the class ResponseHeaderPluginIT method testThatSettingHeadersWorks.

public void testThatSettingHeadersWorks() throws IOException {
    ensureGreen();
    try {
        getRestClient().performRequest("GET", "/_protected");
        fail("request should have failed");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        assertThat(response.getStatusLine().getStatusCode(), equalTo(401));
        assertThat(response.getHeader("Secret"), equalTo("required"));
    }
    Response authResponse = getRestClient().performRequest("GET", "/_protected", new BasicHeader("Secret", "password"));
    assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(authResponse.getHeader("Secret"), equalTo("granted"));
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 45 with Response

use of org.elasticsearch.client.Response in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatRelevantHttpHeadersBecomeRequestHeaders.

public void testThatRelevantHttpHeadersBecomeRequestHeaders() throws IOException {
    final String IRRELEVANT_HEADER = "SomeIrrelevantHeader";
    Response response = getRestClient().performRequest("GET", "/" + queryIndex + "/_search", new BasicHeader(CUSTOM_HEADER, randomHeaderValue), new BasicHeader(IRRELEVANT_HEADER, randomHeaderValue));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    List<RequestAndHeaders> searchRequests = getRequests(SearchRequest.class);
    assertThat(searchRequests, hasSize(greaterThan(0)));
    for (RequestAndHeaders requestAndHeaders : searchRequests) {
        assertThat(requestAndHeaders.headers.containsKey(CUSTOM_HEADER), is(true));
        // was not specified, thus is not included
        assertThat(requestAndHeaders.headers.containsKey(IRRELEVANT_HEADER), is(false));
    }
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(org.elasticsearch.client.Response) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

Response (org.elasticsearch.client.Response)75 IOException (java.io.IOException)24 Request (org.elasticsearch.client.Request)19 BasicHeader (org.apache.http.message.BasicHeader)14 NStringEntity (org.apache.http.nio.entity.NStringEntity)14 HttpEntity (org.apache.http.HttpEntity)13 ResponseException (org.elasticsearch.client.ResponseException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 HashMap (java.util.HashMap)9 RestClient (org.elasticsearch.client.RestClient)8 Map (java.util.Map)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7 ArrayList (java.util.ArrayList)6 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)6 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 ParseException (org.json.simple.parser.ParseException)6 InputStream (java.io.InputStream)5 StringEntity (org.apache.http.entity.StringEntity)5