Search in sources :

Example 11 with HttpServletResponseWrapper

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

the class ErrorPageFilterTests method responseIsCommittedWhenRequestIsAsyncAndExceptionIsThrown.

@Test
void responseIsCommittedWhenRequestIsAsyncAndExceptionIsThrown() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.request.setAsyncStarted(true);
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        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 : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Example 12 with HttpServletResponseWrapper

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

the class ErrorPageFilterTests method responseIsCommittedWhenStatusIs400PlusDuringAsyncDispatch.

@Test
void responseIsCommittedWhenStatusIs400PlusDuringAsyncDispatch() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    setUpAsyncDispatch();
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        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 : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Example 13 with HttpServletResponseWrapper

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

the class ErrorPageFilterTests method globalError.

@Test
void globalError() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new TestFilterChain((request, response, chain) -> response.sendError(400, "BAD"));
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(400);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/error");
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Example 14 with HttpServletResponseWrapper

use of jakarta.servlet.http.HttpServletResponseWrapper 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 15 with HttpServletResponseWrapper

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

the class ServletWebRequestTests method decoratedNativeRequest.

@Test
public void decoratedNativeRequest() {
    HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest);
    HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse);
    ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse);
    assertThat(request.getNativeRequest()).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(ServletRequest.class)).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(HttpServletRequest.class)).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(MockHttpServletRequest.class)).isSameAs(servletRequest);
    assertThat(request.getNativeRequest(MultipartRequest.class)).isNull();
    assertThat(request.getNativeResponse()).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(ServletResponse.class)).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(HttpServletResponse.class)).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(MockHttpServletResponse.class)).isSameAs(servletResponse);
    assertThat(request.getNativeResponse(MultipartRequest.class)).isNull();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)17 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)16 Test (org.junit.jupiter.api.Test)16 ServletResponse (jakarta.servlet.ServletResponse)14 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)14 RequestDispatcher (jakarta.servlet.RequestDispatcher)13 ServletException (jakarta.servlet.ServletException)13 ServletRequest (jakarta.servlet.ServletRequest)13 IOException (java.io.IOException)13 Enumeration (java.util.Enumeration)13 HashMap (java.util.HashMap)13 Map (java.util.Map)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ClientAbortException (org.apache.catalina.connector.ClientAbortException)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)13 BDDMockito.given (org.mockito.BDDMockito.given)13 BDDMockito.then (org.mockito.BDDMockito.then)13