Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

limited prettifying length to 1mb #1157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
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
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.