use of com.tvd12.ezyhttp.client.request.Request 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.client.request.Request 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.client.request.Request in project ezyhttp by youngmonkeys.
the class AsyncCallbackTest method onTimeoutWithHttpServletResponse.
@Test
public void onTimeoutWithHttpServletResponse() {
// given
AsyncCallback callback = event -> {
};
AsyncContext asyncContext = mock(AsyncContext.class);
ServletRequest request = mock(ServletRequest.class);
when(asyncContext.getRequest()).thenReturn(request);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
AsyncEvent event = new AsyncEvent(asyncContext);
// when
callback.onTimeout(event);
// then
verify(asyncContext, times(1)).getRequest();
verify(asyncContext, times(2)).getResponse();
verify(response, times(1)).setStatus(StatusCodes.REQUEST_TIMEOUT);
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class UnhandledErrorHandlerTest method returnString.
@Test
public void returnString() {
// given
String result = "bar";
UnhandledErrorHandler sut = new UnhandledErrorHandler() {
@Override
public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
return result;
}
};
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
// when
Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
// then
Asserts.assertEquals(result, actual);
verify(response, times(1)).setContentType(ContentTypes.APPLICATION_JSON);
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class GlobalExceptionHandler method handleException.
@TryCatch(InvalidFormatException.class)
public void handleException(RequestArguments args, HttpServletRequest request, HttpServletResponse response, InvalidFormatException e) {
e.printStackTrace();
Map<String, String> data = new HashMap<>();
for (Reference reference : e.getPath()) data.put(reference.getFieldName(), "invalid");
throw new HttpBadRequestException(data);
}
Aggregations