Request body is showing as AnyContentAsRaw with inMemory 0

五迷三道 提交于 2019-12-13 03:26:53

问题


My outgoing request looks like follows

POST ws/questions/new-question
headers List((Host,localhost), (X-Auth-Token...)
body AnyContentAsJson({"practice-question":...})

The Action to process the request should first check that the size of the body does not exceed a maximum value.

def newQuestion = silhouette.SecuredAction.async(parse.maxLength(maxAllowedBodySize, parse.anyContent)(materializer)) { 
    implicit request => {
      println("got request with body:" + request.body)
      val anyBodyErrors: Either[MaxSizeExceeded, AnyContent] = request.body
      anyBodyErrors match {
        case Left(size) => {
          Future {
            println("body size too big to handle: "+size)
            EntityTooLarge(Json.toJson(JsonResultError(messagesApi("error.entityTooLarge")(langs.availables(0)))))
          }
        }
        case Right(body) => {

          //val body:AnyContent = request.body
          val jsonBodyOption = body.asJson

I have written the following test case test the Action

 "newQuestion" should {
    "should return error if the size of the body in the request is more than the maximum allowed size" in {
      val sizeConfig = Map("maxAllowedBodySize"-> 10)
      val maxBodySizeConfig = Map("codingJedi" -> sizeConfig )
      val newConfiguration = Configuration.from(maxBodySizeConfig)
      val questionControllerTestEnv = new QuestionsControllerSpecTestEnv(Some(newConfiguration),components)
      val body =
        s"""
           |{...
           |}
        """.stripMargin

      val jsonBody = Json.parse(body)

      val request = new FakeRequest(FakeRequest("POST","ws/questions/new-question")).withAuthenticator(questionControllerTestEnv.testEnv.loginInfo)(questionControllerTestEnv.testEnv.fakeEnv).withBody(AnyContentAsJson(jsonBody))
      println("outgoing request is "+request)
      println("headers "+request.headers)
      println("body "+request.body)
      val response = questionControllerTestEnv.questionsController.newQuestion(request)


      val responseBody = contentAsJson(response)
      println(s"received response body ${responseBody}")
      val result = (responseBody \ "result").get.as[String]
      val additionalInfo = (responseBody \ "additional-info").get.as[String]
      result mustBe "error"
      additionalInfo mustBe components.messagesApi("error.entityTooLarge")(components.langs.availables(0))
    }
  }

But Play seem to be parsing the body as AnyContentAsRaw and inMemory value is 0 as can be seen by the following print statement

got request with body:Right(AnyContentAsRaw(RawBuffer(inMemory=0, backedByTemporaryFile=null)))

This is failing my test case. Why is the body showing as AnyContentAsRaw with buffer values as 0??

来源:https://stackoverflow.com/questions/57581602/request-body-is-showing-as-anycontentasraw-with-inmemory-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!