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());
}
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());
}
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);
}
}
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;
}
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);
}
Aggregations