Search in sources :

Example 21 with RequestURI

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");
}
Also used : RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Example 22 with RequestURI

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();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) BodySerializer(com.tvd12.ezyhttp.core.codec.BodySerializer) Test(org.testng.annotations.Test)

Example 23 with RequestURI

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ToString(lombok.ToString) Test(org.testng.annotations.Test)

Example 24 with RequestURI

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);
}
Also used : RequestHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) RequestURIDecorator(com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator) Test(org.testng.annotations.Test)

Example 25 with RequestURI

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);
}
Also used : RequestHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) List(java.util.List) ArrayList(java.util.ArrayList) RequestURIDecorator(com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator)

Aggregations

RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)36 Test (org.testng.annotations.Test)33 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 ToString (lombok.ToString)29 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)27 ServletOutputStream (javax.servlet.ServletOutputStream)24 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 Cookie (javax.servlet.http.Cookie)22 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)10 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)9 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)4 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)4 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)4 ExceptionHandlerManager (com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager)4 ArrayList (java.util.ArrayList)4 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 RequestHandlersImplementer (com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer)3