Search in sources :

Example 96 with HttpServletRequest

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

the class CharacterEncodingFilterTests method encodingIfEmptyAndNotForced.

@Test
public void encodingIfEmptyAndNotForced() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    given(request.getCharacterEncoding()).willReturn(null);
    given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
    given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);
    CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
    filter.init(new MockFilterConfig(FILTER_NAME));
    filter.doFilter(request, response, filterChain);
    verify(request).setCharacterEncoding(ENCODING);
    verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
    verify(request).removeAttribute(filteredName(FILTER_NAME));
    verify(filterChain).doFilter(request, response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 97 with HttpServletRequest

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

the class CharacterEncodingFilterTests method doesNotIfEncodingIsNotEmptyAndNotForced.

@Test
public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    given(request.getCharacterEncoding()).willReturn(ENCODING);
    given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
    given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);
    CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
    filter.init(new MockFilterConfig(FILTER_NAME));
    filter.doFilter(request, response, filterChain);
    verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
    verify(request).removeAttribute(filteredName(FILTER_NAME));
    verify(filterChain).doFilter(request, response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 98 with HttpServletRequest

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

the class ForwardedHeaderFilterTests method forwardedRequestWithServletForward.

// SPR-16983
@Test
public void forwardedRequestWithServletForward() throws Exception {
    this.request.setRequestURI("/foo");
    this.request.addHeader(X_FORWARDED_PROTO, "https");
    this.request.addHeader(X_FORWARDED_HOST, "www.mycompany.example");
    this.request.addHeader(X_FORWARDED_PORT, "443");
    this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
    HttpServletRequest wrappedRequest = (HttpServletRequest) this.filterChain.getRequest();
    this.request.setDispatcherType(DispatcherType.FORWARD);
    this.request.setRequestURI("/bar");
    this.filterChain.reset();
    this.filter.doFilter(wrappedRequest, new MockHttpServletResponse(), this.filterChain);
    HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
    assertThat(actual).isNotNull();
    assertThat(actual.getRequestURI()).isEqualTo("/bar");
    assertThat(actual.getRequestURL().toString()).isEqualTo("https://www.mycompany.example/bar");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 99 with HttpServletRequest

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

the class HiddenHttpMethodFilterTests method filterWithParameterForMethod.

private void filterWithParameterForMethod(String methodParam, String expectedMethod) throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    if (methodParam != null) {
        request.addParameter("_method", methodParam);
    }
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> assertThat(((HttpServletRequest) filterRequest).getMethod()).as("Invalid method").isEqualTo(expectedMethod);
    this.filter.doFilter(request, response, filterChain);
}
Also used : Test(org.junit.jupiter.api.Test) 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) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 100 with HttpServletRequest

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

the class ControllerTests method servletWrappingController.

@Test
public void servletWrappingController() throws Exception {
    HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath");
    HttpServletResponse response = new MockHttpServletResponse();
    ServletWrappingController swc = new ServletWrappingController();
    swc.setServletClass(TestServlet.class);
    swc.setServletName("action");
    Properties props = new Properties();
    props.setProperty("config", "myValue");
    swc.setInitParameters(props);
    swc.afterPropertiesSet();
    assertThat(TestServlet.config).isNotNull();
    assertThat(TestServlet.config.getServletName()).isEqualTo("action");
    assertThat(TestServlet.config.getInitParameter("config")).isEqualTo("myValue");
    assertThat(TestServlet.request).isNull();
    assertThat(TestServlet.destroyed).isFalse();
    assertThat(swc.handleRequest(request, response)).isNull();
    assertThat(TestServlet.request).isEqualTo(request);
    assertThat(TestServlet.response).isEqualTo(response);
    assertThat(TestServlet.destroyed).isFalse();
    swc.destroy();
    assertThat(TestServlet.destroyed).isTrue();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Properties(java.util.Properties) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)289 Test (org.junit.jupiter.api.Test)160 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)93 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)88 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)67 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)44 Authentication (org.springframework.security.core.Authentication)31 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)31 Test (org.junit.Test)28 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)26 IOException (java.io.IOException)21 ServletException (jakarta.servlet.ServletException)20 HttpServlet (jakarta.servlet.http.HttpServlet)19 HashMap (java.util.HashMap)17 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)16 FilterChain (jakarta.servlet.FilterChain)15 HttpSession (jakarta.servlet.http.HttpSession)14 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)14 ServletRequest (jakarta.servlet.ServletRequest)13