我试图测试POST json来播放控制器。控制器本身可以工作,我可以使用httpie:
进行POSThttp --print="HhBb" POST localhost:9000/review some="Its a value"
我试图用以下方法测试:
"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")
}
但我在html正文中出错:
For request 'POST /review' [Host not allowed: ]
即时播放菜鸟,我是否必须以某种方式配置此应用使用的路由?我曾尝试使用GuiceApplicationBuilder配置应用程序,因为FakeApplication已被弃用,但无法使其正常工作。
控制器:
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))
}