Skip to content

Commit 979fdbc

Browse files
authored
fix npe in http client integration (via #454)
1 parent 9b990a3 commit 979fdbc

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.qameta.allure.attachment.http.HttpRequestAttachment;
2424
import org.apache.http.HttpEntity;
2525
import org.apache.http.HttpEntityEnclosingRequest;
26-
import org.apache.http.HttpException;
2726
import org.apache.http.HttpRequest;
2827
import org.apache.http.HttpRequestInterceptor;
2928
import org.apache.http.protocol.HttpContext;
@@ -45,7 +44,7 @@ public class AllureHttpClientRequest implements HttpRequestInterceptor {
4544

4645
public AllureHttpClientRequest() {
4746
this(new FreemarkerAttachmentRenderer("http-request.ftl"),
48-
new DefaultAttachmentProcessor()
47+
new DefaultAttachmentProcessor()
4948
);
5049
}
5150

@@ -57,23 +56,24 @@ public AllureHttpClientRequest(final AttachmentRenderer<AttachmentData> renderer
5756

5857
@Override
5958
public void process(final HttpRequest request,
60-
final HttpContext context) throws HttpException, IOException {
59+
final HttpContext context) throws IOException {
6160
final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri())
6261
.setMethod(request.getRequestLine().getMethod());
6362

6463
Stream.of(request.getAllHeaders())
65-
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));
64+
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));
6665

6766
if (request instanceof HttpEntityEnclosingRequest) {
6867
final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
6968

70-
final ByteArrayOutputStream os = new ByteArrayOutputStream();
71-
entity.writeTo(os);
69+
if (entity != null) {
70+
final ByteArrayOutputStream os = new ByteArrayOutputStream();
71+
entity.writeTo(os);
7272

73-
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
74-
builder.setBody(body);
73+
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
74+
builder.setBody(body);
75+
}
7576
}
76-
7777
final HttpRequestAttachment requestAttachment = builder.build();
7878
processor.addAttachment(requestAttachment, renderer);
7979
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.qameta.allure.attachment.AttachmentProcessor;
2121
import io.qameta.allure.attachment.AttachmentRenderer;
2222
import org.apache.http.client.methods.CloseableHttpResponse;
23+
import org.apache.http.client.methods.HttpDelete;
2324
import org.apache.http.client.methods.HttpGet;
2425
import org.apache.http.impl.client.CloseableHttpClient;
2526
import org.apache.http.impl.client.HttpClientBuilder;
@@ -33,7 +34,9 @@
3334

3435
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
3536
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
37+
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
3638
import static com.github.tomakehurst.wiremock.client.WireMock.get;
39+
import static com.github.tomakehurst.wiremock.client.WireMock.noContent;
3740
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
3841
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
3942
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -65,6 +68,9 @@ void setUp() {
6568
stubFor(get(urlEqualTo("/empty"))
6669
.willReturn(aResponse()
6770
.withStatus(304)));
71+
72+
stubFor(delete(urlEqualTo("/hello"))
73+
.willReturn(noContent()));
6874
}
6975

7076
@AfterEach
@@ -155,4 +161,31 @@ void shouldCreateResponseAttachmentWithEmptyBody() throws Exception {
155161
.extracting("body")
156162
.containsExactly("No body present");
157163
}
164+
165+
@SuppressWarnings("unchecked")
166+
@Test
167+
void shouldCreateRequestAttachmentWithEmptyBodyWhenNoContentIsReturned() throws Exception {
168+
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
169+
final AttachmentProcessor<AttachmentData> processor = mock(AttachmentProcessor.class);
170+
171+
final HttpClientBuilder builder = HttpClientBuilder.create()
172+
.addInterceptorLast(new AllureHttpClientRequest(renderer, processor));
173+
174+
try (CloseableHttpClient httpClient = builder.build()) {
175+
final HttpDelete httpDelete = new HttpDelete(String.format("http://localhost:%d/hello", server.port()));
176+
try (CloseableHttpResponse response = httpClient.execute(httpDelete)) {
177+
assertThat(response.getEntity())
178+
.isEqualTo(null);
179+
}
180+
}
181+
182+
final ArgumentCaptor<AttachmentData> captor = ArgumentCaptor.forClass(AttachmentData.class);
183+
verify(processor, times(1))
184+
.addAttachment(captor.capture(), eq(renderer));
185+
186+
assertThat(captor.getAllValues())
187+
.hasSize(1)
188+
.extracting("body")
189+
.containsNull();
190+
}
158191
}

0 commit comments

Comments
 (0)