Search in sources :

Example 96 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method payloadInputStream.

@Test
void payloadInputStream() throws Exception {
    filter.setIncludePayload(true);
    byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
    request.setContent(requestBody);
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
        byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
        assertThat(buf).isEqualTo(requestBody);
    };
    filter.doFilter(request, response, filterChain);
    assertThat(filter.afterRequestMessage).contains("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) Test(org.junit.jupiter.api.Test)

Example 97 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method payloadMaxLength.

@Test
void payloadMaxLength() throws Exception {
    filter.setIncludePayload(true);
    filter.setMaxPayloadLength(3);
    byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
    request.setContent(requestBody);
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
        byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
        assertThat(buf).isEqualTo(requestBody);
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class);
        assertThat(wrapper.getContentAsByteArray()).isEqualTo("Hel".getBytes(StandardCharsets.UTF_8));
    };
    filter.doFilter(request, response, filterChain);
    assertThat(filter.afterRequestMessage).contains("Hel");
    assertThat(filter.afterRequestMessage).doesNotContain("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) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) Test(org.junit.jupiter.api.Test)

Example 98 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method payloadReader.

@Test
void payloadReader() throws Exception {
    filter.setIncludePayload(true);
    String requestBody = "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.afterRequestMessage).contains(requestBody);
}
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) Test(org.junit.jupiter.api.Test)

Example 99 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class ShallowEtagHeaderFilterTests method filterWriter.

@Test
public void filterWriter() 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);
        ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
        String responseBody = "Hello World";
        FileCopyUtils.copy(responseBody, filterResponse.getWriter());
    };
    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 100 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class ShallowEtagHeaderFilterTests method filterSendError.

@Test
public void filterSendError() 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).sendError(HttpServletResponse.SC_FORBIDDEN);
    };
    filter.doFilter(request, response, filterChain);
    assertThat(response.getStatus()).as("Invalid status").isEqualTo(403);
    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);
}
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

HttpServletResponse (jakarta.servlet.http.HttpServletResponse)118 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)76 Test (org.junit.jupiter.api.Test)47 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)34 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)31 FilterChain (jakarta.servlet.FilterChain)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 ServletException (jakarta.servlet.ServletException)14 StandardCharsets (java.nio.charset.StandardCharsets)14 HttpServlet (jakarta.servlet.http.HttpServlet)13 IOException (java.io.IOException)12 HashMap (java.util.HashMap)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 Test (org.junit.Test)10 Authentication (org.springframework.security.core.Authentication)10 FileCopyUtils (org.springframework.util.FileCopyUtils)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Collections (java.util.Collections)7