Skip to content

Commit

Permalink
Add test for a fully logged request
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster committed Sep 14, 2019
1 parent ae469f4 commit 4b1bcb4
Showing 1 changed file with 45 additions and 0 deletions.
Expand Up @@ -291,6 +291,51 @@ public void payloadMaxLength() throws Exception {
assertThat(filter.afterRequestMessage).doesNotContain("Hello World");
}

@Test
public void allOptions() throws Exception {
filter.setIncludeQueryString(true);
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(true);

MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
request.setQueryString("booking=42");
request.setRemoteAddr("4.2.2.2");
request.setSession(new MockHttpSession(null, "42"));
request.setRemoteUser("Arthur");
request.setContentType("application/json");
String requestBody = "{\"msg\": \"Hello World\"}";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
MockHttpServletResponse response = new MockHttpServletResponse();

FilterChain filterChain = (filterRequest, filterResponse) -> {
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
String buf = FileCopyUtils.copyToString(filterRequest.getReader());
assertThat(buf).isEqualTo(requestBody);
};

filter.doFilter(request, response, filterChain);

assertThat(filter.beforeRequestMessage)
.isEqualTo("Before request ["
+ "POST /hotels?booking=42"
+ ", client=4.2.2.2"
+ ", session=42"
+ ", user=Arthur"
+ ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]"
+ "]");

assertThat(filter.afterRequestMessage)
.isEqualTo("After request ["
+ "POST /hotels?booking=42"
+ ", client=4.2.2.2"
+ ", session=42"
+ ", user=Arthur"
+ ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]"
+ ", payload={\"msg\": \"Hello World\"}"
+ "]");
}


private static class MyRequestLoggingFilter extends AbstractRequestLoggingFilter {

Expand Down

0 comments on commit 4b1bcb4

Please sign in to comment.