Search in sources :

Example 11 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method downloadToFileByRequestTest.

@Test
public void downloadToFileByRequestTest() throws Exception {
    // given
    String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
    DownloadRequest request = new DownloadRequest().setFileURL(fileUrl).setConnectTimeout(5000).setReadTimeout(5000).setHeaders(MultiValueMap.builder().setValue("hello", "world").build());
    HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
    // when
    String fileName = sut.download(request, new File("test-output/no-commit"));
    // then
    Asserts.assertEquals(fileName, "ezy-settings-1.0.0.xsd");
    Asserts.assertTrue(new File("test-output/no-commit/ezy-settings-1.0.0.xsd").exists());
    sut.close();
    sut.stop();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) File(java.io.File) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 12 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method newClientProxy.

private HttpClientProxy newClientProxy() {
    HttpClientProxy sut = HttpClientProxy.builder().autoStart(true).readTimeout(15000).connectTimeout(15000).setStringConverter(SingletonStringDeserializer.getInstance()).addBodyConverter(new TestBodyConverter()).addBodyConverters(Collections.singletonList(new TestBodyConverter())).addBodyConverter("world", new TestBodyConverter()).addBodyConverters(Collections.singletonMap("foo", new TestBodyConverter())).threadPoolSize(1).requestQueueCapacity(10).build();
    EzyProcessor.processWithLogException(sut::start);
    return sut;
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy)

Example 13 with HttpClientProxy

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

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method postWithExceptionTest.

@Test
public void postWithExceptionTest() {
    // given
    HttpClientProxy sut = newClientProxy();
    PostRequest request = new PostRequest().setConnectTimeout(15000).setEntity(boolean.class).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1.0:18081/greet");
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request, 150000));
    // then
    Asserts.assertThat(e).isEqualsType(UnknownHostException.class);
    sut.close();
    sut.stop();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 15 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireExceptionTest.

@Test
public void fireExceptionTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(String.class).setResponseType(StatusCodes.OK, String.class).setURL("http://unknow-host:18081/greet");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<Exception> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<String>() {

        @Override
        public void onResponse(String response) {
        }

        @Override
        public void onException(Exception e) {
            wrap.setValue(e);
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    // then
    Asserts.assertEquals(BadRequestException.class, wrap.getValue().getClass());
    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)

Aggregations

HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)31 BaseTest (com.tvd12.test.base.BaseTest)27 BeforeTest (org.testng.annotations.BeforeTest)27 Test (org.testng.annotations.Test)27 File (java.io.File)11 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)8 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)8 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)8 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)8 UnknownHostException (java.net.UnknownHostException)8 DownloadCancellationToken (com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken)6 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)5 FileOutputStream (java.io.FileOutputStream)5 OutputStream (java.io.OutputStream)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 URL (java.net.URL)4 HelloRequest (com.tvd12.ezyhttp.client.test.request.HelloRequest)3 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 DownloadRequest (com.tvd12.ezyhttp.client.request.DownloadRequest)2 Request (com.tvd12.ezyhttp.client.request.Request)2