Search in sources :

Example 1 with MockFilterChain

use of org.springframework.web.testfixture.servlet.MockFilterChain in project spring-framework by spring-projects.

the class WebUtilsTests method adaptFromForwardedHeaders.

// SPR-16668
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
    MockFilterChain chain = new MockFilterChain();
    new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
    return (HttpServletRequest) chain.getRequest();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 2 with MockFilterChain

use of org.springframework.web.testfixture.servlet.MockFilterChain in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method adaptRequestFromForwardedHeaders.

// SPR-16668
private void adaptRequestFromForwardedHeaders() throws Exception {
    MockFilterChain chain = new MockFilterChain();
    new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
    HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 3 with MockFilterChain

use of org.springframework.web.testfixture.servlet.MockFilterChain in project spring-framework by spring-projects.

the class FormContentFilterTests method invalidMediaType.

@Test
public void invalidMediaType() throws Exception {
    this.request.setContent("".getBytes("ISO-8859-1"));
    this.request.setContentType("foo");
    this.filterChain = new MockFilterChain();
    this.filter.doFilter(this.request, this.response, this.filterChain);
    assertThat(this.filterChain.getRequest()).isSameAs(this.request);
}
Also used : MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 4 with MockFilterChain

use of org.springframework.web.testfixture.servlet.MockFilterChain in project spring-framework by spring-projects.

the class OncePerRequestFilterTests method setup.

@BeforeEach
@SuppressWarnings("serial")
public void setup() throws Exception {
    this.request = new MockHttpServletRequest();
    this.request.setScheme("http");
    this.request.setServerName("localhost");
    this.request.setServerPort(80);
    this.filterChain = new MockFilterChain(new HttpServlet() {
    });
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with MockFilterChain

use of org.springframework.web.testfixture.servlet.MockFilterChain in project spring-framework by spring-projects.

the class RelativeRedirectFilterTests method sendRedirect.

private void sendRedirect(String location) throws Exception {
    MockFilterChain chain = new MockFilterChain();
    this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain);
    HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse();
    wrappedResponse.sendRedirect(location);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain)

Aggregations

MockFilterChain (org.springframework.web.testfixture.servlet.MockFilterChain)11 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)10 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)7 Test (org.junit.jupiter.api.Test)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ForwardedHeaderFilter (org.springframework.web.filter.ForwardedHeaderFilter)3 HttpServlet (jakarta.servlet.http.HttpServlet)2 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Configuration (org.springframework.context.annotation.Configuration)1 HttpMethod (org.springframework.http.HttpMethod)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1 ResourceHandlerRegistry (org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)1 WebMvcConfigurationSupport (org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport)1 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)1