use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestUnknownExceptionError.
@Test
public void getRequestUnknownExceptionError() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
ExceptionHandlerManager exceptionHandlerManager = componentManager.getExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler("", null);
exceptionHandlerManager.addUncaughtExceptionHandler(IllegalStateException.class, exceptionHandler);
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.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();
GetRequestUnknownExceptionnHandler requestHandler = new GetRequestUnknownExceptionnHandler(null);
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), 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(2)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetNotAcceptable.
@Test
public void doGetNotAcceptable() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
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(false);
componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
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.NOT_ACCEPTABLE);
verify(interceptor, times(1)).preHandle(any(), any());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetButResponseBodyIsNullTest.
@Test
public void doGetButResponseBodyIsNullTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get-no-body";
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.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();
GetNoBodyRequestHandler requestHandler = new GetNoBodyRequestHandler();
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)).setStatus(StatusCodes.OK);
verify(interceptor, times(1)).preHandle(any(), any());
verify(interceptor, times(1)).postHandle(any(), any());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestDeserializeValueException.
@Test
public void getRequestDeserializeValueException() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
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.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();
GetRequestDeserializeValueExceptionHandler requestHandler = new GetRequestDeserializeValueExceptionHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), 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(response, times(1)).getContentType();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirstMaxUploadSizeException.
@SuppressWarnings("unchecked")
@Test
public void acceptFirstMaxUploadSizeException() throws Exception {
// given
AsyncContext asyncContext = mock(AsyncContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("hello-world");
when(asyncContext.getRequest()).thenReturn(request);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
Part part = mock(Part.class);
File outputFile = new File("test-output/files");
EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
doThrow(new MaxUploadSizeException(100)).when(callback).apply();
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doAnswer(it -> {
EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
cb.onResponse(Boolean.TRUE);
return null;
}).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, part, outputFile, callback);
// then
verify(callback, times(1)).apply();
verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
verify(response, times(1)).getOutputStream();
verify(outputStream, times(1)).write(any(byte[].class));
verify(request, times(1)).getRequestURI();
verify(asyncContext, times(1)).getRequest();
verify(asyncContext, times(1)).complete();
}
Aggregations