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);
}
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);
}
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();
}
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();
}
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);
}
Aggregations