Search in sources :

Example 51 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ErrorPageFilterTests method nestedServletExceptionIsUnwrapped.

@Test
public void nestedServletExceptionIsUnwrapped() throws Exception {
    this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new NestedServletException("Wrapper", new RuntimeException("BAD"));
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(RuntimeException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(RuntimeException.class);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) NestedServletException(org.springframework.web.util.NestedServletException) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 52 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ErrorPageFilterTests method errorMessageForRequestWithPathInfo.

@Test
public void errorMessageForRequestWithPathInfo() throws IOException, ServletException {
    this.request.setServletPath("/test");
    this.request.setPathInfo("/alpha");
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException();
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.output.toString()).contains("request [/test/alpha]");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 53 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ErrorPageFilterTests method oncePerRequest.

@Test
public void oncePerRequest() throws Exception {
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            assertThat(request.getAttribute("FILTER.FILTERED")).isNotNull();
            super.doFilter(request, response);
        }
    };
    this.filter.init(new MockFilterConfig("FILTER"));
    this.filter.doFilter(this.request, this.response, this.chain);
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 54 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ErrorPageFilterTests method unauthorizedWithErrorPath.

@Test
public void unauthorizedWithErrorPath() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(401, "UNAUTHORIZED");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    HttpServletResponseWrapper wrapper = (HttpServletResponseWrapper) this.chain.getResponse();
    assertThat(wrapper.getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(wrapper.getStatus()).isEqualTo(401);
    // The real response has to be 401 as well...
    assertThat(this.response.getStatus()).isEqualTo(401);
    assertThat(this.response.getForwardedUrl()).isEqualTo("/error");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 55 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project uplace.es by Uplace.

the class JWTFilterTest method testJWTFilterMissingAuthorization.

@Test
public void testJWTFilterMissingAuthorization() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/api/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain = new MockFilterChain();
    jwtFilter.doFilter(request, response, filterChain);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)108 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)106 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)106 Test (org.junit.Test)77 ServletRequest (javax.servlet.ServletRequest)28 ServletResponse (javax.servlet.ServletResponse)28 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 NestedServletException (org.springframework.web.util.NestedServletException)19 Before (org.junit.Before)17 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)14 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)11 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)9 MockServletContext (org.springframework.mock.web.MockServletContext)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8