use of com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestUnknownExceptionResultIsNull.
@Test
public void getRequestUnknownExceptionResultIsNull() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
ExceptionHandlerManager exceptionHandlerManager = componentManager.getExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler(null, null);
exceptionHandlerManager.addUncaughtExceptionHandler(IllegalStateException.class, exceptionHandler);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestUnknownExceptionnHandler requestHandler = new GetRequestUnknownExceptionnHandler(null);
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
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(2)).setStatus(StatusCodes.BAD_REQUEST);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestUnknownExceptionNoHandler.
@Test
public void getRequestUnknownExceptionNoHandler() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
ExceptionHandlerManager exceptionHandlerManager = componentManager.getExceptionHandlerManager();
exceptionHandlerManager.destroy();
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestUnknownExceptionnHandler requestHandler = new GetRequestUnknownExceptionnHandler(null);
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
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(2)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestUnknownExceptionContentTypeIsNull.
@Test
public void getRequestUnknownExceptionContentTypeIsNull() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
ExceptionHandlerManager exceptionHandlerManager = componentManager.getExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler("hello", null);
exceptionHandlerManager.addUncaughtExceptionHandler(IllegalStateException.class, exceptionHandler);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestUnknownExceptionnHandler requestHandler = new GetRequestUnknownExceptionnHandler(null);
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
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)).getContentType();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestUnknownExceptionError.
@Test
public void getRequestUnknownExceptionError() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
ExceptionHandlerManager exceptionHandlerManager = componentManager.getExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler("", null);
exceptionHandlerManager.addUncaughtExceptionHandler(IllegalStateException.class, exceptionHandler);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestUnknownExceptionnHandler requestHandler = new GetRequestUnknownExceptionnHandler(null);
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
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(2)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager in project ezyhttp by youngmonkeys.
the class ExceptionHandlerManagerTest method test.
@Test
public void test() {
// given
ExceptionHandlerManager sut = new ExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler();
sut.addExceptionHandler(exceptionHandler);
ExUncaughtExceptionHandler uncaughtExceptionHandler = new ExUncaughtExceptionHandler();
sut.addUncaughtExceptionHandler(Exception.class, uncaughtExceptionHandler);
// when
// then
Asserts.assertEquals(1, sut.getExceptionHandlerList().size());
Asserts.assertEquals(uncaughtExceptionHandler, sut.getUncaughtExceptionHandler(Exception.class));
}
Aggregations