Search in sources :

Example 6 with RequestArguments

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);
}
Also used : HashMap(java.util.HashMap) Reference(com.fasterxml.jackson.databind.JsonMappingException.Reference) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) TryCatch(com.tvd12.ezyhttp.server.core.annotation.TryCatch)

Example 7 with RequestArguments

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();
}
Also used : EzyResultCallback(com.tvd12.ezyfox.concurrent.callback.EzyResultCallback) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Example 8 with RequestArguments

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);
}
Also used : RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) DeserializeHeaderException(com.tvd12.ezyhttp.core.exception.DeserializeHeaderException) DeserializePathVariableException(com.tvd12.ezyhttp.core.exception.DeserializePathVariableException) DeserializeBodyException(com.tvd12.ezyhttp.core.exception.DeserializeBodyException) DeserializeCookieException(com.tvd12.ezyhttp.core.exception.DeserializeCookieException) DeserializeParameterException(com.tvd12.ezyhttp.core.exception.DeserializeParameterException) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Test(org.testng.annotations.Test)

Example 9 with RequestArguments

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());
}
Also used : RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Example 10 with RequestArguments

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();
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Aggregations

RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)7 AsyncContext (javax.servlet.AsyncContext)6 Test (org.testng.annotations.Test)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)4 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)3 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 DeserializeBodyException (com.tvd12.ezyhttp.core.exception.DeserializeBodyException)2 DeserializeCookieException (com.tvd12.ezyhttp.core.exception.DeserializeCookieException)2 DeserializeHeaderException (com.tvd12.ezyhttp.core.exception.DeserializeHeaderException)2 DeserializeParameterException (com.tvd12.ezyhttp.core.exception.DeserializeParameterException)2 DeserializePathVariableException (com.tvd12.ezyhttp.core.exception.DeserializePathVariableException)2 DeserializeValueException (com.tvd12.ezyhttp.core.exception.DeserializeValueException)2 HttpRequestException (com.tvd12.ezyhttp.core.exception.HttpRequestException)2 SimpleRequestArguments (com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ServletException (javax.servlet.ServletException)2