Search in sources :

Example 6 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest 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 7 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyfox-examples by tvd12.

the class ApiGetUserTest method main.

public static void main(String[] args) throws Exception {
    HttpClientProxy httpClient = HttpClientProxy.builder().build();
    httpClient.start();
    RequestEntity entity = RequestEntity.builder().build();
    Request helloRequest = new GetRequest().setURL(API_URL + "tvd12").setEntity(entity).setResponseType(String.class).setResponseType(StatusCodes.NOT_FOUND, String.class);
    String response = httpClient.call(helloRequest, 10000);
    System.out.println("get user response: " + response);
}
Also used : GetRequest(com.tvd12.ezyhttp.client.request.GetRequest) GetRequest(com.tvd12.ezyhttp.client.request.GetRequest) Request(com.tvd12.ezyhttp.client.request.Request) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity)

Example 8 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstFailed.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstFailed() 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);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onException(new Exception("just test"));
        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(0)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 9 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstException.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstException() 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);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    doThrow(IllegalStateException.class).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.INTERNAL_SERVER_ERROR);
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 10 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest 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)

Aggregations

Test (org.testng.annotations.Test)14 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)8 BaseTest (com.tvd12.test.base.BaseTest)7 AsyncContext (javax.servlet.AsyncContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 BeforeTest (org.testng.annotations.BeforeTest)7 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)5 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)5 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)5 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)5 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)5 UnknownHostException (java.net.UnknownHostException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 GetRequest (com.tvd12.ezyhttp.client.request.GetRequest)4 StatusCodes (com.tvd12.ezyhttp.core.constant.StatusCodes)4 AsyncCallback (com.tvd12.ezyhttp.server.core.servlet.AsyncCallback)4 AsyncEvent (javax.servlet.AsyncEvent)4 ServletRequest (javax.servlet.ServletRequest)4 Mockito (org.mockito.Mockito)4 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)3