[play-framework] [2.0] How to test chunked results?

293 views
Skip to first unread message

Eric Jain

unread,
May 4, 2012, 2:51:42 PM5/4/12
to play-framework
I can check the response status and content type, but
Helpers.contentAsString(result) fails:

"Cannot extract the body content from a result of type
play.api.mvc.ChunkedResult"

Is there a simple way to verify the contents of chunked results in
unit tests?

Erem Boto

unread,
Oct 25, 2012, 5:03:38 PM10/25/12
to play-fr...@googlegroups.com
I'm coming across this same issue and also would like to know how. I am trying to read how it's done in PlayDefaultUpstreamHandler.scala, but I can't make sense of the code.

Erem Boto

unread,
Oct 25, 2012, 7:17:46 PM10/25/12
to play-fr...@googlegroups.com
I ended up writing the following function that returns the length of the chunked result. Should be pretty clear how you would use a mutable collection to return the content itself rather than the length, assuming you're writing a byte array like I am.

We wouldn't have to use this crufty var if chunks accepted something other than an Iteratee[A, Unit], and we wouldn't have to do the iteration if applying chunks returned a Promise[Iteratee[A, Unit]] rather than an Any.

  def chunkedContentLength(chunkedResult: ChunkedResult[Array[Byte]]): Int =  {

    var numBytes = 0

    val countIteratee = Iteratee.fold[Array[Byte], Unit](0) { (_, bytes) => numBytes += bytes.size }

    val promisedIteratee = chunkedResult.chunks(countIteratee).asInstanceOf[Promise[Iteratee[Array[Byte], Unit]]]

    promisedIteratee.await(5000).get.run.await(5000).get // waits until all the chunks have been retrieved

    

    numBytes

  }


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/AxfPrhLVicQJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Nick McCready

unread,
Feb 14, 2013, 11:26:48 AM2/14/13
to play-fr...@googlegroups.com
This needs to be addressed in play 2.1 as well. Getting the data out of an Iteratee seems incorrect with having to use var. This should be improved ti allow an Iteratee to return something other than Unit in a ChunkedResult.
Reply all
Reply to author
Forward
0 new messages