Search in sources :

Example 11 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper 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)

Aggregations

ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 ContentCachingResponseWrapper (org.springframework.web.util.ContentCachingResponseWrapper)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 FilterChain (jakarta.servlet.FilterChain)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 StringJoiner (java.util.StringJoiner)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 FileCopyUtils (org.springframework.util.FileCopyUtils)1 StopWatch (org.springframework.util.StopWatch)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1