Skip to content

Commit 65a81cf

Browse files
proton72baev
authored andcommitted
fix closed streams in apache http client integration (via #218)
1 parent 12398e6 commit 65a81cf

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
77
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
88
import io.qameta.allure.attachment.http.HttpResponseAttachment;
9-
import org.apache.http.HttpException;
9+
import org.apache.http.HttpEntity;
1010
import org.apache.http.HttpResponse;
1111
import org.apache.http.HttpResponseInterceptor;
12+
import org.apache.http.entity.HttpEntityWrapper;
1213
import org.apache.http.protocol.HttpContext;
14+
import org.apache.http.util.EntityUtils;
1315

14-
import java.io.ByteArrayOutputStream;
16+
import java.io.ByteArrayInputStream;
1517
import java.io.IOException;
18+
import java.io.InputStream;
1619
import java.nio.charset.StandardCharsets;
1720
import java.util.stream.Stream;
1821

@@ -40,21 +43,42 @@ public AllureHttpClientResponse(final AttachmentRenderer<AttachmentData> rendere
4043

4144
@Override
4245
public void process(final HttpResponse response,
43-
final HttpContext context) throws HttpException, IOException {
46+
final HttpContext context) throws IOException {
4447

4548
final HttpResponseAttachment.Builder builder = create("Response")
4649
.withResponseCode(response.getStatusLine().getStatusCode());
4750

4851
Stream.of(response.getAllHeaders())
4952
.forEach(header -> builder.withHeader(header.getName(), header.getValue()));
5053

51-
final ByteArrayOutputStream os = new ByteArrayOutputStream();
52-
response.getEntity().writeTo(os);
54+
final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity());
55+
response.setEntity(loggableEntity);
5356

54-
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
55-
builder.withBody(body);
57+
builder.withBody(loggableEntity.getBody());
5658

5759
final HttpResponseAttachment responseAttachment = builder.build();
5860
processor.addAttachment(responseAttachment, renderer);
5961
}
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+
}
6084
}

allure-httpclient/src/test/java/io/qameta/allure/httpclient/AllureHttpClientTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.http.client.methods.HttpGet;
99
import org.apache.http.impl.client.CloseableHttpClient;
1010
import org.apache.http.impl.client.HttpClientBuilder;
11+
import org.apache.http.util.EntityUtils;
1112
import org.junit.After;
1213
import org.junit.Before;
1314
import org.junit.Test;
@@ -32,6 +33,8 @@
3233
*/
3334
public class AllureHttpClientTest {
3435

36+
private static final String BODY_STRING = "Hello world!";
37+
3538
private WireMockServer server;
3639

3740
@Before
@@ -42,7 +45,7 @@ public void setUp() {
4245

4346
stubFor(get(urlEqualTo("/hello"))
4447
.willReturn(aResponse()
45-
.withBody("Hello world!")));
48+
.withBody(BODY_STRING)));
4649
}
4750

4851
@SuppressWarnings("unchecked")
@@ -58,6 +61,8 @@ public void shouldCreateRequestAttachment() throws Exception {
5861
final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port()));
5962
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
6063
response.getStatusLine().getStatusCode();
64+
assertThat(EntityUtils.toString(response.getEntity()))
65+
.isEqualTo(BODY_STRING);
6166
}
6267
}
6368

@@ -84,6 +89,8 @@ public void shouldCreateResponseAttachment() throws Exception {
8489
final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port()));
8590
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
8691
response.getStatusLine().getStatusCode();
92+
assertThat(EntityUtils.toString(response.getEntity()))
93+
.isEqualTo(BODY_STRING);
8794
}
8895
}
8996

0 commit comments

Comments
 (0)