Search in sources :

Example 51 with ServletException

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

the class CorsFilterTests method nonCorsRequest.

@Test
void nonCorsRequest() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/test.html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
        assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)).isNull();
    };
    filter.doFilter(request, response, filterChain);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpMethod(org.springframework.http.HttpMethod) UrlBasedCorsConfigurationSource(org.springframework.web.cors.UrlBasedCorsConfigurationSource) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.fail(org.assertj.core.api.Assertions.fail) 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 52 with ServletException

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

the class RequestContextFilterTests method testFilterInvocation.

private void testFilterInvocation(final ServletException sex) throws Exception {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute("myAttr", "myValue");
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    // Expect one invocation by the filter being tested
    class DummyFilterChain implements FilterChain {

        public int invocations = 0;

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
            ++invocations;
            if (invocations == 1) {
                assertThat(RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST)).isSameAs("myValue");
                if (sex != null) {
                    throw sex;
                }
            } else {
                throw new IllegalStateException("Too many invocations");
            }
        }
    }
    DummyFilterChain fc = new DummyFilterChain();
    MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
    RequestContextFilter rbf = new RequestContextFilter();
    rbf.init(mfc);
    try {
        rbf.doFilter(req, resp, fc);
        assertThat(sex).isNull();
    } catch (ServletException ex) {
        assertThat(sex).isNotNull();
    }
    assertThatIllegalStateException().isThrownBy(RequestContextHolder::currentRequestAttributes);
    assertThat(fc.invocations).isEqualTo(1);
}
Also used : ServletException(jakarta.servlet.ServletException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig)

Example 53 with ServletException

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

the class CorsWebFilterTests method validPreFlightRequest.

@Test
public void validPreFlightRequest() throws ServletException, IOException {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("https://domain1.com/test.html").header(HOST, "domain1.com").header(ORIGIN, "https://domain2.com").header(ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.GET.name()).header(ACCESS_CONTROL_REQUEST_HEADERS, "header1, header2"));
    WebFilterChain filterChain = filterExchange -> Mono.error(new AssertionError("Preflight requests must not be forwarded to the filter chain"));
    filter.filter(exchange, filterChain).block();
    HttpHeaders headers = exchange.getResponse().getHeaders();
    assertThat(headers.getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("https://domain2.com");
    assertThat(headers.getFirst(ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("header1, header2");
    assertThat(headers.getFirst(ACCESS_CONTROL_EXPOSE_HEADERS)).isEqualTo("header3, header4");
    assertThat(Long.parseLong(headers.getFirst(ACCESS_CONTROL_MAX_AGE))).isEqualTo(123L);
}
Also used : ACCESS_CONTROL_EXPOSE_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ACCESS_CONTROL_REQUEST_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ACCESS_CONTROL_ALLOW_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS) HttpMethod(org.springframework.http.HttpMethod) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ACCESS_CONTROL_MAX_AGE(org.springframework.http.HttpHeaders.ACCESS_CONTROL_MAX_AGE) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServletException(jakarta.servlet.ServletException) ACCESS_CONTROL_REQUEST_METHOD(org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) HOST(org.springframework.http.HttpHeaders.HOST) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ORIGIN(org.springframework.http.HttpHeaders.ORIGIN) WebFilterChain(org.springframework.web.server.WebFilterChain) ACCESS_CONTROL_ALLOW_ORIGIN(org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) HttpHeaders(org.springframework.http.HttpHeaders) WebFilterChain(org.springframework.web.server.WebFilterChain) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 54 with ServletException

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

the class CorsWebFilterTests method invalidPreFlightRequest.

@Test
public void invalidPreFlightRequest() throws ServletException, IOException {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("https://domain1.com/test.html").header(HOST, "domain1.com").header(ORIGIN, "https://domain2.com").header(ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.DELETE.name()).header(ACCESS_CONTROL_REQUEST_HEADERS, "header1, header2"));
    WebFilterChain filterChain = filterExchange -> Mono.error(new AssertionError("Preflight requests must not be forwarded to the filter chain"));
    filter.filter(exchange, filterChain).block();
    assertThat(exchange.getResponse().getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
}
Also used : ACCESS_CONTROL_EXPOSE_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ACCESS_CONTROL_REQUEST_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ACCESS_CONTROL_ALLOW_HEADERS(org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS) HttpMethod(org.springframework.http.HttpMethod) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ACCESS_CONTROL_MAX_AGE(org.springframework.http.HttpHeaders.ACCESS_CONTROL_MAX_AGE) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServletException(jakarta.servlet.ServletException) ACCESS_CONTROL_REQUEST_METHOD(org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) HOST(org.springframework.http.HttpHeaders.HOST) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ORIGIN(org.springframework.http.HttpHeaders.ORIGIN) WebFilterChain(org.springframework.web.server.WebFilterChain) ACCESS_CONTROL_ALLOW_ORIGIN(org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) WebFilterChain(org.springframework.web.server.WebFilterChain) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 55 with ServletException

use of jakarta.servlet.ServletException 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)

Aggregations

ServletException (jakarta.servlet.ServletException)115 IOException (java.io.IOException)72 Test (org.junit.jupiter.api.Test)26 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)17 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)15 ServletContext (jakarta.servlet.ServletContext)14 FilterChain (jakarta.servlet.FilterChain)13 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)13 BeforeEach (org.junit.jupiter.api.BeforeEach)12 BeforeMethod (org.testng.annotations.BeforeMethod)11 ServletConfig (jakarta.servlet.ServletConfig)10 Arrays (java.util.Arrays)10 Enumeration (java.util.Enumeration)10 UnavailableException (jakarta.servlet.UnavailableException)9 HttpHeaders (org.springframework.http.HttpHeaders)9 HttpMethod (org.springframework.http.HttpMethod)9 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)9 ServletRequest (jakarta.servlet.ServletRequest)8