Search in sources :

Example 1 with HttpClient

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

the class HttpClientTest method postMethodNotAllow.

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

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

the class HttpClientTest method tryDeserializeResponseBodyStringSuccess.

@Test
public void tryDeserializeResponseBodyStringSuccess() throws Exception {
    // given
    String contentType = RandomUtil.randomShortAlphabetString();
    int contentLength = RandomUtil.randomSmallInt();
    InputStream inputStream = mock(InputStream.class);
    String data = RandomUtil.randomShortAlphabetString();
    BodyDeserializer deserializer = mock(BodyDeserializer.class);
    when(deserializer.deserializeToString(inputStream, contentLength)).thenReturn(data);
    Map<String, String> map = new HashMap<>();
    when(deserializer.deserialize(data, Map.class)).thenReturn(map);
    HttpClient sut = HttpClient.builder().addBodyConverter(contentType, deserializer).build();
    // when
    Map<String, String> actual = MethodInvoker.create().object(sut).method("deserializeResponseBody").param(String.class, contentType).param(int.class, contentLength).param(InputStream.class, inputStream).param(Class.class, null).call();
    // then
    Asserts.assertEquals(map, actual);
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with HttpClient

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

the class HttpClientTest method postMethodNotFound.

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

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

the class HttpClientTest method deserializeResponseBodyStringFailed.

@Test
public void deserializeResponseBodyStringFailed() throws Exception {
    // given
    String contentType = RandomUtil.randomShortAlphabetString();
    int contentLength = RandomUtil.randomSmallInt();
    InputStream inputStream = mock(InputStream.class);
    BodyDeserializer deserializer = mock(BodyDeserializer.class);
    IOException exception = new IOException("just test");
    when(deserializer.deserializeToString(inputStream, contentLength)).thenThrow(exception);
    HttpClient sut = HttpClient.builder().addBodyConverter(contentType, deserializer).build();
    // when
    Throwable e = Asserts.assertThrows(() -> MethodInvoker.create().object(sut).method("deserializeResponseBody").param(String.class, contentType).param(int.class, contentLength).param(InputStream.class, inputStream).param(Class.class, null).call());
    // then
    Asserts.assertThat(e.getCause().getCause()).isEqualsTo(exception);
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) InputStream(java.io.InputStream) HttpClient(com.tvd12.ezyhttp.client.HttpClient) IOException(java.io.IOException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with HttpClient

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

the class HttpClientTest method downloadToFileNotFoundTest.

@Test
public void downloadToFileNotFoundTest() {
    // given
    String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd/not-found-here";
    HttpClient sut = HttpClient.builder().build();
    // when
    Throwable e = Asserts.assertThrows(() -> sut.download(fileUrl, new File("test-output/no-commit")));
    // then
    Asserts.assertEqualsType(e, HttpNotFoundException.class);
}
Also used : HttpClient(com.tvd12.ezyhttp.client.HttpClient) File(java.io.File) 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