Search in sources :

Example 1 with BodyDeserializer

use of com.tvd12.ezyhttp.core.codec.BodyDeserializer 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 2 with BodyDeserializer

use of com.tvd12.ezyhttp.core.codec.BodyDeserializer 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 3 with BodyDeserializer

use of com.tvd12.ezyhttp.core.codec.BodyDeserializer in project ezyhttp by youngmonkeys.

the class BodyDeserializerTest method commonTest.

@Test
public void commonTest() throws Exception {
    // given
    BodyDeserializer sut = new BodyDeserializer() {
    };
    // when
    Asserts.assertNull(sut.deserialize("", Object.class));
    Asserts.assertNull(sut.deserialize(mock(BodyData.class), Object.class));
    Asserts.assertNull(sut.deserialize(mock(InputStream.class), Object.class));
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) Test(org.testng.annotations.Test)

Example 4 with BodyDeserializer

use of com.tvd12.ezyhttp.core.codec.BodyDeserializer in project ezyhttp by youngmonkeys.

the class BodyDeserializerTest method deserializeToStringWithContentLengthTest.

@Test
public void deserializeToStringWithContentLengthTest() throws Exception {
    // given
    byte[] bytes = "abc".getBytes();
    InputStream inputStream = new ByteArrayInputStream(bytes);
    int contentLength = 2;
    BodyDeserializer sut = new BodyDeserializer() {
    };
    // when
    String actual = sut.deserializeToString(inputStream, contentLength);
    // then
    Asserts.assertEquals("ab", actual);
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Example 5 with BodyDeserializer

use of com.tvd12.ezyhttp.core.codec.BodyDeserializer in project ezyhttp by youngmonkeys.

the class BodyDeserializerTest method deserializeToStringWithoutContentLengthTest.

@Test
public void deserializeToStringWithoutContentLengthTest() throws Exception {
    // given
    byte[] bytes = "abc".getBytes();
    InputStream inputStream = new ByteArrayInputStream(bytes);
    int contentLength = 0;
    BodyDeserializer sut = new BodyDeserializer() {
    };
    // when
    String actual = sut.deserializeToString(inputStream, contentLength);
    // then
    Asserts.assertEquals("abc", actual);
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Aggregations

BodyDeserializer (com.tvd12.ezyhttp.core.codec.BodyDeserializer)11 Test (org.testng.annotations.Test)9 InputStream (java.io.InputStream)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpClient (com.tvd12.ezyhttp.client.HttpClient)3 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)3 IOException (java.io.IOException)3 BeforeTest (org.testng.annotations.BeforeTest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 EzyProcessor.processWithLogException (com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)1 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)1 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 DeserializeBodyException (com.tvd12.ezyhttp.core.exception.DeserializeBodyException)1 DeserializeCookieException (com.tvd12.ezyhttp.core.exception.DeserializeCookieException)1 DeserializeHeaderException (com.tvd12.ezyhttp.core.exception.DeserializeHeaderException)1 DeserializeParameterException (com.tvd12.ezyhttp.core.exception.DeserializeParameterException)1 DeserializePathVariableException (com.tvd12.ezyhttp.core.exception.DeserializePathVariableException)1 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)1 Map (java.util.Map)1