use of com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetManagementTest.
@Test
public void doGetManagementTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
componentManager.setManagementPort(MANAGEMENT_POR);
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(MANAGEMENT_POR);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "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();
GetRequestHandler requestHandler = new GetRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, true), 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(request, times(1)).getServerPort();
verify(response, times(1)).getContentType();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.OK);
DataConverters dataConverters = componentManager.getDataConverters();
BodySerializer bodySerializer = dataConverters.getBodySerializer(ContentTypes.APPLICATION_JSON);
ExResponse responseData = new ExResponse("Hello ParameterValue, HeaderValue, CookieValue");
verify(outputStream, times(1)).write(bodySerializer.serialize(responseData));
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager in project ezyhttp by youngmonkeys.
the class RequestHandlersImplementerTest method implementOneFailed.
@Test
public void implementOneFailed() {
// given
RequestHandlersImplementer sut = new RequestHandlersImplementer();
Controller controller = new Controller();
RequestHandlerManager manager = new RequestHandlerManager();
// when
Throwable e = Asserts.assertThrows(() -> manager.addHandlers(sut.implement(Collections.singletonList(controller))));
// then
Asserts.assertThat(e).isEqualsType(DuplicateURIMappingHandlerException.class);
}
use of com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager in project ezyhttp by youngmonkeys.
the class RequestHandlersImplementerTest method implementOneWithURIDecorator.
@Test
public void implementOneWithURIDecorator() {
// given
RequestHandlersImplementer sut = new RequestHandlersImplementer();
Controller controller = new Controller();
RequestHandlerManager manager = new RequestHandlerManager();
manager.setAllowOverrideURI(true);
RequestURIDecorator requestURIDecorator = mock(RequestURIDecorator.class);
when(requestURIDecorator.decorate(any(), any())).thenReturn("hello-world");
sut.setRequestURIDecorator(requestURIDecorator);
// when
manager.addHandlers(sut.implement(Collections.singletonList(controller)));
// then
RequestURI uri = new RequestURI(HttpMethod.GET, "/hello-world", false);
Asserts.assertThat(manager.getHandlerListByURI().get(uri).size()).isEqualsTo(2);
}
use of com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doPostTest.
@Test
public void doPostTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/post";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.POST.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.getParameterValues("param")).thenReturn(new String[] { "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"), new Cookie(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_NAME, "{}") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
PostRequestHandler requestHandler = new PostRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
RequestInterceptor interceptor = mock(RequestInterceptor.class);
when(interceptor.preHandle(any(), any())).thenReturn(true);
componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
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.OK);
DataConverters dataConverters = componentManager.getDataConverters();
BodySerializer bodySerializer = dataConverters.getBodySerializer(ContentTypes.APPLICATION_JSON);
ExResponse responseData = new ExResponse("Hello ParameterValue, HeaderValue, CookieValue");
verify(outputStream, times(1)).write(bodySerializer.serialize(responseData));
verify(interceptor, times(1)).preHandle(any(), any());
verify(interceptor, times(1)).postHandle(any(), any());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager 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();
}
Aggregations