Skip to content

Commit 03bc59f

Browse files
authored
don't consume http responses (via allure-framework#502)
1 parent 16a2c80 commit 03bc59f

2 files changed

Lines changed: 26 additions & 30 deletions

File tree

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@
2121
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
2222
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
2323
import io.qameta.allure.attachment.http.HttpResponseAttachment;
24-
import org.apache.http.HttpEntity;
2524
import org.apache.http.HttpResponse;
2625
import org.apache.http.HttpResponseInterceptor;
27-
import org.apache.http.entity.HttpEntityWrapper;
26+
import org.apache.http.entity.BufferedHttpEntity;
2827
import org.apache.http.protocol.HttpContext;
2928
import org.apache.http.util.EntityUtils;
3029

31-
import java.io.ByteArrayInputStream;
3230
import java.io.IOException;
33-
import java.io.InputStream;
34-
import java.nio.charset.StandardCharsets;
3531
import java.util.stream.Stream;
3632

3733
import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
@@ -67,37 +63,17 @@ public void process(final HttpResponse response,
6763
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));
6864

6965
if (response.getEntity() != null) {
70-
final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity());
71-
response.setEntity(loggableEntity);
66+
if (!response.getEntity().isRepeatable()) {
67+
final BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(response.getEntity());
68+
response.setEntity(bufferedEntity);
69+
}
7270

73-
builder.setBody(loggableEntity.getBody());
71+
builder.setBody(EntityUtils.toString(response.getEntity()));
7472
} else {
7573
builder.setBody("No body present");
7674
}
7775

7876
final HttpResponseAttachment responseAttachment = builder.build();
7977
processor.addAttachment(responseAttachment, renderer);
8078
}
81-
82-
/**
83-
* Required to allow consume httpEntity twice.
84-
*/
85-
private static class LoggableEntity extends HttpEntityWrapper {
86-
87-
private final byte[] rawContent;
88-
89-
LoggableEntity(final HttpEntity wrappedEntity) throws IOException {
90-
super(wrappedEntity);
91-
rawContent = EntityUtils.toByteArray(wrappedEntity);
92-
}
93-
94-
public String getBody() {
95-
return new String(rawContent, StandardCharsets.UTF_8);
96-
}
97-
98-
@Override
99-
public InputStream getContent() {
100-
return new ByteArrayInputStream(rawContent);
101-
}
102-
}
10379
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.http.client.methods.CloseableHttpResponse;
2323
import org.apache.http.client.methods.HttpDelete;
2424
import org.apache.http.client.methods.HttpGet;
25+
import org.apache.http.entity.BufferedHttpEntity;
2526
import org.apache.http.impl.client.CloseableHttpClient;
2627
import org.apache.http.impl.client.HttpClientBuilder;
2728
import org.apache.http.util.EntityUtils;
@@ -187,4 +188,23 @@ void shouldCreateRequestAttachmentWithEmptyBodyWhenNoContentIsReturned() throws
187188
.extracting("body")
188189
.containsNull();
189190
}
191+
192+
@Test
193+
void shouldNotConsumeBody() throws Exception {
194+
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
195+
final AttachmentProcessor<AttachmentData> processor = mock(AttachmentProcessor.class);
196+
197+
final HttpClientBuilder builder = HttpClientBuilder.create()
198+
.addInterceptorLast(new AllureHttpClientResponse(renderer, processor));
199+
200+
try (CloseableHttpClient httpClient = builder.build()) {
201+
final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port()));
202+
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
203+
response.getStatusLine().getStatusCode();
204+
BufferedHttpEntity ent = new BufferedHttpEntity(response.getEntity());
205+
assertThat(EntityUtils.toString(ent))
206+
.isEqualTo(BODY_STRING);
207+
}
208+
}
209+
}
190210
}

0 commit comments

Comments
 (0)