Search in sources :

Example 6 with UnhandledErrorHandler

use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.

the class UnhandledErrorHandlerTest method defaultNull.

@Test
public void defaultNull() {
    // given
    UnhandledErrorHandler sut = new UnhandledErrorHandler() {
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    // when
    Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
    // then
    Asserts.assertNull(actual);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test)

Example 7 with UnhandledErrorHandler

use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.

the class UnhandledErrorHandlerTest method returnResponseEntity.

@Test
public void returnResponseEntity() {
    // given
    ResponseEntity result = ResponseEntity.ok();
    UnhandledErrorHandler sut = new UnhandledErrorHandler() {

        @Override
        public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
            return result;
        }
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    // when
    Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
    // then
    Asserts.assertEquals(result, actual);
    verify(response, times(1)).setContentType(null);
    verify(response, times(1)).setContentType(ContentTypes.APPLICATION_JSON);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Test(org.testng.annotations.Test)

Example 8 with UnhandledErrorHandler

use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.

the class UnhandledErrorHandlerTest method returnView.

@Test
public void returnView() {
    // given
    View result = View.builder().template("foo").build();
    UnhandledErrorHandler sut = new UnhandledErrorHandler() {

        @Override
        public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
            return result;
        }
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.TEXT_HTML_UTF8);
    // when
    Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
    // then
    Asserts.assertEquals(result, actual);
    verify(response, times(1)).setContentType(ContentTypes.TEXT_HTML_UTF8);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) View(com.tvd12.ezyhttp.server.core.view.View) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Test(org.testng.annotations.Test)

Aggregations

UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Test (org.testng.annotations.Test)8 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)4 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)4 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 ToString (lombok.ToString)4 HttpMethod (com.tvd12.ezyhttp.core.constant.HttpMethod)3 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)3 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)3 View (com.tvd12.ezyhttp.server.core.view.View)1