Skip to content

Commit ba9314b

Browse files
CodeGeek32Alexander Ruchkov
authored andcommitted
limited prettifying length to 1mb
1 parent 5dc185e commit ba9314b

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.TreeSet;
3636

3737
import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;
38-
import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
3938
import static java.util.Optional.ofNullable;
4039

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

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

47+
private int maxAllowedPrettifyLength = 1048576;
48+
4849
private String requestTemplatePath = "http-request.ftl";
4950
private String responseTemplatePath = "http-response.ftl";
5051
private String requestAttachmentName = "Request";
5152
private String responseAttachmentName;
5253

54+
public AllureRestAssured setMaxAllowedPrettifyLength(final int maxAllowedPrettifyLength) {
55+
this.maxAllowedPrettifyLength = maxAllowedPrettifyLength;
56+
return this;
57+
}
58+
5359
public AllureRestAssured setRequestTemplate(final String templatePath) {
5460
this.requestTemplatePath = templatePath;
5561
return this;
@@ -123,10 +129,14 @@ public Response filter(final FilterableRequestSpecification requestSpec,
123129
final String attachmentName = ofNullable(responseAttachmentName)
124130
.orElse(response.getStatusLine());
125131

126-
final HttpResponseAttachment responseAttachment = create(attachmentName)
132+
final String responseAsString = response.getBody().asString();
133+
final String body = responseAsString.length() > maxAllowedPrettifyLength ?
134+
responseAsString : prettifier.getPrettifiedBodyIfPossible(response, response.getBody());
135+
136+
final HttpResponseAttachment responseAttachment = HttpResponseAttachment.Builder.create(attachmentName)
127137
.setResponseCode(response.getStatusCode())
128138
.setHeaders(toMapConverter(response.getHeaders(), hiddenHeaders))
129-
.setBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))
139+
.setBody(body)
130140
.build();
131141

132142
new DefaultAttachmentProcessor().addAttachment(

allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import io.restassured.config.RestAssuredConfig;
2929
import io.restassured.http.ContentType;
3030
import io.restassured.specification.RequestSpecification;
31+
3132
import java.nio.charset.StandardCharsets;
33+
3234
import org.junit.jupiter.api.Test;
3335
import org.junit.jupiter.api.extension.ExtensionContext;
3436
import org.junit.jupiter.params.ParameterizedTest;
@@ -59,6 +61,23 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
5961
}
6062
}
6163

64+
class JsonPrettifyingArgumentsProvider implements ArgumentsProvider {
65+
@Override
66+
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
67+
68+
return Stream.of(
69+
arguments(new AllureRestAssured(), """
70+
{
71+
&quot;name&quot;: &quot;12345&quot;,
72+
&quot;value&quot;: &quot;abcdef&quot;
73+
}"""),
74+
arguments(new AllureRestAssured().setMaxAllowedPrettifyLength(5), """
75+
{&quot;name&quot;:&quot;12345&quot;,&quot;value&quot;:&quot;abcdef&quot;}
76+
""")
77+
);
78+
}
79+
}
80+
6281
class HiddenHeadersArgumentProvider implements ArgumentsProvider {
6382
@Override
6483
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
@@ -174,6 +193,34 @@ void shouldHideHeadersInAttachments(final Map<String, String> headers,
174193
.allSatisfy(at -> expectedValues.forEach(ev -> assertThat(at).contains(ev)));
175194
}
176195

196+
@ParameterizedTest
197+
@ArgumentsSource(JsonPrettifyingArgumentsProvider.class)
198+
void responseJsonPrettified(final AllureRestAssured filter, final String formattedBody) {
199+
final ResponseDefinitionBuilder responseBuilder = WireMock.aResponse()
200+
.withHeader("Content-Type", "application/json")
201+
.withBody("""
202+
{"name":"12345","value":"abcdef"}
203+
""")
204+
.withStatus(200);
205+
206+
RestAssured.replaceFiltersWith(filter);
207+
208+
final AllureResults results = executeWithStub(responseBuilder, RestAssured.with());
209+
final Attachment requestAttachment = results.getTestResults()
210+
.stream()
211+
.map(TestResult::getAttachments)
212+
.flatMap(Collection::stream)
213+
.filter(attachment -> "HTTP/1.1 200 OK".equals(attachment.getName()))
214+
.findAny()
215+
.orElseThrow(() -> new AssertionError("No response attachment found"));
216+
217+
final byte[] attachmentBody = results.getAttachments().get(requestAttachment.getSource());
218+
final String attachmentBodyString = new String(attachmentBody, StandardCharsets.UTF_8);
219+
220+
assertThat(attachmentBodyString)
221+
.contains(formattedBody);
222+
}
223+
177224
protected final AllureResults execute() {
178225
return executeWithStub(WireMock.aResponse().withBody("some body"));
179226
}
@@ -193,8 +240,8 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
193240
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello?Allure=Form")).willReturn(responseBuilder));
194241
try {
195242
spec.contentType(ContentType.URLENC)
196-
.formParams("Allure", "Form")
197-
.get(server.url("/hello")).then().statusCode(statusCode);
243+
.formParams("Allure", "Form")
244+
.get(server.url("/hello")).then().statusCode(statusCode);
198245
} finally {
199246
server.stop();
200247
RestAssured.replaceFiltersWith(ImmutableList.of());

0 commit comments

Comments
 (0)