Search in sources :

Example 61 with FilterChain

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\"}" + "]");
}
Also used : Test(org.junit.jupiter.api.Test) WebUtils(org.springframework.web.util.WebUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 62 with FilterChain

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);
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 63 with FilterChain

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);
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 64 with FilterChain

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");
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 65 with FilterChain

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);
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

FilterChain (jakarta.servlet.FilterChain)141 Test (org.junit.jupiter.api.Test)134 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)103 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)102 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)68 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)54 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)35 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 ServletRequest (jakarta.servlet.ServletRequest)25 ServletResponse (jakarta.servlet.ServletResponse)25 Authentication (org.springframework.security.core.Authentication)23 MockFilterChain (org.springframework.mock.web.MockFilterChain)20 ServletException (jakarta.servlet.ServletException)16 StandardCharsets (java.nio.charset.StandardCharsets)16 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)16 IOException (java.io.IOException)15 BeforeEach (org.junit.jupiter.api.BeforeEach)14 FileCopyUtils (org.springframework.util.FileCopyUtils)14 Arrays (java.util.Arrays)11