|
6 | 6 | import io.qameta.allure.attachment.DefaultAttachmentProcessor; |
7 | 7 | import io.qameta.allure.attachment.FreemarkerAttachmentRenderer; |
8 | 8 | import io.qameta.allure.attachment.http.HttpResponseAttachment; |
9 | | -import org.apache.http.HttpException; |
| 9 | +import org.apache.http.HttpEntity; |
10 | 10 | import org.apache.http.HttpResponse; |
11 | 11 | import org.apache.http.HttpResponseInterceptor; |
| 12 | +import org.apache.http.entity.HttpEntityWrapper; |
12 | 13 | import org.apache.http.protocol.HttpContext; |
| 14 | +import org.apache.http.util.EntityUtils; |
13 | 15 |
|
14 | | -import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.ByteArrayInputStream; |
15 | 17 | import java.io.IOException; |
| 18 | +import java.io.InputStream; |
16 | 19 | import java.nio.charset.StandardCharsets; |
17 | 20 | import java.util.stream.Stream; |
18 | 21 |
|
@@ -40,21 +43,42 @@ public AllureHttpClientResponse(final AttachmentRenderer<AttachmentData> rendere |
40 | 43 |
|
41 | 44 | @Override |
42 | 45 | public void process(final HttpResponse response, |
43 | | - final HttpContext context) throws HttpException, IOException { |
| 46 | + final HttpContext context) throws IOException { |
44 | 47 |
|
45 | 48 | final HttpResponseAttachment.Builder builder = create("Response") |
46 | 49 | .withResponseCode(response.getStatusLine().getStatusCode()); |
47 | 50 |
|
48 | 51 | Stream.of(response.getAllHeaders()) |
49 | 52 | .forEach(header -> builder.withHeader(header.getName(), header.getValue())); |
50 | 53 |
|
51 | | - final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
52 | | - response.getEntity().writeTo(os); |
| 54 | + final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity()); |
| 55 | + response.setEntity(loggableEntity); |
53 | 56 |
|
54 | | - final String body = new String(os.toByteArray(), StandardCharsets.UTF_8); |
55 | | - builder.withBody(body); |
| 57 | + builder.withBody(loggableEntity.getBody()); |
56 | 58 |
|
57 | 59 | final HttpResponseAttachment responseAttachment = builder.build(); |
58 | 60 | processor.addAttachment(responseAttachment, renderer); |
59 | 61 | } |
| 62 | + |
| 63 | + /** |
| 64 | + * Required to allow consume httpEntity twice. |
| 65 | + */ |
| 66 | + private static class LoggableEntity extends HttpEntityWrapper { |
| 67 | + |
| 68 | + private final byte[] rawContent; |
| 69 | + |
| 70 | + LoggableEntity(final HttpEntity wrappedEntity) throws IOException { |
| 71 | + super(wrappedEntity); |
| 72 | + rawContent = EntityUtils.toByteArray(wrappedEntity); |
| 73 | + } |
| 74 | + |
| 75 | + public String getBody() { |
| 76 | + return new String(rawContent, StandardCharsets.UTF_8); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public InputStream getContent() { |
| 81 | + return new ByteArrayInputStream(rawContent); |
| 82 | + } |
| 83 | + } |
60 | 84 | } |
0 commit comments