Search in sources :

Example 1 with StringEntity

use of org.apache.http.entity.StringEntity 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)

Example 2 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class WaitForRefreshAndCloseTests method testUpdateAndThenClose.

public void testUpdateAndThenClose() throws Exception {
    client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
    closeWhileListenerEngaged(start("POST", "/_update", new StringEntity("{\"doc\":{\"name\":\"test\"}}", ContentType.APPLICATION_JSON)));
}
Also used : StringEntity(org.apache.http.entity.StringEntity)

Example 3 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class WaitForRefreshAndCloseTests method testDeleteAndThenClose.

public void testDeleteAndThenClose() throws Exception {
    client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
    closeWhileListenerEngaged(start("DELETE", "", null));
}
Also used : StringEntity(org.apache.http.entity.StringEntity)

Example 4 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class RemoteScrollableHitSourceTests method testWrapExceptionToPreserveStatus.

public void testWrapExceptionToPreserveStatus() throws IOException {
    Exception cause = new Exception();
    // Successfully get the status without a body
    RestStatus status = randomFrom(RestStatus.values());
    ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("No error body.", wrapped.getMessage());
    // Successfully get the status without a body
    HttpEntity okEntity = new StringEntity("test body", ContentType.TEXT_PLAIN);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), okEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("body=test body", wrapped.getMessage());
    // Successfully get the status with a broken body
    IOException badEntityException = new IOException();
    HttpEntity badEntity = mock(HttpEntity.class);
    when(badEntity.getContent()).thenThrow(badEntityException);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), badEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
    // Fail to get the status without a body
    int notAnHttpStatus = -1;
    assertNull(RestStatus.fromCode(notAnHttpStatus));
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());
    // Fail to get the status without a body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());
    // Fail to get the status with a broken body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RestStatus(org.elasticsearch.rest.RestStatus) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 5 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class Netty4HeadBodyIsEmptyIT method testGetSourceAction.

public void testGetSourceAction() throws IOException {
    createTestDoc();
    headTestCase("/test/test/1/_source", emptyMap(), greaterThan(0));
    headTestCase("/test/test/2/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
    try (XContentBuilder builder = jsonBuilder()) {
        builder.startObject();
        {
            builder.startObject("mappings");
            {
                builder.startObject("test-no-source");
                {
                    builder.startObject("_source");
                    {
                        builder.field("enabled", false);
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        client().performRequest("PUT", "/test-no-source", emptyMap(), new StringEntity(builder.string(), ContentType.APPLICATION_JSON));
        createTestDoc("test-no-source", "test-no-source");
        headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

StringEntity (org.apache.http.entity.StringEntity)1008 HttpPost (org.apache.http.client.methods.HttpPost)527 HttpResponse (org.apache.http.HttpResponse)349 IOException (java.io.IOException)271 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)246 Test (org.junit.Test)188 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)180 HttpPut (org.apache.http.client.methods.HttpPut)177 HttpEntity (org.apache.http.HttpEntity)146 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)91 HttpClient (org.apache.http.client.HttpClient)77 JsonNode (com.fasterxml.jackson.databind.JsonNode)60 File (java.io.File)54 Header (org.apache.http.Header)53 HttpGet (org.apache.http.client.methods.HttpGet)53 URI (java.net.URI)52 Deployment (org.activiti.engine.test.Deployment)50 UnsupportedEncodingException (java.io.UnsupportedEncodingException)47 HttpHost (org.apache.http.HttpHost)46 JSONObject (com.alibaba.fastjson.JSONObject)45