Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.qameta.allure.attachment.http.HttpRequestAttachment;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;
Expand All @@ -45,7 +44,7 @@ public class AllureHttpClientRequest implements HttpRequestInterceptor {

public AllureHttpClientRequest() {
this(new FreemarkerAttachmentRenderer("http-request.ftl"),
new DefaultAttachmentProcessor()
new DefaultAttachmentProcessor()
);
}

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

@Override
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
final HttpContext context) throws IOException {
final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri())
.setMethod(request.getRequestLine().getMethod());

Stream.of(request.getAllHeaders())
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));
.forEach(header -> builder.setHeader(header.getName(), header.getValue()));

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

final ByteArrayOutputStream os = new ByteArrayOutputStream();
entity.writeTo(os);
if (entity != null) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
entity.writeTo(os);

final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
builder.setBody(body);
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
builder.setBody(body);
}
}

final HttpRequestAttachment requestAttachment = builder.build();
processor.addAttachment(requestAttachment, renderer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.qameta.allure.attachment.AttachmentProcessor;
import io.qameta.allure.attachment.AttachmentRenderer;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -33,7 +34,9 @@

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.noContent;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
Expand Down Expand Up @@ -65,6 +68,9 @@ void setUp() {
stubFor(get(urlEqualTo("/empty"))
.willReturn(aResponse()
.withStatus(304)));

stubFor(delete(urlEqualTo("/hello"))
.willReturn(noContent()));
}

@AfterEach
Expand Down Expand Up @@ -155,4 +161,31 @@ void shouldCreateResponseAttachmentWithEmptyBody() throws Exception {
.extracting("body")
.containsExactly("No body present");
}

@SuppressWarnings("unchecked")
@Test
void shouldCreateRequestAttachmentWithEmptyBodyWhenNoContentIsReturned() throws Exception {
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
final AttachmentProcessor<AttachmentData> processor = mock(AttachmentProcessor.class);

final HttpClientBuilder builder = HttpClientBuilder.create()
.addInterceptorLast(new AllureHttpClientRequest(renderer, processor));

try (CloseableHttpClient httpClient = builder.build()) {
final HttpDelete httpDelete = new HttpDelete(String.format("http://localhost:%d/hello", server.port()));
try (CloseableHttpResponse response = httpClient.execute(httpDelete)) {
assertThat(response.getEntity())
.isEqualTo(null);
}
}

final ArgumentCaptor<AttachmentData> captor = ArgumentCaptor.forClass(AttachmentData.class);
verify(processor, times(1))
.addAttachment(captor.capture(), eq(renderer));

assertThat(captor.getAllValues())
.hasSize(1)
.extracting("body")
.containsNull();
}
}