From 857312a64d292b4ce7833281b95a813fbbe07bd5 Mon Sep 17 00:00:00 2001 From: Alexander Ruchkov Date: Tue, 15 Apr 2025 16:55:51 +0300 Subject: [PATCH] limited prettifying length to 1mb --- .../allure/restassured/AllureRestAssured.java | 16 ++++-- .../restassured/AllureRestAssuredTest.java | 51 ++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java b/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java index 9e166c30..9b35c9d4 100644 --- a/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java +++ b/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java @@ -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; /** @@ -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; @@ -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( diff --git a/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java b/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java index 61e2b4e8..4b3b2092 100644 --- a/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java +++ b/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java @@ -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; @@ -59,6 +61,23 @@ public Stream provideArguments(ExtensionContext context) { } } +class JsonPrettifyingArgumentsProvider implements ArgumentsProvider { + @Override + public Stream provideArguments(ExtensionContext context) { + + return Stream.of( + arguments(new AllureRestAssured(), """ + { + "name": "12345", + "value": "abcdef" + }"""), + arguments(new AllureRestAssured().setMaxAllowedPrettifyLength(5), """ + {"name":"12345","value":"abcdef"} + """) + ); + } +} + class HiddenHeadersArgumentProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext context) { @@ -174,6 +193,34 @@ void shouldHideHeadersInAttachments(final Map 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")); } @@ -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());