use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.
the class RequestURITest method test.
@Test
public void test() {
// given
RequestURI uri1 = new RequestURI(HttpMethod.GET, "/", true);
RequestURI uri2 = new RequestURI(HttpMethod.GET, "/", true);
RequestURI uri3 = new RequestURI(HttpMethod.POST, "/", true);
RequestURI uri4 = new RequestURI(HttpMethod.GET, "/api", true);
RequestURI uri5 = new RequestURI(HttpMethod.PUT, "/api/v1", true);
RequestURI uri6 = new RequestURI(HttpMethod.GET, "", true);
RequestURI uri11 = new RequestURI(HttpMethod.GET, "/", false, true, false, "fullPath");
RequestURI uri12 = new RequestURI(HttpMethod.PUT, "/api/v1/", true);
RequestURI ur122 = new RequestURI(HttpMethod.GET, "/", RequestURIMeta.builder().api(true).authenticated(true).payment(true).feature("hello.world").build());
// when
// then
Asserts.assertEquals(uri1.getMethod(), HttpMethod.GET);
Asserts.assertNotEquals(uri11, null);
Asserts.assertEquals(uri11, uri11);
Asserts.assertNotEquals(uri1, new Object());
Asserts.assertEquals(uri1, uri1);
Asserts.assertEquals(uri1, uri2);
Asserts.assertNotEquals(uri1, uri3);
Asserts.assertNotEquals(uri1, uri4);
Asserts.assertNotEquals(uri1, uri5);
Asserts.assertNotEquals(uri1, uri6);
Asserts.assertNotEquals(uri1, uri11);
Asserts.assertTrue(uri11.isResource());
Asserts.assertEquals(uri11.getResourceFullPath(), "fullPath");
Asserts.assertEquals(uri1.getSameURI(), "/");
Asserts.assertEquals(uri5.getSameURI(), "/api/v1/");
Asserts.assertEquals(uri12.getSameURI(), "/api/v1");
Asserts.assertTrue(ur122.isApi());
Asserts.assertTrue(ur122.isPayment());
Asserts.assertEquals(ur122.getFeature(), "hello.world");
}
use of com.tvd12.ezyhttp.server.core.request.RequestURI 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.request.RequestURI in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetFromManagement.
@Test
public void doGetFromManagement() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
componentManager.setManagementPort(MANAGEMENT_POR);
componentManager.getRequestHandlerManager().addHandler(new RequestURI(HttpMethod.GET, "/get", false), mock(RequestHandler.class));
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") });
HttpServletResponse response = mock(HttpServletResponse.class);
// 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.request.RequestURI 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.request.RequestURI in project ezyhttp by youngmonkeys.
the class ApplicationContextBuilder method addRequestHandlers.
protected void addRequestHandlers(EzyBeanContext beanContext) {
List<Object> controllerList = controllerManager.getControllers();
RequestHandlersImplementer implementer = newRequestHandlersImplementer();
implementer.setRequestURIDecorator(beanContext.getSingleton(RequestURIDecorator.class));
Map<RequestURI, List<RequestHandler>> requestHandlers = implementer.implement(controllerList);
requestHandlerManager.addHandlers(requestHandlers);
}
Aggregations