My controller
checks that size of incoming json and should reject it if the size is large
implicit request:SecuredRequest[JWTEnv,Either[MaxSizeExceeded,AnyContent]] => { //Action[A] created request of type A and passes it to the block passed to Action
println("got request with body:" + request.body)
println("got content header headers "+request.request.headers.get("Content-Type"))
//request.request.
val anyBodyErrors:Either[MaxSizeExceeded,AnyContent] = request.body
anyBodyErrors match {
case Left(size:MaxSizeExceeded) => {
Future {
println("body size too big to handle: "+size)
EntityTooLarge(Json.toJson(JsonResultError(messagesApi("error.entityTooLarge")(langs.availables(0)))))
}
}
case Right(body:AnyContentAsRaw)=>{
Future {
println("body was raw. send application/json header: "+body.raw.size)
EntityTooLarge(Json.toJson(JsonResultError("body was raw. send application/json header")))
}
}
case Right(body:AnyContent) => {
// val body:AnyContent = request.body
val jsonBodyOption:Option[JsValue] = body.asJson
When I run the unit test for the controller, I get error Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')...
I think the Controller is unable to pick the json
body. Following is the unit test.
"newQuestion" should {
"should return error if the size of the body in the request is more than the maximum allowed size" in {
//val testEnv = new QuestionsControllerSpecTestEnv(components=components)
val sizeConfig = Map("maxAllowedBodySize"-> 10)
val maxBodySizeConfig = Map("codingJedi" -> sizeConfig )
val newConfiguration = Configuration.from(maxBodySizeConfig)
val testEnv = new QuestionsControllerSpecTestEnv(Some(newConfiguration),components)
val body =
s"""
|{
| "practice-question":{
| "description": "some description",
| "hints": ["hint1","hint2"],
| "image": ["image1 data","image2 data","image3 data"],
| "success-test": "success test",
| "fail-test": "fail test",
| "tags": ["${testEnv.tag}","${testEnv.tag}"],
| "title":"some title",
| "answer": "some answer",
| "references":["ref1","ref2"]
| }
|}
""".stripMargin
val jsonBody = Json.parse(body)
println("jsBody is "+jsonBody)
val jsResultBody = Json.fromJson[Question](jsonBody)(questionReads)
println("jsresult is "+jsResultBody)
val request = new FakeRequest(FakeRequest("POST","ws/questions/new-question")).withHeaders(CONTENT_TYPE -> "application/json").withAuthenticator(testEnv.testEnv.loginInfo)(testEnv.testEnv.fakeEnv).withJsonBody(jsonBody)
println("outgoing request is "+request)
println("headers "+request.headers)
println("body "+request.body)
val response = testEnv.questionsController.newQuestion(request)
//val r1 = response.run()
//materializer is getting picked from TestImplicits
val responseBody = contentAsJson(response) //(Timeout(Duration(5000,"millis")),TestEnvImplicits.mat)
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))
}
}
Why is the controller unable to find the json body?