jeudi 24 novembre 2016

How to test endpoint using BodyParsers.parse.raw

I am trying to test endpoint which use a BodyParsers.parse.raw in Play 2.5.

So this is my controller endpoint:

def handle = Action.async(BodyParsers.parse.raw) { implicit request =>
  val body = request.body.asBytes().get.toArray
  if (body.length > 0) {
    Future.successful(Ok)
  } else {
    Future.successful(BadRequest)
  }
}

Test:

"test" in {
   val content = Json.obj("field" -> "value")
   val result = controller.handle()(
    FakeRequest(
      "POST",
      "/",
      Headers.apply((HeaderNames.CONTENT_TYPE, JSON)),
      content))

   status(result.run()(materializer)) mustBe OK
}

The test fails. when debugging it looks like:

request.body.asBytes().get.toArray

is an empty array, although I set the body in the fake request. Not sure what I am missing. something with the materializer?

Any idea?

Aucun commentaire:

Enregistrer un commentaire