我有 play scala v2.3 应用程序。我正在尝试通过使用 FakeRequest 发送 json 字符串来创建 Controller 测试,如下所示:
class ApplicationSpec extends Specification {
"Application" should {
"Create Bob Test" in new WithApplication {
val jsonStr = """{"text": "hi bob"}"""
val result = route(FakeRequest(POST, "/bob")
.withHeaders("Content-Type" -> "application/json")
.withBody(jsonStr)
).get
status(result) === OK
}
}
}
Controller :
object Application extends Controller {
def bob = Action.async { request =>
println("request.headers: " + request.headers)
println("request.body: " + request.body)
println("request.body.asJson: " + request.body.asJson)
request.body.asJson.map { json =>
// do something with the json
Future.successful(Ok)
}.getOrElse(Future.successful(BadRequest))
}
}
当我运行测试时,它失败了,这是打印的内容:
request.headers: ArrayBuffer((Content-Type,List(text/plain; charset=utf-8)))
request.body: AnyContentAsText({"text": "hi bob"})
request.body.asJson: None
所以发送的 Content-Type
header 不是 application/json
尽管我已经用 application/json
设置了 header 。可能正因为如此,request.body.asJson
返回 None。
有人知道怎么解决吗?
注意:我知道我可以在 FakeRequest 上使用 .withJsonBody(Json.parse(jsonStr))
并且它会成功,但是通过这种方式我无法发送损坏或无效的 json 字符串负测试用例,因为 withJsonBody
接受 JsValue,json 字符串必须首先使用 Json.parse
进行转换。
最佳答案
默认情况下,Content-Type
header 会被框架覆盖。 blog 中给出了解决方法
在你的情况下,这应该有效
route(FakeRequest(POST, "/bob", FakeHeaders(Seq(CONTENT_TYPE->Seq("application/json"))), jsonStr))(Writeable(_.getBytes, None)).get
对于多个测试,可以在开始时为writable
创建一个隐式,而不需要在每个测试中都通过:
implicit val wString: Writeable[String] = Writeable(_.getBytes, None)
关于json - 如何在 Play 框架 v2.x 中不使用 withJsonBody 为 FakeRequest 发送 json 字符串主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968599/