How to check actions pending file upload in Play Framework (version 2.0 using Scala)?

Writing functional tests, part of the documentation is rather scarce and does not contain details about the full presentation of the values ​​of the layout form. I somehow (cannot remember how / where) determined that you can send the base values ​​of the form (mocking the POST request) by passing Map to FakeRequest as follows:

 val Some(result) = routeAndCall(FakeRequest(POST, "/path/to/test", FakeHeaders(), Map("postedVariable" -> Seq("and a value")))) 

However, this is not like the case of a β€œuploaded” file.

+6
source share
1 answer

Our file upload tests look something like this:

 val tempFile = TemporaryFile(new java.io.File("/tmp/the.file")) val part = FilePart[TemporaryFile](key = "image", filename = "the.file", contentType = Some("image/jpeg"), ref = tempFile) val formData = MultipartFormData(dataParts = Map(), files = Seq(part), badParts = Seq(), missingFileParts = Seq()) val result = routeAndCall(FakeRequest(POST, "/path/to/test", FakeHeaders(), formData)) 

where "image" is the name of the HTML form element in which you expect to find the contents of the file.

If you use BodyParsers.maxLength to limit download size, you can replace formData with Right(formData)

+10
source

All Articles