Search in sources :

Example 1 with GetRequest

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

the class HttpClientProxyTest method executeJsonTest.

@Test
public void executeJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.execute(request, new RequestCallback<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            wrap.setValue(response.getBody());
            countDownLatch.countDown();
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with GetRequest

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

the class HttpClientTest method getTest.

protected static void getTest() throws Exception {
    HttpClient client = HttpClient.builder().build();
    GetRequest request = new GetRequest().setURL("http://localhost:8081/bye?messages=a,b,c&numbers=1,2,3").setEntity(null).setResponseType(String.class).setReadTimeout(HttpClient.NO_TIMEOUT).setConnectTimeout(HttpClient.NO_TIMEOUT);
    String response = client.call(request);
    System.out.println(response);
}
Also used : HttpClient(com.tvd12.ezyhttp.client.HttpClient) GetRequest(com.tvd12.ezyhttp.client.request.GetRequest)

Example 3 with GetRequest

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

the class CustomerApisTest method getCustomerTest.

protected static void getCustomerTest() throws Exception {
    HttpClient client = HttpClient.builder().build();
    RequestEntity entity = RequestEntity.builder().header("token", "123").build();
    GetRequest request = new GetRequest().setURL("http://localhost:8081/api/v1/customer/hello/dung").setEntity(entity).setResponseType(String.class).setReadTimeout(HttpClient.NO_TIMEOUT).setConnectTimeout(HttpClient.NO_TIMEOUT);
    String response = client.call(request);
    System.out.println(response);
}
Also used : HttpClient(com.tvd12.ezyhttp.client.HttpClient) GetRequest(com.tvd12.ezyhttp.client.request.GetRequest) RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity)

Example 4 with GetRequest

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

the class HttpClientProxyTest method fireJsonButExceptionInCallbackTest.

@Test
public void fireJsonButExceptionInCallbackTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<TestResponse>() {

        @Override
        public void onResponse(TestResponse response) {
            wrap.setValue(response);
            countDownLatch.countDown();
            throw new RuntimeException("just test");
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    Thread.sleep(100);
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with GetRequest

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

the class FileUploaderTest method acceptFirstMaxUploadSizeException.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstMaxUploadSizeException() 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);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    doThrow(new MaxUploadSizeException(100)).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.BAD_REQUEST);
    verify(response, times(1)).getOutputStream();
    verify(outputStream, times(1)).write(any(byte[].class));
    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) ServletOutputStream(javax.servlet.ServletOutputStream) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) 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)

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