Skip to content
Open
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 @@ -35,7 +35,6 @@
import java.util.TreeSet;

import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;
import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
import static java.util.Optional.ofNullable;

/**
Expand All @@ -45,11 +44,18 @@ public class AllureRestAssured implements OrderedFilter {

private static final String HIDDEN_PLACEHOLDER = "[ BLACKLISTED ]";

private int maxAllowedPrettifyLength = 1048576;

private String requestTemplatePath = "http-request.ftl";
private String responseTemplatePath = "http-response.ftl";
private String requestAttachmentName = "Request";
private String responseAttachmentName;

public AllureRestAssured setMaxAllowedPrettifyLength(final int maxAllowedPrettifyLength) {
this.maxAllowedPrettifyLength = maxAllowedPrettifyLength;
return this;
}

public AllureRestAssured setRequestTemplate(final String templatePath) {
this.requestTemplatePath = templatePath;
return this;
Expand Down Expand Up @@ -123,10 +129,14 @@ public Response filter(final FilterableRequestSpecification requestSpec,
final String attachmentName = ofNullable(responseAttachmentName)
.orElse(response.getStatusLine());

final HttpResponseAttachment responseAttachment = create(attachmentName)
final String responseAsString = response.getBody().asString();
final String body = responseAsString.length() > maxAllowedPrettifyLength ?
responseAsString : prettifier.getPrettifiedBodyIfPossible(response, response.getBody());

final HttpResponseAttachment responseAttachment = HttpResponseAttachment.Builder.create(attachmentName)
.setResponseCode(response.getStatusCode())
.setHeaders(toMapConverter(response.getHeaders(), hiddenHeaders))
.setBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))
.setBody(body)
.build();

new DefaultAttachmentProcessor().addAttachment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -59,6 +61,23 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
}
}

class JsonPrettifyingArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {

return Stream.of(
arguments(new AllureRestAssured(), """
{
&quot;name&quot;: &quot;12345&quot;,
&quot;value&quot;: &quot;abcdef&quot;
}"""),
arguments(new AllureRestAssured().setMaxAllowedPrettifyLength(5), """
{&quot;name&quot;:&quot;12345&quot;,&quot;value&quot;:&quot;abcdef&quot;}
""")
);
}
}

class HiddenHeadersArgumentProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
Expand Down Expand Up @@ -174,6 +193,34 @@ void shouldHideHeadersInAttachments(final Map<String, String> headers,
.allSatisfy(at -> expectedValues.forEach(ev -> assertThat(at).contains(ev)));
}

@ParameterizedTest
@ArgumentsSource(JsonPrettifyingArgumentsProvider.class)
void responseJsonPrettified(final AllureRestAssured filter, final String formattedBody) {
final ResponseDefinitionBuilder responseBuilder = WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody("""
{"name":"12345","value":"abcdef"}
""")
.withStatus(200);

RestAssured.replaceFiltersWith(filter);

final AllureResults results = executeWithStub(responseBuilder, RestAssured.with());
final Attachment requestAttachment = results.getTestResults()
.stream()
.map(TestResult::getAttachments)
.flatMap(Collection::stream)
.filter(attachment -> "HTTP/1.1 200 OK".equals(attachment.getName()))
.findAny()
.orElseThrow(() -> new AssertionError("No response attachment found"));

final byte[] attachmentBody = results.getAttachments().get(requestAttachment.getSource());
final String attachmentBodyString = new String(attachmentBody, StandardCharsets.UTF_8);

assertThat(attachmentBodyString)
.contains(formattedBody);
}

protected final AllureResults execute() {
return executeWithStub(WireMock.aResponse().withBody("some body"));
}
Expand All @@ -193,8 +240,8 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello?Allure=Form")).willReturn(responseBuilder));
try {
spec.contentType(ContentType.URLENC)
.formParams("Allure", "Form")
.get(server.url("/hello")).then().statusCode(statusCode);
.formParams("Allure", "Form")
.get(server.url("/hello")).then().statusCode(statusCode);
} finally {
server.stop();
RestAssured.replaceFiltersWith(ImmutableList.of());
Expand Down