mardi 11 avril 2017

How to test POST json on playframework controller

im trying to test POST json to play controller. Controller itself works, I can make a POST using httpie:

http --print="HhBb" POST localhost:9000/review some="Its a value"

Im trying to test that using:

"should post review" in {
  implicit val app = FakeApplication()
  val controller = new ReviewController

  val request = FakeRequest(POST, "/review")
  val home: Future[Result] = route(app, request).get

  contentAsString(home) must be ("asd")

}

but I get error in html body:

For request 'POST /review' [Host not allowed: ]

im play noob, do I have to somehow configure which routes this apps uses? Ive tried to configure app with GuiceApplicationBuilder since FakeApplication is deprecated, but couldnt make it work either.

Controller:

 def postReview = Action(parse.json) { implicit request =>

implicit val reviewReads = Json.reads[Review]

val body = request.body
println("Json: " + body)

val review = Json.fromJson[Review](request.body)
println("Json-body: " + Json.parse(body.toString))

review match{
  case JsSuccess(r:Review, path: JsPath) => {
    println(s"Review:  $r")
    reviewActor ! review
  }
  case e: JsError => println("Errors: "+ JsError.toJson(e).toString())
}


Ok(views.html.index(request.body.toString))
}

Aucun commentaire:

Enregistrer un commentaire