From d667f6bfe0d45e6312e7c81155c95f9dcf6ddd28 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Fri, 1 Apr 2022 15:27:44 +0400 Subject: [PATCH 1/2] fix: update implementation of `FromAsciiString.encode` --- zio-http/src/main/scala/zhttp/http/HttpData.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/HttpData.scala b/zio-http/src/main/scala/zhttp/http/HttpData.scala index a22b250262..3a1c1b2bb6 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpData.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpData.scala @@ -174,11 +174,13 @@ object HttpData { private[zhttp] case class FromAsciiString(asciiString: AsciiString) extends Complete { + private def encode: ByteBuf = Unpooled.wrappedBuffer(asciiString.array()) + /** * Encodes the HttpData into a ByteBuf. Takes in ByteBufConfig to have a * more fine grained control over the encoding. */ - override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = Task(Unpooled.wrappedBuffer(asciiString.array())) + override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = Task(encode) /** * Encodes the HttpData into a Stream of ByteBufs. Takes in ByteBufConfig to @@ -192,7 +194,7 @@ object HttpData { * in certain cases. Takes in ByteBufConfig to have a more fine grained * control over the encoding. */ - override def toHttp(config: ByteBufConfig): Http[Any, Throwable, Any, ByteBuf] = ??? + override def toHttp(config: ByteBufConfig): Http[Any, Throwable, Any, ByteBuf] = Http.attempt(encode) } private[zhttp] final case class BinaryChunk(data: Chunk[Byte]) extends Complete { From a74823e61a6c05e32f3e00329bcf3627544e3435 Mon Sep 17 00:00:00 2001 From: amitsingh Date: Fri, 1 Apr 2022 17:13:52 +0530 Subject: [PATCH 2/2] Add test for toHttp FromASCIIString --- zio-http/src/test/scala/zhttp/service/ServerSpec.scala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala index f838d26730..dfbb1a0862 100644 --- a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala @@ -1,5 +1,6 @@ package zhttp.service +import io.netty.util.AsciiString import zhttp.html._ import zhttp.http._ import zhttp.internal.{DynamicServer, HttpGen, HttpRunnableSpec} @@ -267,7 +268,13 @@ object ServerSpec extends HttpRunnableSpec { equalTo(c), ) } - } + } + + testM("FromASCIIString: toHttp") { + checkAllM(Gen.anyASCIIString) { payload => + val res = HttpData.fromAsciiString(AsciiString.cached(payload)).toHttp.map(_.toString(HTTP_CHARSET)) + assertM(res.run())(equalTo(payload)) + } + } } def serverErrorSpec = suite("ServerErrorSpec") {