Search in sources :

Example 16 with HttpClientProxy

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

the class HttpClientProxyTest method closeWithRemainTasks.

@Test
public void closeWithRemainTasks() {
    // given
    HttpClientProxy sut = new HttpClientProxy(1, 100, HttpClient.builder().build());
    EzyFutureMap<Request> futures = FieldUtil.getFieldValue(sut, "futures");
    Request request = mock(Request.class);
    futures.addFuture(request);
    sut.start();
    // when
    sut.close();
    // then
    verify(request, times(1)).getURL();
}
Also used : HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 17 with HttpClientProxy

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

the class HttpClientProxyTest method downloadToOutputStreamTest.

@Test
public void downloadToOutputStreamTest() throws Exception {
    // given
    String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
    HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
    File outFile = new File("test-output/no-commit/download-test.xml");
    EzyFileUtil.createFileIfNotExists(outFile);
    OutputStream outputStream = new FileOutputStream(outFile);
    // when
    sut.download(fileUrl, outputStream);
    // then
    Asserts.assertTrue(new File("test-output/no-commit/download-test.xml").exists());
    sut.close();
    sut.stop();
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) 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 18 with HttpClientProxy

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

the class HttpClientProxyTest method clientWasNotActiveAtExecute.

@Test
public void clientWasNotActiveAtExecute() {
    // given
    HttpClientProxy sut = HttpClientProxy.builder().build();
    PostRequest request = new PostRequest().setConnectTimeout(15000).setEntity(String.class).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet");
    // when
    Throwable e = Asserts.assertThrows(() -> sut.execute(request, new RequestCallback<ResponseEntity>() {

        public void onException(Exception e) {
        }

        public void onResponse(ResponseEntity response) {
        }
    }));
    // then
    Asserts.assertThat(e).isEqualsType(ClientNotActiveException.class);
    sut.close();
    sut.stop();
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) RequestCallback(com.tvd12.ezyhttp.client.callback.RequestCallback) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) 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 19 with HttpClientProxy

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

the class HttpClientProxyTest method main.

public static void main(String[] args) throws Exception {
    HttpClientProxy client = HttpClientProxy.builder().build();
    client.start();
    postTest(client);
    new Thread(() -> {
        for (int i = 0; i < 100; ++i) {
            try {
                postTest(client);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    Thread.sleep(3);
    getTest(client);
    client.close();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) 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)

Example 20 with HttpClientProxy

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

the class HttpClientProxyTest method getJsonTest.

@Test
public void getJsonTest() 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").setURL(new URL("http://127.0.0.1:18081/greet?who=Monkey")).setURL(URI.create("http://127.0.0.1:18081/greet?who=Monkey"));
    // when
    TestResponse actual = sut.call(request, 15000);
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, actual);
    sut.close();
    sut.stop();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) URL(java.net.URL) 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