diff --git a/allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java b/allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java index 0507beb86..f75891181 100644 --- a/allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java +++ b/allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java @@ -21,17 +21,13 @@ import io.qameta.allure.attachment.DefaultAttachmentProcessor; import io.qameta.allure.attachment.FreemarkerAttachmentRenderer; import io.qameta.allure.attachment.http.HttpResponseAttachment; -import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; -import org.apache.http.entity.HttpEntityWrapper; +import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.stream.Stream; import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create; @@ -67,10 +63,12 @@ public void process(final HttpResponse response, .forEach(header -> builder.setHeader(header.getName(), header.getValue())); if (response.getEntity() != null) { - final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity()); - response.setEntity(loggableEntity); + if (!response.getEntity().isRepeatable()) { + final BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(response.getEntity()); + response.setEntity(bufferedEntity); + } - builder.setBody(loggableEntity.getBody()); + builder.setBody(EntityUtils.toString(response.getEntity())); } else { builder.setBody("No body present"); } @@ -78,26 +76,4 @@ public void process(final HttpResponse response, final HttpResponseAttachment responseAttachment = builder.build(); processor.addAttachment(responseAttachment, renderer); } - - /** - * Required to allow consume httpEntity twice. - */ - private static class LoggableEntity extends HttpEntityWrapper { - - private final byte[] rawContent; - - LoggableEntity(final HttpEntity wrappedEntity) throws IOException { - super(wrappedEntity); - rawContent = EntityUtils.toByteArray(wrappedEntity); - } - - public String getBody() { - return new String(rawContent, StandardCharsets.UTF_8); - } - - @Override - public InputStream getContent() { - return new ByteArrayInputStream(rawContent); - } - } } diff --git a/allure-httpclient/src/test/java/io/qameta/allure/httpclient/AllureHttpClientTest.java b/allure-httpclient/src/test/java/io/qameta/allure/httpclient/AllureHttpClientTest.java index 366458e4a..d7e56ea95 100644 --- a/allure-httpclient/src/test/java/io/qameta/allure/httpclient/AllureHttpClientTest.java +++ b/allure-httpclient/src/test/java/io/qameta/allure/httpclient/AllureHttpClientTest.java @@ -22,6 +22,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; @@ -187,4 +188,23 @@ void shouldCreateRequestAttachmentWithEmptyBodyWhenNoContentIsReturned() throws .extracting("body") .containsNull(); } + + @Test + void shouldNotConsumeBody() throws Exception { + final AttachmentRenderer renderer = mock(AttachmentRenderer.class); + final AttachmentProcessor processor = mock(AttachmentProcessor.class); + + final HttpClientBuilder builder = HttpClientBuilder.create() + .addInterceptorLast(new AllureHttpClientResponse(renderer, processor)); + + try (CloseableHttpClient httpClient = builder.build()) { + final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port())); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + response.getStatusLine().getStatusCode(); + BufferedHttpEntity ent = new BufferedHttpEntity(response.getEntity()); + assertThat(EntityUtils.toString(ent)) + .isEqualTo(BODY_STRING); + } + } + } }