From 2bbeb9ba324e2dc18366ccd54ba571b70c104f45 Mon Sep 17 00:00:00 2001 From: ex0ns Date: Sat, 5 Mar 2022 05:12:36 +0100 Subject: [PATCH] Fix toByteBuf for streamed HttpData (#1118) * Fix toByteBuf for streamed HttpData --- .../src/main/scala/zhttp/http/HttpData.scala | 2 +- .../test/scala/zhttp/http/HttpDataSpec.scala | 25 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/HttpData.scala b/zio-http/src/main/scala/zhttp/http/HttpData.scala index 6959942d3e..e04f554419 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpData.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpData.scala @@ -95,7 +95,7 @@ object HttpData { case HttpData.BinaryStream(stream) => stream .asInstanceOf[ZStream[Any, Throwable, ByteBuf]] - .fold(Unpooled.compositeBuffer())((c, b) => c.addComponent(b)) + .fold(Unpooled.compositeBuffer())((c, b) => c.addComponent(true, b)) case HttpData.RandomAccessFile(raf) => effectBlocking { val fis = new FileInputStream(raf().getFD) diff --git a/zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala index 44da146fe6..a25545429c 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala @@ -1,16 +1,29 @@ package zhttp.http +import zio.stream.ZStream import zio.test.Assertion.equalTo -import zio.test.{DefaultRunnableSpec, assertM} +import zio.test.{DefaultRunnableSpec, Gen, assertM, checkAllM} import java.io.File object HttpDataSpec extends DefaultRunnableSpec { // TODO : Add tests for othe HttpData types override def spec = - suite("HttpDataSpec")(suite("Test toByteBuf")(testM("HttpData.fromFile") { - val file = new File(getClass.getResource("/TestFile.txt").getPath) - val res = HttpData.fromFile(file).toByteBuf.map(_.toString(HTTP_CHARSET)) - assertM(res)(equalTo("abc\nfoo")) - })) + suite("HttpDataSpec")( + suite("Test toByteBuf")( + testM("HttpData.fromFile") { + val file = new File(getClass.getResource("/TestFile.txt").getPath) + val res = HttpData.fromFile(file).toByteBuf.map(_.toString(HTTP_CHARSET)) + assertM(res)(equalTo("abc\nfoo")) + }, + testM("HttpData.fromStream") { + checkAllM(Gen.anyString) { payload => + val stringBuffer = payload.toString.getBytes(HTTP_CHARSET) + val responseContent = ZStream.fromIterable(stringBuffer) + val res = HttpData.fromStream(responseContent).toByteBuf.map(_.toString(HTTP_CHARSET)) + assertM(res)(equalTo(payload)) + } + }, + ), + ) }