Skip to content

Commit

Permalink
Also hiden blacklisted response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
johanhaleby committed Jan 17, 2020
1 parent aff2a28 commit d09a101
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
Expand Up @@ -1421,7 +1421,7 @@ public void logsMultiPartParamsOnLogAll() throws Exception {
}

@Test public void
its_possible_to_hide_headers_when_blacklist_is_defined_using_a_request_spec_builder() {
its_possible_to_hide_request_headers_when_blacklist_is_defined_using_a_request_spec_builder() {
final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
final RequestSpecification spec = new RequestSpecBuilder()
Expand Down Expand Up @@ -1455,7 +1455,7 @@ public void logsMultiPartParamsOnLogAll() throws Exception {
}

@Test public void
its_possible_to_hide_headers_when_blacklist_is_defined_in_log_config_from_request_specification() {
its_possible_to_hide_request_headers_when_blacklist_is_defined_in_log_config_from_request_specification() {
final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

Expand All @@ -1482,4 +1482,24 @@ public void logsMultiPartParamsOnLogAll() throws Exception {
"Multiparts:\t\t<none>%n" +
"Body:\t\t\t<none>%n")));
}
}

@Test public void
its_possible_to_hide_response_headers_when_blacklist_is_defined_in_log_config_from_request_specification() {
final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

given().
config(config().logConfig(logConfig().defaultStream(captor).blacklistHeader("MultiHeader"))).
when().
get("/multiValueHeader").
then().
log().all();

assertThat(writer.toString(), equalTo(String.format("HTTP/1.1 200 OK%n" +
"Content-Type: text/plain;charset=utf-8%n" +
"MultiHeader: [ BLACKLISTED ]%n" +
"MultiHeader: [ BLACKLISTED ]%n" +
"Content-Length: 0%n" +
"Server: Jetty(9.3.2.v20150730)%n")));
}
}
Expand Up @@ -28,6 +28,9 @@
import org.hamcrest.Matcher;

import java.io.PrintStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static io.restassured.RestAssured.config;

Expand All @@ -37,6 +40,7 @@ class StatusCodeBasedLoggingFilter implements Filter {
private final Matcher<?> matcher;
private final LogDetail logDetail;
private final boolean shouldPrettyPrint;
private final Set<String> blacklistedHeaders;

/**
* Log to system out
Expand Down Expand Up @@ -77,23 +81,38 @@ public StatusCodeBasedLoggingFilter(LogDetail logDetail, PrintStream stream, Mat
* @param matcher The matcher for the logging to take place
*/
public StatusCodeBasedLoggingFilter(LogDetail logDetail, boolean prettyPrint, PrintStream stream, Matcher<? super Integer> matcher) {
this(logDetail, prettyPrint, stream, matcher, Collections.emptySet());
}


/**
* Instantiate a logger using a specific print stream and a specific log detail and the option to pretty printing
*
* @param logDetail The log detail
* @param prettyPrint Enabled pretty printing if possible
* @param stream The stream to log errors to.
* @param matcher The matcher for the logging to take place
*/
public StatusCodeBasedLoggingFilter(LogDetail logDetail, boolean prettyPrint, PrintStream stream, Matcher<? super Integer> matcher, Set<String> blacklistedHeaders) {
Validate.notNull(logDetail, "Log details cannot be null");
Validate.notNull(stream, "Print stream cannot be null");
Validate.notNull(matcher, "Matcher cannot be null");
Validate.notNull(blacklistedHeaders, "Blacklisted headers cannot be null");
if (logDetail == LogDetail.PARAMS || logDetail == LogDetail.URI || logDetail == LogDetail.METHOD) {
throw new IllegalArgumentException(String.format("%s is not a valid %s for a response.", logDetail, LogDetail.class.getSimpleName()));
}
this.shouldPrettyPrint = prettyPrint;
this.logDetail = logDetail;
this.stream = stream;
this.matcher = matcher;
this.blacklistedHeaders = new HashSet<>(blacklistedHeaders);
}

public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
Response response = ctx.next(requestSpec, responseSpec);
final int statusCode = response.statusCode();
if (matcher.matches(statusCode)) {
ResponsePrinter.print(response, response, stream, logDetail, shouldPrettyPrint);
ResponsePrinter.print(response, response, stream, logDetail, shouldPrettyPrint, blacklistedHeaders);
final byte[] responseBody;
if (logDetail == LogDetail.BODY || logDetail == LogDetail.ALL) {
responseBody = response.asByteArray();
Expand Down
Expand Up @@ -397,7 +397,7 @@ private T logResponse(LogDetail logDetail, boolean shouldPrettyPrint) {
}

private T logResponse(LogDetail logDetail, boolean shouldPrettyPrint, PrintStream printStream) {
ResponsePrinter.print(response, response, printStream, logDetail, shouldPrettyPrint);
ResponsePrinter.print(response, response, printStream, logDetail, shouldPrettyPrint, config.getLogConfig().blacklistedHeaders());
return (T) this;
}

Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.SystemUtils;

import java.io.PrintStream;
import java.util.Set;

import static io.restassured.filter.log.LogDetail.*;
import static org.apache.commons.lang3.StringUtils.isBlank;
Expand All @@ -34,22 +35,23 @@
*/
public class ResponsePrinter {

private static final String BLACKLISTED = "[ BLACKLISTED ]";
private static final String HEADER_NAME_AND_VALUE_SEPARATOR = ": ";

/**
* Prints the response to the print stream
*
* @return A string of representing the response
*/
public static String print(ResponseOptions responseOptions, ResponseBody responseBody, PrintStream stream, LogDetail logDetail, boolean shouldPrettyPrint) {
public static String print(ResponseOptions responseOptions, ResponseBody responseBody, PrintStream stream, LogDetail logDetail, boolean shouldPrettyPrint, Set<String> blacklistedHeaders) {
final StringBuilder builder = new StringBuilder();
if (logDetail == ALL || logDetail == STATUS) {
builder.append(responseOptions.statusLine());
}
if (logDetail == ALL || logDetail == HEADERS) {
final Headers headers = responseOptions.headers();
if (headers.exist()) {
appendNewLineIfAll(logDetail, builder).append(toString(headers));
appendNewLineIfAll(logDetail, builder).append(toString(headers, blacklistedHeaders));
}
} else if (logDetail == COOKIES) {
final Cookies cookies = responseOptions.detailedCookies();
Expand All @@ -75,16 +77,21 @@ public static String print(ResponseOptions responseOptions, ResponseBody respons
return response;
}

private static String toString(Headers headers) {
private static String toString(Headers headers, Set<String> blacklistedHeaders) {
if (!headers.exist()) {
return "";
}

final StringBuilder builder = new StringBuilder();
for (Header header : headers) {
builder.append(header.getName())
.append(HEADER_NAME_AND_VALUE_SEPARATOR)
.append(header.getValue())
StringBuilder headerStringBuilder = builder.append(header.getName())
.append(HEADER_NAME_AND_VALUE_SEPARATOR);
if (blacklistedHeaders.contains(header.getName())) {
headerStringBuilder.append(BLACKLISTED);
} else {
headerStringBuilder.append(header.getValue());
}
headerStringBuilder
.append(SystemUtils.LINE_SEPARATOR);
}
builder.delete(builder.length() - SystemUtils.LINE_SEPARATOR.length(), builder.length());
Expand All @@ -97,6 +104,4 @@ private static StringBuilder appendNewLineIfAll(LogDetail logDetail, StringBuild
}
return builder;
}


}
}

0 comments on commit d09a101

Please sign in to comment.