Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: update implementation of FromAsciiString.encode #1175

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions zio-http/src/main/scala/zhttp/http/HttpData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion zio-http/src/test/scala/zhttp/service/ServerSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zhttp.service

import io.netty.util.AsciiString
import zhttp.html._
import zhttp.http._
import zhttp.internal.{DynamicServer, HttpGen, HttpRunnableSpec}
Expand Down Expand Up @@ -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") {
Expand Down