Search in sources :

Example 1 with NStringEntity

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

the class RequestLoggerTests method testTraceRequest.

public void testTraceRequest() throws IOException, URISyntaxException {
    HttpHost host = new HttpHost("localhost", 9200, randomBoolean() ? "http" : "https");
    String expectedEndpoint = "/index/type/_api";
    URI uri;
    if (randomBoolean()) {
        uri = new URI(expectedEndpoint);
    } else {
        uri = new URI("index/type/_api");
    }
    HttpUriRequest request = randomHttpRequest(uri);
    String expected = "curl -iX " + request.getMethod() + " '" + host + expectedEndpoint + "'";
    boolean hasBody = request instanceof HttpEntityEnclosingRequest && randomBoolean();
    String requestBody = "{ \"field\": \"value\" }";
    if (hasBody) {
        expected += " -d '" + requestBody + "'";
        HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity;
        switch(randomIntBetween(0, 4)) {
            case 0:
                entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
                break;
            case 1:
                entity = new InputStreamEntity(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)), ContentType.APPLICATION_JSON);
                break;
            case 2:
                entity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
                break;
            case 3:
                entity = new NByteArrayEntity(requestBody.getBytes(StandardCharsets.UTF_8), ContentType.APPLICATION_JSON);
                break;
            case 4:
                // Evil entity without a charset
                entity = new StringEntity(requestBody, ContentType.create("application/json", (Charset) null));
                break;
            default:
                throw new UnsupportedOperationException();
        }
        enclosingRequest.setEntity(entity);
    }
    String traceRequest = RequestLogger.buildTraceRequest(request, host);
    assertThat(traceRequest, equalTo(expected));
    if (hasBody) {
        //check that the body is still readable as most entities are not repeatable
        String body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(), StandardCharsets.UTF_8);
        assertThat(body, equalTo(requestBody));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) URI(java.net.URI) InputStreamEntity(org.apache.http.entity.InputStreamEntity) NStringEntity(org.apache.http.nio.entity.NStringEntity) StringEntity(org.apache.http.entity.StringEntity) NStringEntity(org.apache.http.nio.entity.NStringEntity) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 HttpHost (org.apache.http.HttpHost)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 StringEntity (org.apache.http.entity.StringEntity)1 NByteArrayEntity (org.apache.http.nio.entity.NByteArrayEntity)1 NStringEntity (org.apache.http.nio.entity.NStringEntity)1