问题
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