Search in sources :

Example 1 with Response

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

the class HttpCompressionIT method testUncompressedResponseByDefault.

public void testUncompressedResponseByDefault() throws IOException {
    RestClient client = client();
    Response response = client.performRequest("GET", "/");
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertNull(response.getHeader(HttpHeaders.CONTENT_ENCODING));
    response = client.performRequest("POST", "/company/employees/1", Collections.emptyMap(), SAMPLE_DOCUMENT);
    assertEquals(201, response.getStatusLine().getStatusCode());
    assertNull(response.getHeader(HttpHeaders.CONTENT_ENCODING));
}
Also used : Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient)

Example 2 with Response

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

the class CorsNotSetIT method testThatOmittingCorsHeaderDoesNotReturnAnything.

public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws IOException {
    Response response = getRestClient().performRequest("GET", "/");
    assertThat(response.getStatusLine().getStatusCode(), is(200));
    assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
    assertThat(response.getHeader("Access-Control-Allow-Credentials"), nullValue());
}
Also used : Response(org.elasticsearch.client.Response)

Example 3 with Response

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

the class CorsRegexIT method testThatRegularExpressionReturnsForbiddenOnNonMatch.

public void testThatRegularExpressionReturnsForbiddenOnNonMatch() throws IOException {
    try {
        getRestClient().performRequest("GET", "/", new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", "http://evil-host:9200"));
        fail("request should have failed");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        // a rejected origin gets a FORBIDDEN - 403
        assertThat(response.getStatusLine().getStatusCode(), is(403));
        assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
    }
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 4 with Response

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

the class CorsRegexIT method testThatSendingNoOriginHeaderReturnsNoAccessControlHeader.

public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws IOException {
    Response response = getRestClient().performRequest("GET", "/", new BasicHeader("User-Agent", "Mozilla Bar"));
    assertThat(response.getStatusLine().getStatusCode(), is(200));
    assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
}
Also used : Response(org.elasticsearch.client.Response) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with Response

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

the class DeprecationHttpIT method testUniqueDeprecationResponsesMergedTogether.

/**
     * Attempts to do a scatter/gather request that expects unique responses per sub-request.
     */
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/19222")
public void testUniqueDeprecationResponsesMergedTogether() throws IOException {
    final String[] indices = new String[randomIntBetween(2, 5)];
    // add at least one document for each index
    for (int i = 0; i < indices.length; ++i) {
        indices[i] = "test" + i;
        // create indices with a single shard to reduce noise; the query only deprecates uniquely by index anyway
        assertTrue(prepareCreate(indices[i]).setSettings(Settings.builder().put("number_of_shards", 1)).get().isAcknowledged());
        int randomDocCount = randomIntBetween(1, 2);
        for (int j = 0; j < randomDocCount; ++j) {
            index(indices[i], "type", Integer.toString(j), "{\"field\":" + j + "}");
        }
    }
    refresh(indices);
    final String commaSeparatedIndices = Stream.of(indices).collect(Collectors.joining(","));
    final String body = "{\"query\":{\"bool\":{\"filter\":[{\"" + TestDeprecatedQueryBuilder.NAME + "\":{}}]}}}";
    // trigger all index deprecations
    Response response = getRestClient().performRequest("GET", "/" + commaSeparatedIndices + "/_search", Collections.emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(OK.getStatus()));
    final List<String> deprecatedWarnings = getWarningHeaders(response.getHeaders());
    final List<Matcher<String>> headerMatchers = new ArrayList<>(indices.length);
    for (String index : indices) {
        headerMatchers.add(containsString(LoggerMessageFormat.format("[{}] index", (Object) index)));
    }
    assertThat(deprecatedWarnings, hasSize(headerMatchers.size()));
    for (Matcher<String> headerMatcher : headerMatchers) {
        assertThat(deprecatedWarnings, hasItem(headerMatcher));
    }
}
Also used : Response(org.elasticsearch.client.Response) StringEntity(org.apache.http.entity.StringEntity) Matcher(org.hamcrest.Matcher) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

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