Search in sources :

Example 6 with MockFilterChain

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

the class RelativeRedirectFilterTests method wrapOnceOnly.

@Test
public void wrapOnceOnly() throws Exception {
    HttpServletResponse original = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    this.filter.doFilterInternal(new MockHttpServletRequest(), original, chain);
    HttpServletResponse wrapped1 = (HttpServletResponse) chain.getResponse();
    assertThat(wrapped1).isNotSameAs(original);
    chain.reset();
    this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped1, chain);
    HttpServletResponse current = (HttpServletResponse) chain.getResponse();
    assertThat(current).isSameAs(wrapped1);
    chain.reset();
    HttpServletResponse wrapped2 = new HttpServletResponseWrapper(wrapped1);
    this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped2, chain);
    current = (HttpServletResponse) chain.getResponse();
    assertThat(current).isSameAs(wrapped2);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 7 with MockFilterChain

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

the class FormContentFilterTests method setup.

@BeforeEach
public void setup() {
    this.request = new MockHttpServletRequest("PUT", "/");
    this.request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
    this.response = new MockHttpServletResponse();
    this.filterChain = new MockFilterChain();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with MockFilterChain

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

the class FormContentFilterTests method wrapFormEncodedOnly.

@Test
public void wrapFormEncodedOnly() throws Exception {
    String[] contentTypes = new String[] { "text/plain", "multipart/form-data" };
    for (String contentType : contentTypes) {
        MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/");
        request.setContent("".getBytes("ISO-8859-1"));
        request.setContentType(contentType);
        this.filterChain = new MockFilterChain();
        this.filter.doFilter(request, this.response, this.filterChain);
        assertThat(this.filterChain.getRequest()).isSameAs(request);
    }
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 9 with MockFilterChain

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

the class FormContentFilterTests method wrapPutPatchAndDeleteOnly.

@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
    for (HttpMethod method : HttpMethod.values()) {
        MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
        request.setContent("foo=bar".getBytes("ISO-8859-1"));
        request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
        this.filterChain = new MockFilterChain();
        this.filter.doFilter(request, this.response, this.filterChain);
        if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
            assertThat(this.filterChain.getRequest()).isNotSameAs(request);
        } else {
            assertThat(this.filterChain.getRequest()).isSameAs(request);
        }
    }
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 10 with MockFilterChain

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

the class ResourceUrlProviderJavaConfigTests method setup.

@BeforeEach
@SuppressWarnings("resource")
public void setup() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(WebConfig.class);
    context.refresh();
    this.request = new MockHttpServletRequest("GET", "/");
    this.request.setContextPath("/myapp");
    this.response = new MockHttpServletResponse();
    this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter(), (request, response, chain) -> {
        Object urlProvider = context.getBean(ResourceUrlProvider.class);
        request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
        chain.doFilter(request, response);
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebMvcConfigurationSupport(org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport) ResourceHandlerRegistry(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry) HttpServlet(jakarta.servlet.http.HttpServlet) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

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