Search in sources :

Example 6 with HttpClient

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

the class HttpClientTest method postMethodConflict.

@Test
public void postMethodConflict() {
    // 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/409"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request));
    // then
    Asserts.assertThat(e).isEqualsType(HttpConflictException.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 7 with HttpClient

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

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

the class HttpClientTest method httpClientBuilderTest.

@Test
public void httpClientBuilderTest() {
    // given
    int readTimeout = RandomUtil.randomInt();
    int connectionTimeout = RandomUtil.randomInt();
    Object stringConverter = SingletonStringDeserializer.getInstance();
    Object bodyConverter = new MyTextBodyConverter();
    HttpClient.Builder clientBuilder = HttpClient.builder().readTimeout(readTimeout).connectTimeout(connectionTimeout).setStringConverter(stringConverter).addBodyConverter(bodyConverter).addBodyConverters(Collections.singletonList(bodyConverter)).addBodyConverters(Collections.singletonMap(ContentTypes.APPLICATION_JSON, bodyConverter));
    // when
    HttpClient client = clientBuilder.build();
    // then
    Asserts.assertEquals(FieldUtil.getFieldValue(client, "defaultReadTimeout"), readTimeout);
    Asserts.assertEquals(FieldUtil.getFieldValue(client, "defaultConnectTimeout"), connectionTimeout);
}
Also used : HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 9 with HttpClient

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

the class HttpClientTest method postMethodServer501.

@Test
public void postMethodServer501() {
    // 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/501"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request));
    // then
    Asserts.assertThat(e).isEqualsType(HttpRequestException.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 10 with HttpClient

use of com.tvd12.ezyhttp.client.HttpClient 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)

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