Skip to content

Commit

Permalink
Removed configuration option - always follow RestAssured.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfilichkin committed Mar 19, 2024
1 parent 43f81c4 commit 5542bb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@
*/
public class AllureRestAssured implements OrderedFilter {

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

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

private boolean followHeadersBlacklist = true;

public AllureRestAssured setRequestTemplate(final String templatePath) {
this.requestTemplatePath = templatePath;
return this;
Expand All @@ -72,16 +70,6 @@ public AllureRestAssured setResponseAttachmentName(final String responseAttachme
return this;
}

/**
* Configure filter to consider headers that are blacklisted by RestAssured LogConfig. Enabled by default.
* @see io.restassured.config.LogConfig#blacklistHeader
* @see io.restassured.config.LogConfig#blacklistHeaders
*/
public AllureRestAssured followHeadersBlacklist(final boolean isEnabled) {
this.followHeadersBlacklist = isEnabled;
return this;
}

/**
* @deprecated use {@link #setRequestTemplate(String)} instead.
* Scheduled for removal in 3.0 release.
Expand All @@ -107,14 +95,12 @@ public Response filter(final FilterableRequestSpecification requestSpec,
final Prettifier prettifier = new Prettifier();
final String url = requestSpec.getURI();

final Set<String> blacklistedHeaders = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
if (followHeadersBlacklist) {
blacklistedHeaders.addAll(Objects.requireNonNull(requestSpec.getConfig().getLogConfig().blacklistedHeaders()));
}
final Set<String> hiddenHeaders = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
hiddenHeaders.addAll(Objects.requireNonNull(requestSpec.getConfig().getLogConfig().blacklistedHeaders()));

final HttpRequestAttachment.Builder requestAttachmentBuilder = create(requestAttachmentName, url)
.setMethod(requestSpec.getMethod())
.setHeaders(toMapConverter(requestSpec.getHeaders(), blacklistedHeaders))
.setHeaders(toMapConverter(requestSpec.getHeaders(), hiddenHeaders))
.setCookies(toMapConverter(requestSpec.getCookies(), new HashSet<>()));

if (Objects.nonNull(requestSpec.getBody())) {
Expand All @@ -139,7 +125,7 @@ public Response filter(final FilterableRequestSpecification requestSpec,

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

Expand All @@ -151,9 +137,9 @@ public Response filter(final FilterableRequestSpecification requestSpec,
return response;
}

private static Map<String, String> toMapConverter(final Iterable<? extends NameAndValue> items, final Set<String> toBlacklist) {
private static Map<String, String> toMapConverter(final Iterable<? extends NameAndValue> items, final Set<String> toHide) {
final Map<String, String> result = new HashMap<>();
items.forEach(h -> result.put(h.getName(), toBlacklist.contains(h.getName()) ? BLACKLISTED : h.getValue()));
items.forEach(h -> result.put(h.getName(), toHide.contains(h.getName()) ? HIDDEN_PLACEHOLDER : h.getValue()));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package io.qameta.allure.restassured;

import static io.qameta.allure.test.RunUtils.runWithinTestContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
Expand All @@ -24,29 +28,23 @@
import io.qameta.allure.model.TestResult;
import io.qameta.allure.test.AllureResults;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.config.LogConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.specification.RequestSender;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qameta.allure.test.RunUtils.runWithinTestContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class AttachmentArgumentProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
Expand All @@ -60,20 +58,20 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
}
}

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

final String blacklistedHeader = "Authorization";
final String hiddenHeader = "Authorization";
final String header = "Accept";
final String headerValue = "value";

final Map<String, String> headers = Map.of(blacklistedHeader, headerValue, header, headerValue);
final Map<String, String> headers = Map.of(hiddenHeader, headerValue, header, headerValue);
final List<String> expectedHeaders = List.of(hiddenHeader + ": [ BLACKLISTED ]", header + ": " + headerValue);

return Stream.of(
arguments(headers, blacklistedHeader, List.of(blacklistedHeader + ": " + headerValue, header + ": " + headerValue), new AllureRestAssured().followHeadersBlacklist(false)),
arguments(headers, blacklistedHeader, List.of(blacklistedHeader + ": [ BLACKLISTED ]", header + ": " + headerValue), new AllureRestAssured()),
arguments(headers, blacklistedHeader.toUpperCase(), List.of(blacklistedHeader + ": [ BLACKLISTED ]", header + ": " + headerValue), new AllureRestAssured())
arguments(headers, hiddenHeader, expectedHeaders, new AllureRestAssured()),
arguments(headers, hiddenHeader.toUpperCase(), expectedHeaders, new AllureRestAssured())
);
}
}
Expand Down Expand Up @@ -155,12 +153,14 @@ void shouldCatchAttachmentBody(final List<String> attachmentNames, final AllureR
}

@ParameterizedTest
@ArgumentsSource(BlacklistHeadersArgumentProvider.class)
void shouldBlacklistHeadersInAttachments(final Map<String, String> headers, final String blacklistedHeader, final List<String> expectedValues, AllureRestAssured filter) {
@ArgumentsSource(HiddenHeadersArgumentProvider.class)
void shouldHideHeadersInAttachments(
final Map<String, String> headers, final String hiddenHeader, final List<String> expectedValues, AllureRestAssured filter) {

final ResponseDefinitionBuilder responseBuilder = WireMock.aResponse().withStatus(200);
headers.forEach(responseBuilder::withHeader);

RestAssured.config = new RestAssuredConfig().logConfig(LogConfig.logConfig().blacklistHeaders(List.of(blacklistedHeader)));
RestAssured.config = new RestAssuredConfig().logConfig(LogConfig.logConfig().blacklistHeaders(List.of(hiddenHeader)));
RestAssured.replaceFiltersWith(filter);

final AllureResults results = executeWithStub(responseBuilder, RestAssured.with().headers(headers));
Expand Down

0 comments on commit 5542bb9

Please sign in to comment.