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();
}
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();
}
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");
}
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);
}
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();
}
Aggregations