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 JsonStreamParser.head[J] #36

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import org.apache.pekko
import pekko.actor.ActorSystem
import pekko.http.scaladsl.marshalling.Marshal
import pekko.http.scaladsl.model.HttpCharsets.`UTF-8`
import pekko.http.scaladsl.model.HttpEntity.ChunkStreamPart
import pekko.http.scaladsl.model.MediaTypes.`application/json`
import pekko.http.scaladsl.model.{HttpEntity, RequestEntity, UniversalEntity}
import pekko.http.scaladsl.model.{HttpEntity, RequestEntity}
import pekko.http.scaladsl.unmarshalling.Unmarshal
import pekko.stream.scaladsl.{Keep, Sink, Source}
import pekko.util.ByteString
Expand Down Expand Up @@ -149,6 +150,15 @@ class JsonSupportSpec
}
}

"A valid, lazily streamed chunked json entity" should {
val lazyChunkedEntity = mkEntity(goodJson, chunked = true)
"produce the proper type" in {
Unmarshal(lazyChunkedEntity).to[Foo].map {
_ shouldBe foo
}
}
}

"A complete, lazily streamed json entity with superfluous content" should {
val entity = mkEntity(goodJson + incompleteJson)
"produce the proper type" in {
Expand Down Expand Up @@ -307,12 +317,17 @@ class JsonSupportSpec
}
}

def mkEntity(s: String, strict: Boolean = false): UniversalEntity =
def mkEntity(s: String, strict: Boolean = false, chunked: Boolean = false): HttpEntity =
if (strict) {
HttpEntity(`application/json`, s)
} else {
val source = Source.fromIterator(() => s.grouped(8).map(ByteString(_)))
HttpEntity(`application/json`, s.length.toLong, source)
if (chunked) {
val source = Source.fromIterator(() => s.grouped(1).map(bs => ChunkStreamPart(ByteString(bs))))
HttpEntity.Chunked(`application/json`, source)
} else {
val source = Source.fromIterator(() => s.grouped(8).map(ByteString(_)))
HttpEntity(`application/json`, s.length.toLong, source)
}
}

override def afterAll(): Unit = {
Expand Down
Loading