From 734f173ef9a88860f2c16a8b2d947319ab760cdb Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 30 Mar 2024 20:31:04 +0700 Subject: [PATCH] version 1.2.9: update JsonBodyConverter --- ezyhttp-client/pom.xml | 2 +- ezyhttp-core/pom.xml | 2 +- .../ezyhttp/core/codec/JsonBodyConverter.java | 6 +++++- .../test/codec/JsonBodyConverterTest.java | 17 ++++++++++++++++ .../io/BytesRangeFileInputStreamTest.java | 20 +++++++++++++++++++ ezyhttp-server-boot/pom.xml | 2 +- ezyhttp-server-core/pom.xml | 2 +- ezyhttp-server-graphql/pom.xml | 2 +- ezyhttp-server-jetty/pom.xml | 2 +- ezyhttp-server-management/pom.xml | 2 +- ezyhttp-server-thymeleaf/pom.xml | 2 +- ezyhttp-server-tomcat/pom.xml | 2 +- pom.xml | 2 +- 13 files changed, 52 insertions(+), 11 deletions(-) diff --git a/ezyhttp-client/pom.xml b/ezyhttp-client/pom.xml index d2b22fd4..c8036d75 100644 --- a/ezyhttp-client/pom.xml +++ b/ezyhttp-client/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-client diff --git a/ezyhttp-core/pom.xml b/ezyhttp-core/pom.xml index 716fc74b..eb80e02c 100644 --- a/ezyhttp-core/pom.xml +++ b/ezyhttp-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-core diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/codec/JsonBodyConverter.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/codec/JsonBodyConverter.java index dd0857c6..94a711bd 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/codec/JsonBodyConverter.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/codec/JsonBodyConverter.java @@ -5,6 +5,7 @@ import java.nio.charset.StandardCharsets; import com.fasterxml.jackson.databind.ObjectMapper; +import com.tvd12.ezyfox.stream.EzyInputStreams; import com.tvd12.ezyhttp.core.data.BodyData; public class JsonBodyConverter implements BodyConverter { @@ -33,8 +34,11 @@ public T deserialize(BodyData data, Class bodyType) throws IOException { return deserialize(inputStream, bodyType); } + @SuppressWarnings("unchecked") @Override public T deserialize(InputStream inputStream, Class bodyType) throws IOException { - return objectMapper.readValue(inputStream, bodyType); + return bodyType == String.class + ? (T) EzyInputStreams.toStringUtf8(inputStream) + : objectMapper.readValue(inputStream, bodyType); } } diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/codec/JsonBodyConverterTest.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/codec/JsonBodyConverterTest.java index 6637295a..39ce4795 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/codec/JsonBodyConverterTest.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/codec/JsonBodyConverterTest.java @@ -117,6 +117,23 @@ public void deserializeFromBodyData() throws Exception { verify(bodyData, times(1)).getInputStream(); } + @Test + public void deserializeStringToStringTest() throws Exception { + // given + JsonBodyConverter sut = new JsonBodyConverter(new ObjectMapper()); + + String body = "{\"hello\":\"world\",\"foo\":\"bar\"}"; + + // when + String actual = sut.deserialize( + new ByteArrayInputStream(body.getBytes()), + String.class + ); + + // then + Asserts.assertEquals(body, actual); + } + @Data @AllArgsConstructor diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java index 39995420..fcc989f1 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.RandomAccessFile; import java.nio.file.Files; @@ -311,4 +312,23 @@ public void seekErrorTest() { // then Asserts.assertEqualsType(e, IllegalArgumentException.class); } + + @Test + public void seekIoErrorTest() { + // given + final String pomFilePath = "pom.xml"; + final String range = "bytes=-1-2"; + + // when + final Throwable e = Asserts.assertThrows( + () -> new BytesRangeFileInputStream( + pomFilePath, + -1, + -2 + ) + ); + + // then + Asserts.assertEqualsType(e, IOException.class); + } } diff --git a/ezyhttp-server-boot/pom.xml b/ezyhttp-server-boot/pom.xml index e0e72d76..e55dddc4 100644 --- a/ezyhttp-server-boot/pom.xml +++ b/ezyhttp-server-boot/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-boot diff --git a/ezyhttp-server-core/pom.xml b/ezyhttp-server-core/pom.xml index 5867edc6..dbe2f128 100644 --- a/ezyhttp-server-core/pom.xml +++ b/ezyhttp-server-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-core diff --git a/ezyhttp-server-graphql/pom.xml b/ezyhttp-server-graphql/pom.xml index 80cfae71..b56065d8 100644 --- a/ezyhttp-server-graphql/pom.xml +++ b/ezyhttp-server-graphql/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-graphql diff --git a/ezyhttp-server-jetty/pom.xml b/ezyhttp-server-jetty/pom.xml index ae9119d5..bf1f7af7 100644 --- a/ezyhttp-server-jetty/pom.xml +++ b/ezyhttp-server-jetty/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-jetty diff --git a/ezyhttp-server-management/pom.xml b/ezyhttp-server-management/pom.xml index f675f3f7..8ec4a2ca 100644 --- a/ezyhttp-server-management/pom.xml +++ b/ezyhttp-server-management/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-management ezyhttp-server-management diff --git a/ezyhttp-server-thymeleaf/pom.xml b/ezyhttp-server-thymeleaf/pom.xml index 6b49797e..9c699efe 100644 --- a/ezyhttp-server-thymeleaf/pom.xml +++ b/ezyhttp-server-thymeleaf/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-thymeleaf diff --git a/ezyhttp-server-tomcat/pom.xml b/ezyhttp-server-tomcat/pom.xml index 465de81d..b0ff0ea8 100644 --- a/ezyhttp-server-tomcat/pom.xml +++ b/ezyhttp-server-tomcat/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.8 + 1.2.9 ezyhttp-server-tomcat diff --git a/pom.xml b/pom.xml index 55051984..d5cb6a16 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0.6 ezyhttp - 1.2.8 + 1.2.9 pom ezyhttp