Skip to content

Commit

Permalink
Fix toByteBuf for streamed HttpData (#1118)
Browse files Browse the repository at this point in the history
* Fix toByteBuf for streamed HttpData
  • Loading branch information
ex0ns authored Mar 5, 2022
1 parent 8e4f6bf commit 2bbeb9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/http/HttpData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 19 additions & 6 deletions zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala
Original file line number Diff line number Diff line change
@@ -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))
}
},
),
)
}

0 comments on commit 2bbeb9b

Please sign in to comment.