Skip to content

Commit 26135e7

Browse files
useriqbaev
authored andcommitted
fix empty body request handling for allure-httpclient (via allure-framework#313)
1 parent 8e8fa64 commit 26135e7

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ public void process(final HttpResponse response,
5151
Stream.of(response.getAllHeaders())
5252
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));
5353

54-
final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity());
55-
response.setEntity(loggableEntity);
54+
if (response.getEntity() != null) {
55+
final LoggableEntity loggableEntity = new LoggableEntity(response.getEntity());
56+
response.setEntity(loggableEntity);
5657

57-
builder.setBody(loggableEntity.getBody());
58+
builder.setBody(loggableEntity.getBody());
59+
} else {
60+
builder.setBody("No body present");
61+
}
5862

5963
final HttpResponseAttachment responseAttachment = builder.build();
6064
processor.addAttachment(responseAttachment, renderer);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ void setUp() {
4646
stubFor(get(urlEqualTo("/hello"))
4747
.willReturn(aResponse()
4848
.withBody(BODY_STRING)));
49+
50+
stubFor(get(urlEqualTo("/empty"))
51+
.willReturn(aResponse()
52+
.withStatus(304)));
4953
}
5054

5155
@AfterEach
@@ -110,4 +114,30 @@ void shouldCreateResponseAttachment() throws Exception {
110114
.extracting("responseCode")
111115
.containsExactly(200);
112116
}
117+
118+
@Test
119+
void shouldCreateResponseAttachmentWithEmptyBody() throws Exception {
120+
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
121+
final AttachmentProcessor<AttachmentData> processor = mock(AttachmentProcessor.class);
122+
123+
final HttpClientBuilder builder = HttpClientBuilder.create()
124+
.addInterceptorLast(new AllureHttpClientResponse(renderer, processor));
125+
126+
try (CloseableHttpClient httpClient = builder.build()) {
127+
final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/empty", server.port()));
128+
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
129+
assertThat(response.getEntity())
130+
.isEqualTo(null);
131+
}
132+
}
133+
134+
final ArgumentCaptor<AttachmentData> captor = ArgumentCaptor.forClass(AttachmentData.class);
135+
verify(processor, times(1))
136+
.addAttachment(captor.capture(), eq(renderer));
137+
138+
assertThat(captor.getAllValues())
139+
.hasSize(1)
140+
.extracting("body")
141+
.containsExactly("No body present");
142+
}
113143
}

0 commit comments

Comments
 (0)