Search in sources :

Example 1 with BodySerializer

use of com.tvd12.ezyhttp.core.codec.BodySerializer in project ezyhttp by youngmonkeys.

the class DataConvertersTest method getBodySerializerDefaultTest.

@Test
public void getBodySerializerDefaultTest() {
    // given
    DataConverters sut = new DataConverters(new ObjectMapper());
    String contentType = "unknown";
    // when
    BodySerializer actual = sut.getBodySerializer(contentType);
    // then
    Asserts.assertEquals(TextBodyConverter.class, actual.getClass());
}
Also used : DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) BodySerializer(com.tvd12.ezyhttp.core.codec.BodySerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 2 with BodySerializer

use of com.tvd12.ezyhttp.core.codec.BodySerializer in project ezyhttp by youngmonkeys.

the class DataConvertersTest method getBodySerializerWithUTF8Test.

@Test
public void getBodySerializerWithUTF8Test() {
    // given
    DataConverters sut = new DataConverters(new ObjectMapper());
    String contentType = ContentTypes.TEXT_HTML_UTF8;
    // when
    BodySerializer actual = sut.getBodySerializer(contentType);
    // then
    Asserts.assertEquals(TextBodyConverter.class, actual.getClass());
}
Also used : DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) BodySerializer(com.tvd12.ezyhttp.core.codec.BodySerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with BodySerializer

use of com.tvd12.ezyhttp.core.codec.BodySerializer 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 4 with BodySerializer

use of com.tvd12.ezyhttp.core.codec.BodySerializer 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();
}
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) Test(org.testng.annotations.Test)

Example 5 with BodySerializer

use of com.tvd12.ezyhttp.core.codec.BodySerializer 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)

Aggregations

BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)8 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)7 Test (org.testng.annotations.Test)7 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)4 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)4 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)4 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)4 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 Cookie (javax.servlet.http.Cookie)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ToString (lombok.ToString)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)3 RequestResponseWatcher (com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher)2 RequestURIManager (com.tvd12.ezyhttp.server.core.manager.RequestURIManager)1 RequestURIMeta (com.tvd12.ezyhttp.server.core.request.RequestURIMeta)1