use of org.springframework.web.servlet.handler.DispatcherServletWebRequest in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderContainsViewWithExceptionDetails.
@Test
void renderContainsViewWithExceptionDetails() {
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), false);
errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(), webRequest.getResponse());
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
assertThat(responseString).contains("<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>").contains("<div>Exception message</div>").contains("<div style='white-space:pre-wrap;'>java.lang.IllegalStateException");
});
}
use of org.springframework.web.servlet.handler.DispatcherServletWebRequest in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderCanUseJavaTimeTypeAsTimestamp.
@Test
void renderCanUseJavaTimeTypeAsTimestamp() {
// gh-23256
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), false);
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, withAllOptions());
attributes.put("timestamp", Clock.systemUTC().instant());
errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
assertThat(responseString).contains("This application has no explicit mapping for /error");
});
}
use of org.springframework.web.servlet.handler.DispatcherServletWebRequest in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderWhenAlreadyCommittedLogsMessage.
@Test
void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), true);
errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(), webRequest.getResponse());
assertThat(output).contains("Cannot render error page for request [/path] " + "and exception [Exception message] as the response has " + "already been committed. As a result, the response may have the wrong status code.");
});
}
use of org.springframework.web.servlet.handler.DispatcherServletWebRequest in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method createWebRequest.
private DispatcherServletWebRequest createWebRequest(Exception ex, boolean committed) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
MockHttpServletResponse response = new MockHttpServletResponse();
DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, response);
webRequest.setAttribute("jakarta.servlet.error.exception", ex, RequestAttributes.SCOPE_REQUEST);
webRequest.setAttribute("jakarta.servlet.error.request_uri", "/path", RequestAttributes.SCOPE_REQUEST);
response.setCommitted(committed);
response.setOutputStreamAccessAllowed(!committed);
response.setWriterAccessAllowed(!committed);
return webRequest;
}
Aggregations