Search in sources :

Example 1 with DispatcherServletWebRequest

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");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with DispatcherServletWebRequest

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");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with DispatcherServletWebRequest

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.");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 4 with DispatcherServletWebRequest

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;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

DispatcherServletWebRequest (org.springframework.web.servlet.handler.DispatcherServletWebRequest)4 Test (org.junit.jupiter.api.Test)3 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 View (org.springframework.web.servlet.View)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1