无效的Json:使用播放体解析器时,由于输入结束而没有要映射的内容

时间:2017-06-27 10:53:41

标签: scala playframework

我正在尝试测试一个尝试解析请求中发送的JSON的控制器方法:

  def addRoomToProfileForTime = Action.async(parse.json[AddRoomToProfileForTimeRequest]) { request =>
    profileService.addRoomToProfileForTime(request.body.roomId, request.body.profileId, request.body.timeRange).value.map {
      case Xor.Right(_)  => Ok
      case Xor.Left(err) => BadRequest(Json.toJson(err))
    }
  }

这是表示请求的案例类:

final case class AddRoomToProfileForTimeRequest(
  roomId: Int,
  profileId: Int,
  timeRange: TimeRange
)

implicit val addRoomToProfileForTimeRequestFormat:Format[AddRoomToProfileForTimeRequest] = Json.format

此代码按预期工作,我发出如下请求:

curl -H "Content-Type: application/json" -X POST -d '{"roomId":3,"profileId":1,"timeRange":{"id":1,"fromTime":"2000-01-01T01:01","toTime":"2000-01-01T02:01"}}' http://localhost:9000/api/profiles/addRoomToProfileForTime

但我正在尝试为此方法编写一个测试(注意我使用macwire进行依赖注入,因此无法使用WithApplication

"add a room to profile for time" in new TestContext {
  val roomId = 1
  val profileId = 1
  val from = "2000-01-01T01:01"
  val to = "2000-01-01T02:01"
  val requestJson = Json.obj(
    "roomId"    -> roomId,
    "profileId" -> profileId,
    "timeRange" -> Json.obj(
      "id"       -> 1,
      "fromTime" -> from,
      "toTime"   -> to
    )
  )

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()

  val fakeReq = FakeRequest(Helpers.POST, "api/profiles/addRoomToProfileForTime")
    .withHeaders(CONTENT_TYPE -> "application/json")
    .withJsonBody(requestJson)
  val result = profileController.addRoomToProfileForTime()(fakeReq).run

  val content  = contentAsString(result)
  println(content)
  status(result) must equalTo(OK)
}

但是,此测试因播放错误请求而失败:

<body>                                             
    <h1>Bad Request</h1>                                

    <p id="detail">
        For request 'POST api/profiles/addRoomToProfileForTime' [Invalid Json: No content to map due to end-of-input at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@37d14073; line: 1, column: 0]]
    </p>

</body>

如果我使用request.body.asJson解析JSON,则该方法的行为与预期一致。它只使用上面的body解析器方法,我得到了这个错误。

2 个答案:

答案 0 :(得分:15)

简短的回答是:在控制器测试的FakeRequest上,使用withBody方法代替withJsonBody

我也遇到过这个问题,我尴尬地花了好几个小时,直到我弄清楚了。答案很长,FakeRequest withJsonBody会返回FakeRequest[AnyContentAsJson],因为您的控制器期待JsValue AnyContentAsJson),当您在操作上调用apply()时,它无法匹配此应用方法,这是您想要的方法:

def apply(request: Request[A]): Future[Result]

而不是点击此apply方法:

def apply(rh: RequestHeader): Accumulator[ByteString, Result]

因此,由于您未将任何字节传递给累加器,因此您会收到意外的end-of-input错误消息。

答案 1 :(得分:0)

另一个原因可能是未在Postman应用程序中设置Content-Length。我错误地禁用了它,却忘记启用它。 enter image description here