wrap entity only when not repeatable and improve RequestLoggerTests

This commit is contained in:
javanna 2016-06-06 15:41:11 +02:00 committed by Luca Cavanna
parent 1f7f6e2709
commit 2cf04c0877
2 changed files with 44 additions and 13 deletions

View File

@ -102,9 +102,12 @@ final class RequestLogger {
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
if (enclosingRequest.getEntity() != null) {
requestLine += " -d '";
HttpEntity entity = new BufferedHttpEntity(enclosingRequest.getEntity());
enclosingRequest.setEntity(entity);
requestLine += EntityUtils.toString(entity) + "'";
HttpEntity entity = enclosingRequest.getEntity();
if (entity.isRepeatable() == false) {
entity = new BufferedHttpEntity(enclosingRequest.getEntity());
enclosingRequest.setEntity(entity);
}
requestLine += EntityUtils.toString(entity, StandardCharsets.UTF_8) + "'";
}
}
return requestLine;
@ -121,7 +124,9 @@ final class RequestLogger {
responseLine += "\n#";
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
entity = new BufferedHttpEntity(entity);
if (entity.isRepeatable() == false) {
entity = new BufferedHttpEntity(entity);
}
httpResponse.setEntity(entity);
ContentType contentType = ContentType.get(entity);
Charset charset = StandardCharsets.UTF_8;

View File

@ -20,6 +20,7 @@
package org.elasticsearch.client;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.ProtocolVersion;
@ -30,11 +31,14 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -80,16 +84,27 @@ public class RequestLoggerTests extends LuceneTestCase {
}
String expected = "curl -iX " + request.getMethod() + " '" + host + uri + "'";
if (request instanceof HttpEntityEnclosingRequest && random().nextBoolean()) {
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
String requestBody = "{ \"field\": \"value\" }";
enclosingRequest.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
boolean hasBody = request instanceof HttpEntityEnclosingRequest && random().nextBoolean();
String requestBody = "{ \"field\": \"value\" }";
if (hasBody) {
expected += " -d '" + requestBody + "'";
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity;
if (random().nextBoolean()) {
entity = new StringEntity(requestBody, StandardCharsets.UTF_8);
} else {
entity = new InputStreamEntity(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)));
}
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));
}
}
public void testTraceResponse() throws IOException {
@ -105,15 +120,26 @@ public class RequestLoggerTests extends LuceneTestCase {
expected += "\n# header" + i + ": value";
}
expected += "\n#";
if (random().nextBoolean()) {
String responseBody = "{\n \"field\": \"value\"\n}";
httpResponse.setEntity(new StringEntity(responseBody, StandardCharsets.UTF_8));
boolean hasBody = random().nextBoolean();
String responseBody = "{\n \"field\": \"value\"\n}";
if (hasBody) {
expected += "\n# {";
expected += "\n# \"field\": \"value\"";
expected += "\n# }";
HttpEntity entity;
if (random().nextBoolean()) {
entity = new StringEntity(responseBody, StandardCharsets.UTF_8);
} else {
entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)));
}
httpResponse.setEntity(entity);
}
String traceResponse = RequestLogger.buildTraceResponse(httpResponse);
assertThat(traceResponse, equalTo(expected));
if (hasBody) {
//check that the body is still readable as most entities are not repeatable
String body = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8);
assertThat(body, equalTo(responseBody));
}
}
}