use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerNullAndHasErrorHandler.
@Test
public void requestHandlerNullAndHasErrorHandler() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
ResponseEntity responseEntity = ResponseEntity.ok();
when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(responseEntity);
componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get-handler-null";
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.OK);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerEmptyWithErrorHandlerButDataNull.
@Test
public void requestHandlerEmptyWithErrorHandlerButDataNull() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(null);
componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandler requestHandler = new GetRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.METHOD_NOT_ALLOWED);
verify(outputStream, times(1)).write("method GET not allowed".getBytes());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.
the class UnhandledErrorHandlerTest method returnString.
@Test
public void returnString() {
// given
String result = "bar";
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(ContentTypes.APPLICATION_JSON);
}
use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerEmptyAndHasErrorHandlerButException.
@Test
public void requestHandlerEmptyAndHasErrorHandlerButException() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
ResponseEntity responseEntity = ResponseEntity.ok();
when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, null)).thenReturn(responseEntity);
componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
doThrow(new IllegalStateException("just test")).when(response).setStatus(StatusCodes.OK);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandler requestHandler = new GetRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(2)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.OK);
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerEmptyAndHasErrorHandler.
@Test
public void requestHandlerEmptyAndHasErrorHandler() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
ResponseEntity responseEntity = ResponseEntity.ok();
when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, null)).thenReturn(responseEntity);
componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandler requestHandler = new GetRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.OK);
componentManager.destroy();
}
Aggregations