use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class RequestLoggingFilterTests method allOptions.
@Test
void allOptions() throws Exception {
filter.setIncludeQueryString(true);
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(true);
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));
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\"}" + "]");
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterMatch.
@Test
public void filterMatch() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\"";
request.addHeader("If-None-Match", etag);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
filterResponse.setContentLength(responseBody.length);
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(304);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\"");
assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse();
byte[] expecteds = new byte[0];
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterMatchWeakEtag.
@Test
public void filterMatchWeakEtag() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\"";
request.addHeader("If-None-Match", "W/" + etag);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
filterResponse.setContentLength(responseBody.length);
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(304);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\"");
assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse();
byte[] expecteds = new byte[0];
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterSendRedirect.
@Test
public void filterSendRedirect() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
MockHttpServletResponse response = new MockHttpServletResponse();
final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
response.setContentLength(100);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
((HttpServletResponse) filterResponse).sendRedirect("https://www.google.com");
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(302);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isNull();
assertThat(response.getContentLength()).as("Invalid Content-Length header").isEqualTo(100);
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody);
assertThat(response.getRedirectedUrl()).as("Invalid redirect URL").isEqualTo("https://www.google.com");
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterNoMatchWeakETag.
@Test
public void filterNoMatchWeakETag() throws Exception {
this.filter.setWriteWeakETag(true);
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
MockHttpServletResponse response = new MockHttpServletResponse();
final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(200);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("W/\"0b10a8db164e0754105b7a99be72e3fe5\"");
assertThat(response.getContentLength() > 0).as("Invalid Content-Length header").isTrue();
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody);
}
Aggregations