Search in sources :

Example 11 with PostRequest

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

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

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

the class HttpClientProxyTest method postTest.

protected static void postTest(HttpClientProxy client) throws Exception {
    HelloRequest body = new HelloRequest();
    body.setWho("dzung");
    RequestEntity entity = RequestEntity.body(body);
    PostRequest request = new PostRequest().setURL("http://localhost:8081/").setEntity(entity).setResponseType(String.class).setReadTimeout(HttpClient.NO_TIMEOUT).setConnectTimeout(HttpClient.NO_TIMEOUT);
    String response = client.call(request, 1000);
    System.out.println(response);
}
Also used : HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest)

Example 14 with PostRequest

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

the class HttpClientProxyTest method maxCapacity.

@Test
public void maxCapacity() {
    // given
    HttpClientProxy sut = HttpClientProxy.builder().autoStart(true).requestQueueCapacity(1).threadPoolSize(1).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
    AtomicReference<Throwable> ref = new AtomicReference<>();
    Thread[] threads = new Thread[4];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(() -> {
            while (ref.get() == null) {
                try {
                    sut.call(request, 150000);
                } catch (Exception e) {
                    ref.set(e);
                }
            }
        });
    }
    for (Thread thread : threads) {
        thread.start();
    }
    while (ref.get() == null) {
        EzyThreads.sleep(3);
    }
    // then
    Asserts.assertThat(ref.get()).isEqualsType(RequestQueueFullException.class);
    sut.close();
    sut.stop();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) 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 15 with PostRequest

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

the class HttpClientProxyTest method postJsonTest.

@Test
public void postJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    PostRequest request = new PostRequest().setConnectTimeout(15000).setEntity(new TestRequest("Monkey")).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet").setURL(new URL("http://127.0.0.1:18081/greet")).setURL(URI.create("http://127.0.0.1:18081/greet"));
    // 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

PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)22 BeforeTest (org.testng.annotations.BeforeTest)21 Test (org.testng.annotations.Test)21 HttpClient (com.tvd12.ezyhttp.client.HttpClient)20 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)5 BaseTest (com.tvd12.test.base.BaseTest)5 RequestEntity (com.tvd12.ezyhttp.client.request.RequestEntity)3 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)2 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)2 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)2 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)2 HelloRequest (com.tvd12.ezyhttp.client.test.request.HelloRequest)2 UnknownHostException (java.net.UnknownHostException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RequestCallback (com.tvd12.ezyhttp.client.callback.RequestCallback)1 DeleteRequest (com.tvd12.ezyhttp.client.request.DeleteRequest)1 GetRequest (com.tvd12.ezyhttp.client.request.GetRequest)1 Request (com.tvd12.ezyhttp.client.request.Request)1 Customer (com.tvd12.ezyhttp.client.test.request.Customer)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1