richs richs - 1 year ago 94
Scala Question

scala test play parse.mulipartFormData

Attempting to test a play function that parses a multipartFormData

def requestBulkSiteSbcActionConfirm = Action.async(parse.multipartFormData) { request => ....


The test i've created is

val temporartyFile = TemporaryFile("test")
val filePart = FilePart("key: String", "test.csv", None, temporartyFile)
val multipartFormData: MultipartFormData[TemporaryFile] = MultipartFormData(Map(),Seq(filePart), Seq())
val request: Request[AnyContentAsMultipartFormData] = FakeRequest().withHeaders(("Content-Type"->"multipart/form-data")).withMultipartFormDataBody(multipartFormData)
val pw = new PrintWriter(filePart.ref.file)
pw.write(s"$TestSiteId_1, ${AdLeft.id}, on\n")
pw.write(s"$TestSiteId_2, ${AdRight.id}, off\n")
pw.close()
val acc: Accumulator[ByteString, Result] = controller.requestBulkSiteSbcActionConfirm().apply(request)


which exceptions

- valid file sbc command list *** FAILED ***
[info] java.util.NoSuchElementException: None.get
[info] at scala.None$.get(Option.scala:347)
[info] at scala.None$.get(Option.scala:345)
[info] at play.api.mvc.BodyParsers$parse$$anonfun$multipartFormData$1.apply(ContentTypes.scala:657)
[info] at play.api.mvc.BodyParsers$parse$$anonfun$multipartFormData$1.apply(ContentTypes.scala:656)
[info] at play.api.mvc.BodyParser$$anon$8.apply(Action.scala:258)
[info] at play.api.mvc.BodyParser$$anon$8.apply(Action.scala:257)
[info] at play.api.mvc.Action$class.apply(Action.scala:103)
[info] at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:455)
[info] at controllers.SbcConfigControllerTest$$anonfun$2$$anonfun$apply$mcV$sp$1.apply$mcV$sp(SbcConfigControllerTest.scala:64)
[info] at controllers.SbcConfigControllerTest$$anonfun$2$$anonfun$apply$mcV$sp$1.apply(SbcConfigControllerTest.scala:55)

Answer Source

Some internal parts of Play still uses a global state for execution, it seems you just hit one: https://github.com/playframework/playframework/blob/2.5.x/framework/src/play/src/main/scala/play/api/mvc/ContentTypes.scala#L657

This should change with play 2.6 but meanwhile you should start an application in your test to make it work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download