Search in sources :

Example 21 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseIsCommittedWhenExceptionIsThrownDuringAsyncDispatch.

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

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException("BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
}
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 22 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method statusCode.

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

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertThat(((HttpServletResponse) response).getStatus()).isEqualTo(200);
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(200);
}
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) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 23 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseCommitted.

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

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.response.getForwardedUrl()).isNull();
    assertThat(this.response.isCommitted()).isTrue();
}
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 24 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method subClassExceptionError.

@Test
public void subClassExceptionError() 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 IllegalStateException("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(IllegalStateException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(IllegalStateException.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();
}
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) 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 25 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseIsCommittedWhenStatusIs400PlusDuringAsyncDispatch.

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

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            ((HttpServletResponse) response).sendError(400, "BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
}
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)

Aggregations

HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)37 HttpServletResponse (javax.servlet.http.HttpServletResponse)31 ServletResponse (javax.servlet.ServletResponse)23 IOException (java.io.IOException)22 ServletException (javax.servlet.ServletException)20 ServletRequest (javax.servlet.ServletRequest)20 Test (org.junit.Test)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 NestedServletException (org.springframework.web.util.NestedServletException)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 ErrorPage (org.springframework.boot.web.server.ErrorPage)12 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)8 PrintWriter (java.io.PrintWriter)7 ServletOutputStream (javax.servlet.ServletOutputStream)6 Provider (com.google.inject.Provider)4 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)4 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)4 Filter (javax.servlet.Filter)4