use of com.tvd12.ezyhttp.server.core.request.RequestArguments 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);
}
use of com.tvd12.ezyhttp.server.core.request.RequestArguments in project ezyhttp by youngmonkeys.
the class ResourceRequestHandlerTest method handleWithDrainExceptionWhenCallTest.
@SuppressWarnings("unchecked")
@Test
public void handleWithDrainExceptionWhenCallTest() throws Exception {
// given
String resourcePath = "static/index.html";
String resourceURI = "/index.html";
String resourceExtension = "html";
ResourceDownloadManager downloadManager = mock(ResourceDownloadManager.class);
doThrow(IllegalStateException.class).when(downloadManager).drainAsync(any(InputStream.class), any(OutputStream.class), any(EzyResultCallback.class));
int timeout = RandomUtil.randomSmallInt() + 1;
ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, downloadManager, timeout);
RequestArguments arguments = mock(RequestArguments.class);
AsyncContext asyncContext = mock(AsyncContext.class);
when(arguments.getAsyncContext()).thenReturn(asyncContext);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
doThrow(IOException.class).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
when(asyncContext.getResponse()).thenReturn(response);
// when
sut.handle(arguments);
// then
verify(arguments, times(1)).getAsyncContext();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
verify(asyncContext, times(1)).setTimeout(timeout);
verify(asyncContext, times(1)).getResponse();
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.server.core.request.RequestArguments in project ezyhttp by youngmonkeys.
the class AbstractRequestHandlerTest method handleException.
@Test
public void handleException() throws Exception {
// given
ExResponse response = new ExResponse("Hello World");
ExRequestHandler sut = new ExRequestHandler() {
@Override
public Object handleRequest(RequestArguments arguments) throws Exception {
throw new Exception("just test");
}
@Override
protected Object handleException(RequestArguments arguments, Exception e) {
return response;
}
};
RequestArguments requestArguments = mock(RequestArguments.class);
// when
Object actual = sut.handle(requestArguments);
// then
Asserts.assertEquals(response, actual);
}
use of com.tvd12.ezyhttp.server.core.request.RequestArguments in project ezyhttp by youngmonkeys.
the class AbstractRequestHandlerTest method handleTest.
@Test
public void handleTest() throws Exception {
// given
ExResponse response = new ExResponse("Hello World");
ExRequestHandler sut = new ExRequestHandler() {
public Object handleRequest(RequestArguments arguments) {
return response;
}
};
RequestArguments requestArguments = mock(RequestArguments.class);
// when
Object actual = sut.handle(requestArguments);
// then
Asserts.assertEquals(response, actual);
Asserts.assertNull(sut.getHandlerMethod());
}
use of com.tvd12.ezyhttp.server.core.request.RequestArguments in project ezyhttp by youngmonkeys.
the class ResourceRequestHandlerTest method handleAsyncTest.
@Test
public void handleAsyncTest() throws Exception {
// given
String resourcePath = "static/index.html";
String resourceURI = "/index.html";
String resourceExtension = "html";
ResourceDownloadManager downloadManager = new ResourceDownloadManager();
ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, downloadManager);
RequestArguments arguments = mock(RequestArguments.class);
AsyncContext asyncContext = mock(AsyncContext.class);
when(arguments.getAsyncContext()).thenReturn(asyncContext);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
when(asyncContext.getResponse()).thenReturn(response);
// when
sut.handle(arguments);
// then
Asserts.assertTrue(sut.isAsync());
Asserts.assertEquals(HttpMethod.GET, sut.getMethod());
Asserts.assertEquals("/index.html", sut.getRequestURI());
Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getResponseContentType());
Thread.sleep(300);
downloadManager.stop();
verify(arguments, times(1)).getAsyncContext();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.OK);
verify(asyncContext, times(1)).getResponse();
verify(asyncContext, times(1)).complete();
}
Aggregations