Play 2 - Scala FakeRequest withJsonBody
playframework
Solution
Maybe something like:
"POST createGroup with JSON" should {
"create a group and return a message" in {
implicit val app = FakeApplication()
running(app) {
val fakeRequest = FakeRequest(Helpers.POST, controllers.routes.ApplicationController.createGroup().url, FakeHeaders(), """ {"name": "New Group", "collabs": ["foo", "asdf"]} """)
val result = controllers.ApplicationController.createGroup()(fakeRequest).result.value.get
status(result) must equalTo(OK)
contentType(result) must beSome(AcceptExtractors.Accepts.Json.mimeType)
val message = Region.parseJson(contentAsString(result))
// test the message response
}
}
}
Note: The `val result` line might now be correct since I took it from a test that uses an Async controller.
Problem
I am trying to test an action on a controller. It's a rather simple action, it takes JSON and returns JSON: ``` def createGroup = Action(parse.json) { request => val name = (request.body \ "name").as[String] val collabs = (request.body \ "collabs").as[List[String]] Ok(Json.toJson( Map("status" -> "OK", "message" -> "%s created".format(name)) )) } ``` I want to verify that the JSON returned is indeed correct. How would I use FakeRequest to do this?