Serializing Multipart Form requests for testing on Play 2.1
Asked Answered
S

2

2

I'm working on play2.1 writing a test for a post controller that uses multipart forms using the route function.

route(
  FakeRequest(POST,
    postControllerRoute().url,
    FakeHeaders(Seq(HeaderNames.CONTENT_TYPE -> Seq("multipart/form-data"))),
    body = body
  ).withAuthToken.withAdmin(adminId))

I've found that for this code to work I need to define a writeable of this type Writeable[MultipartFormData[TemporaryFile]] since my body variable is of type MultipartFormData[TemporaryFile]. I'm not sure how to serialize a multipart request or if this is even the right approach. Any suggestions?

Edited

Answer: Skip the router and use the controller directly like in the docs: http://www.playframework.com/documentation/2.1.0/ScalaFunctionalTest

Steffaniesteffen answered 21/2, 2013 at 22:8 Comment(0)
I
2
  • You have two options, searializing MultiPartFormData which is (as far as I can tell) quite tricky to do. This post might help: https://groups.google.com/forum/?fromgroups=#!topic/play-framework/MPtQlX-cWMQ
  • You can skip going though the route method. If you want to test the Action in your controller, you can simply bypass the route. See Testing your Controllers in the documentation. If you do not go through the router, you don't need to supply a Writable. It is rarely needed to go through the router which essentially let's you first write it as bytes (using Writer) and then (using the body parser) convert it back to something Play understands.
Ichthyo answered 21/2, 2013 at 22:53 Comment(3)
Well, yes but I want to test multi part form. My controller uses the body parser for MultipartFormDataSteffaniesteffen
I am sorry, yesterday my mind read FormUrlEncoded as MultipartFormData.Ichthyo
should have been obvious...I don't need to test the router to test the controller. Thanks for your nudge in the right direction. :)Steffaniesteffen
W
0

If you don't want to skip route, you need a Writeable[AnyContentAsMultipartFormData], which turns MultipartFormData[TemporaryFile] into Array[Byte].

There's a Writeable[AnyContentAsMultipartFormData] here: http://tech.fongmun.com/post/125479939452/test-multipartformdata-in-play

Weed answered 31/7, 2015 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.