Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert Iteratee to Result

I am writing test-cases for my play application controller, and I am having trouble getting the action result.

val jsonresult = UserController.importOPML()( 
   FakeRequest(POST, "/user/import-opml",FakeHeaders(),data)
   .withCookies(cookie) 
)

this would work only when the Action is specified parse.multipartFormData, if it is changed to parse.json

type mismatch; found : play.api.libs.iteratee.Iteratee[Array[Byte],play.api.mvc.SimpleResult] required: scala.concurrent.Future[play.api.mvc.SimpleResult]

I don't know why , so I changed to

val Some(jsonresult ) = route( request )

this time the compilation can pass, but my Authentication stub cannot pass anymore. what is causing that weird error ? and if work with route, why the cookie didn't work.

like image 367
zinking Avatar asked Apr 26 '14 13:04

zinking


1 Answers

This problem arises because play.api.mvc.Action[A] contains these two apply methods:

// What you're hoping for
def apply(request: Request[A]): Future[Result]

// What actually gets called
def apply(rh: RequestHeader): Iteratee[Array[Byte], Result]

Request[A] extends RequestHeader, so the A in this case makes all the difference. If it doesn't match the action, you'll call the wrong method.

If your action is a Action[AnyContent], then you must pass a Request[AnyContent] - Request[AnyContentAsJson] will work, but FakeRequest[JsValue] won't, because only the former is a Request[AnyContent].

When you use ActionBuilder with a BodyParser[A], you create an Action[A]. As a result, you'll need a Request[A] to test, which means that the type of data in your question is critically important.

  • parse.json returns a BodyParser[JsValue], so data must be a JsValue
  • parse.multipartFormData returns a BodyParser[MultipartFormData[TemporaryFile]], so data must be multi-part form data.

(Note: It's been a while since you asked this question, so I'm answering it using Play 2.3, rather than the 2.2 that you were using when it was asked.)

like image 116
tjdett Avatar answered Sep 30 '22 11:09

tjdett