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

View File

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