Search in sources :

Example 16 with HttpClient

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

the class CustomerApisTest method addCustomerTest.

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

Example 17 with HttpClient

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

the class HttpClientTest method postTest.

protected static void postTest() throws Exception {
    HttpClient client = HttpClient.builder().build();
    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);
    System.out.println(response);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest) RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity)

Example 18 with HttpClient

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

the class HttpClientTest method postMethodTimeout.

@Test
public void postMethodTimeout() {
    // given
    HttpClient sut = HttpClient.builder().build();
    PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/408"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request));
    // then
    Asserts.assertThat(e).isEqualsType(HttpRequestTimeoutException.class);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with HttpClient

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

the class HttpClientTest method postMethodServerInternalError.

@Test
public void postMethodServerInternalError() {
    // given
    HttpClient sut = HttpClient.builder().build();
    PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/500"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request));
    // then
    Asserts.assertThat(e).isEqualsType(HttpInternalServerErrorException.class);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 20 with HttpClient

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

the class HttpClientTest method processDownloadErrorInputStreamIsNull.

@Test
public void processDownloadErrorInputStreamIsNull() {
    // given
    HttpClient sut = HttpClient.builder().build();
    HttpURLConnection connection = mock(HttpURLConnection.class);
    // when
    Exception e = MethodInvoker.create().object(sut).method("processDownloadError").param(HttpURLConnection.class, connection).param(String.class, "https://example.com/file.jpg").param(int.class, 404).call();
    // then
    Asserts.assertEqualsType(e, HttpNotFoundException.class);
    verify(connection, times(1)).getErrorStream();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpClient(com.tvd12.ezyhttp.client.HttpClient) IOException(java.io.IOException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

HttpClient (com.tvd12.ezyhttp.client.HttpClient)31 BeforeTest (org.testng.annotations.BeforeTest)25 Test (org.testng.annotations.Test)25 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)20 RequestEntity (com.tvd12.ezyhttp.client.request.RequestEntity)5 GetRequest (com.tvd12.ezyhttp.client.request.GetRequest)4 BodyDeserializer (com.tvd12.ezyhttp.core.codec.BodyDeserializer)3 InputStream (java.io.InputStream)3 Request (com.tvd12.ezyhttp.client.request.Request)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)1 DeleteRequest (com.tvd12.ezyhttp.client.request.DeleteRequest)1 Customer (com.tvd12.ezyhttp.client.test.request.Customer)1 HelloRequest (com.tvd12.ezyhttp.client.test.request.HelloRequest)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 List (java.util.List)1