use of com.tvd12.ezyhttp.core.codec.BodySerializer 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();
}
use of com.tvd12.ezyhttp.core.codec.BodySerializer in project ezyhttp by youngmonkeys.
the class BlockingServlet method responseBody.
protected void responseBody(HttpServletResponse response, Object data) throws IOException {
String contentType = response.getContentType();
BodySerializer bodySerializer = dataConverters.getBodySerializer(contentType);
byte[] bytes = bodySerializer.serialize(data);
responseBytes(response, bytes);
}
use of com.tvd12.ezyhttp.core.codec.BodySerializer in project ezyhttp by youngmonkeys.
the class DataConvertersTest method getBodySerializerNormalTest.
@Test
public void getBodySerializerNormalTest() {
// given
DataConverters sut = new DataConverters(new ObjectMapper());
String contentType = ContentTypes.APPLICATION_JSON;
// when
BodySerializer actual = sut.getBodySerializer(contentType);
// then
Asserts.assertEquals(JsonBodyConverter.class, actual.getClass());
sut.destroy();
}
Aggregations