Search in sources :

Example 31 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method requestHandlerNull.

@Test
public void requestHandlerNull() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get-handler-null";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    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)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.NOT_FOUND);
    verify(outputStream, times(1)).write("uri /get-handler-null not found".getBytes());
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) Test(org.testng.annotations.Test)

Example 32 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetTest.

@Test
public void doGetTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    RequestResponseWatcher watcher = mock(RequestResponseWatcher.class);
    componentManager.addRequestResponseWatchers(Collections.singletonList(watcher));
    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.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, 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());
    verify(watcher, times(1)).watchRequest(HttpMethod.GET, request);
    verify(watcher, times(1)).watchResponse(HttpMethod.GET, request, response);
    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) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) 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) RequestResponseWatcher(com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher) Test(org.testng.annotations.Test)

Example 33 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetSecureTest.

@Test
public void doGetSecureTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    RequestResponseWatcher watcher = mock(RequestResponseWatcher.class);
    componentManager.addRequestResponseWatchers(Collections.singletonList(watcher));
    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.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();
    RequestURIMeta uriMeta = RequestURIMeta.builder().management(false).authenticated(true).api(true).build();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, uriMeta), 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
    RequestURIManager requestURIManager = requestHandlerManager.getRequestURIManager();
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get"));
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get/"));
    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());
    verify(watcher, times(1)).watchRequest(HttpMethod.GET, request);
    verify(watcher, times(1)).watchResponse(HttpMethod.GET, request, response);
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) RequestURIManager(com.tvd12.ezyhttp.server.core.manager.RequestURIManager) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) 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) RequestResponseWatcher(com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher) Test(org.testng.annotations.Test)

Example 34 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI 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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) ServletOutputStream(javax.servlet.ServletOutputStream) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) 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 35 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doPutWithRedirectTest.

@Test
public void doPutWithRedirectTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/put-with-redirect-attributes";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.PUT.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") });
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    PutWithRedirectAttributesRequestHandler requestHandler = new PutWithRedirectAttributesRequestHandler();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.PUT, 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)).sendRedirect("/home");
    verify(interceptor, times(1)).preHandle(any(), any());
    verify(interceptor, times(1)).postHandle(any(), any());
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) ServletOutputStream(javax.servlet.ServletOutputStream) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) 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) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) Test(org.testng.annotations.Test)

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