diff --git a/zio-http/src/main/scala/zhttp/http/HttpData.scala b/zio-http/src/main/scala/zhttp/http/HttpData.scala index 2b677805ed..ba945b18a3 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpData.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpData.scala @@ -174,12 +174,14 @@ 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] = - ZIO.attempt(Unpooled.wrappedBuffer(asciiString.array())) + ZIO.attempt(encode) /** * Encodes the HttpData into a Stream of ByteBufs. Takes in ByteBufConfig to @@ -193,7 +195,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 { diff --git a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala index d54006c001..69634a6860 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} @@ -266,7 +267,13 @@ object ServerSpec extends HttpRunnableSpec { equalTo(c), ) } - } + } + + test("FromASCIIString: toHttp") { + checkAll(Gen.asciiString) { payload => + val res = HttpData.fromAsciiString(AsciiString.cached(payload)).toHttp.map(_.toString(HTTP_CHARSET)) + assertM(res.run())(equalTo(payload)) + } + } } def serverErrorSpec = suite("ServerErrorSpec") {