Search in sources :

Example 1 with AsyncCallback

use of com.tvd12.ezyhttp.server.core.servlet.AsyncCallback 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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 2 with AsyncCallback

use of com.tvd12.ezyhttp.server.core.servlet.AsyncCallback in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method onErrorFailed.

@Test
public void onErrorFailed() {
    // given
    AsyncCallback callback = event -> {
    };
    AsyncContext asyncContext = mock(AsyncContext.class);
    doThrow(RuntimeException.class).when(asyncContext).complete();
    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.onError(event);
    // then
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(2)).getResponse();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 3 with AsyncCallback

use of com.tvd12.ezyhttp.server.core.servlet.AsyncCallback in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method onTimeoutFailed.

@Test
public void onTimeoutFailed() {
    // given
    AsyncCallback callback = event -> {
    };
    AsyncContext asyncContext = mock(AsyncContext.class);
    doThrow(RuntimeException.class).when(asyncContext).complete();
    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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 4 with AsyncCallback

use of com.tvd12.ezyhttp.server.core.servlet.AsyncCallback in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method test.

@Test
public void test() throws Exception {
    // given
    AsyncCallback callback = event -> {
    };
    ServletRequest request = mock(ServletRequest.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(asyncContext.getRequest()).thenReturn(request);
    AsyncEvent event = new AsyncEvent(asyncContext);
    // when
    // then
    callback.onComplete(event);
    callback.onError(event);
    callback.onTimeout(event);
    callback.onStartAsync(event);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 5 with AsyncCallback

use of com.tvd12.ezyhttp.server.core.servlet.AsyncCallback in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method onErrorWithHttpServletResponse.

@Test
public void onErrorWithHttpServletResponse() {
    // 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.onError(event);
    // then
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(2)).getResponse();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Aggregations

StatusCodes (com.tvd12.ezyhttp.core.constant.StatusCodes)5 AsyncCallback (com.tvd12.ezyhttp.server.core.servlet.AsyncCallback)5 AsyncContext (javax.servlet.AsyncContext)5 AsyncEvent (javax.servlet.AsyncEvent)5 ServletRequest (javax.servlet.ServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Mockito (org.mockito.Mockito)5 Test (org.testng.annotations.Test)5