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 @@ -43,6 +43,8 @@
public class AllureRestAssured implements OrderedFilter {

private static final String HIDDEN_PLACEHOLDER = "[ BLACKLISTED ]";
private static final String REST_ASSURED_NO_PARAMETER_VALUE_CLASS =
"io.restassured.internal.NoParameterValue";

private int maxAllowedPrettifyLength = 1_048_576;

Expand Down Expand Up @@ -114,7 +116,7 @@ public Response filter(final FilterableRequestSpecification requestSpec,
}

if (Objects.nonNull(requestSpec.getFormParams())) {
requestAttachmentBuilder.setFormParams(requestSpec.getFormParams());
requestAttachmentBuilder.setFormParams(normalizeFormParams(requestSpec.getFormParams()));
}

final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
Expand Down Expand Up @@ -155,6 +157,20 @@ private static Map<String, String> toMapConverter(final Iterable<? extends NameA
return result;
}

private static Map<String, String> normalizeFormParams(final Map<?, ?> formParams) {
final Map<String, String> result = new HashMap<>();
formParams.forEach((key, value) -> {
if (value == null) {
return;
}
if (REST_ASSURED_NO_PARAMETER_VALUE_CLASS.equals(value.getClass().getName())) {
return;
}
result.put(String.valueOf(key), String.valueOf(value));
});
return result;
}

@Override
public int getOrder() {
return Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import java.util.Collection;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -249,4 +250,57 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
}
});
}

@Test
void shouldHandleNullFormParams() {
final AllureRestAssured filter = new AllureRestAssured();
RestAssured.replaceFiltersWith(filter);

final ResponseDefinitionBuilder responseBuilder = WireMock.aResponse().withStatus(200);
final AllureResults results = executeWithStub(responseBuilder, RestAssured.with().formParam("key", (String) null));

assertThat(results.getTestResults()).hasSize(1);
final Attachment requestAttachment = results.getTestResults().stream()
.map(TestResult::getAttachments)
.flatMap(Collection::stream)
.filter(attachment -> "Request".equals(attachment.getName()))
.findAny()
.orElseThrow(() -> new AssertionError("No request attachment found"));

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

assertThat(attachmentBodyString).doesNotContain("null");
}

@Test
void shouldHandleNoParameterValueFormParams() {
final AllureRestAssured filter = new AllureRestAssured();
RestAssured.replaceFiltersWith(filter);

final ResponseDefinitionBuilder responseBuilder = WireMock.aResponse().withStatus(200);
final Map<String, Object> formParams = new HashMap<>();
formParams.put("key", "value");
formParams.put("nullKey", null);
final AllureResults results = executeWithStub(responseBuilder,
RestAssured.with().contentType(ContentType.URLENC).formParams(formParams));

assertThat(results.getTestResults()).hasSize(1);
final Attachment requestAttachment = results.getTestResults().stream()
.map(TestResult::getAttachments)
.flatMap(Collection::stream)
.filter(attachment -> "Request".equals(attachment.getName()))
.findAny()
.orElseThrow(() -> new AssertionError("No request attachment found"));

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

assertThat(attachmentBodyString)
.contains("key")
.contains("value")
.doesNotContain("nullKey:")
.doesNotContain("NoParameterValue");

}
}
Loading