use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerNullAndHasErrorHandler.
@Test
public void requestHandlerNullAndHasErrorHandler() 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_NOT_FOUND, null)).thenReturn(responseEntity);
componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get-handler-null";
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);
// 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 BlockingServletTest method requestHandlerEmpty.
@Test
public void requestHandlerEmpty() 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);
HttpServletResponse response = mock(HttpServletResponse.class);
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)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.METHOD_NOT_ALLOWED);
verify(outputStream, times(1)).write("method GET not allowed".getBytes());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method requestHandlerEmptyWithErrorHandlerButDataNull.
@Test
public void requestHandlerEmptyWithErrorHandlerButDataNull() 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);
when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(null);
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)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.METHOD_NOT_ALLOWED);
verify(outputStream, times(1)).write("method GET not allowed".getBytes());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetResponseContentTypeNullAndPostHandleRequestError.
@Test
public void doGetResponseContentTypeNullAndPostHandleRequestError() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
int defaultAsyncTimeout = RandomUtil.randomSmallInt() + 1;
componentManager.setAsyncDefaultTimeout(defaultAsyncTimeout);
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") });
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
AsyncContext asyncContext = mock(AsyncContext.class);
when(request.startAsync(request, response)).thenReturn(asyncContext);
when(request.isAsyncStarted()).thenReturn(true);
EzyWrap<AsyncListener> asyncListener = new EzyWrap<>();
doAnswer(it -> {
asyncListener.setValue(it.getArgumentAt(0, AsyncListener.class));
return null;
}).when(asyncContext).addListener(any(AsyncListener.class));
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandlerContentTypeNull requestHandler = new GetRequestHandlerContentTypeNull();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
// when
sut.service(request, response);
RequestInterceptor interceptor = mock(RequestInterceptor.class);
doThrow(IllegalStateException.class).when(interceptor).postHandle(any(), any());
componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
asyncListener.getValue().onComplete(new AsyncEvent(asyncContext));
// then
verify(request, times(1)).getMethod();
verify(request, times(2)).getRequestURI();
verify(asyncContext, times(1)).addListener(any(AsyncListener.class));
verify(asyncContext, times(1)).setTimeout(defaultAsyncTimeout);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method responseDataIsNull.
@Test
public void responseDataIsNull() 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();
GetRequestDataNullHandler requestHandler = new GetRequestDataNullHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
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();
}
Aggregations