Search in sources :

Example 6 with BodyDeserializer

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

the class DataConvertersTest method getBodyDeserializerDefaultTest.

@Test
public void getBodyDeserializerDefaultTest() {
    // given
    DataConverters sut = new DataConverters(new ObjectMapper());
    String contentType = "unknown";
    // when
    BodyDeserializer actual = sut.getBodyDeserializer(contentType);
    // then
    Asserts.assertEquals(TextBodyConverter.class, actual.getClass());
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 7 with BodyDeserializer

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

the class DataConvertersTest method getBodyDeserializerNormalTest.

@Test
public void getBodyDeserializerNormalTest() {
    // given
    DataConverters sut = new DataConverters(new ObjectMapper());
    String contentType = ContentTypes.APPLICATION_JSON;
    // when
    BodyDeserializer actual = sut.getBodyDeserializer(contentType);
    // then
    Asserts.assertEquals(JsonBodyConverter.class, actual.getClass());
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 8 with BodyDeserializer

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

the class AbstractRequestHandler method deserializeBody.

protected <T> T deserializeBody(BodyData bodyData, Class<T> type) throws IOException {
    String contentType = bodyData.getContentType();
    if (contentType == null) {
        throw new HttpBadRequestException("contentType is null");
    }
    BodyDeserializer deserializer = dataConverters.getBodyDeserializer(contentType);
    try {
        return deserializer.deserialize(bodyData, type);
    } catch (Exception e) {
        throw new DeserializeBodyException("can't deserialize body data to: " + type.getName(), e);
    }
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) DeserializeBodyException(com.tvd12.ezyhttp.core.exception.DeserializeBodyException) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DeserializeHeaderException(com.tvd12.ezyhttp.core.exception.DeserializeHeaderException) IOException(java.io.IOException) DeserializePathVariableException(com.tvd12.ezyhttp.core.exception.DeserializePathVariableException) DeserializeBodyException(com.tvd12.ezyhttp.core.exception.DeserializeBodyException) DeserializeCookieException(com.tvd12.ezyhttp.core.exception.DeserializeCookieException) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException) DeserializeParameterException(com.tvd12.ezyhttp.core.exception.DeserializeParameterException) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException)

Example 9 with BodyDeserializer

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

the class HttpClient method deserializeResponseBody.

protected Object deserializeResponseBody(String contentType, int contentLength, InputStream inputStream, Class<?> responseType) throws IOException {
    BodyDeserializer deserializer = dataConverters.getBodyDeserializer(contentType);
    Object body;
    if (responseType != null) {
        if (responseType == String.class) {
            body = deserializer.deserializeToString(inputStream, contentLength);
        } else {
            body = deserializer.deserialize(inputStream, responseType);
        }
    } else {
        body = deserializer.deserializeToString(inputStream, contentLength);
        if (body != null) {
            try {
                body = deserializer.deserialize((String) body, Map.class);
            } catch (Exception e) {
            // do nothing
            }
        }
    }
    return body;
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) IOException(java.io.IOException)

Example 10 with BodyDeserializer

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

the class HttpClientTest method tryDeserializeResponseBodyStringNull.

@Test
public void tryDeserializeResponseBodyStringNull() {
    // given
    String contentType = RandomUtil.randomShortAlphabetString();
    int contentLength = RandomUtil.randomSmallInt();
    InputStream inputStream = mock(InputStream.class);
    BodyDeserializer deserializer = mock(BodyDeserializer.class);
    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.assertNull(actual);
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) InputStream(java.io.InputStream) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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