Reputation: 769
I am trying to test a controller to upload files. The controller receives a multipartform request.
In the test I am creating a FakeRequest this way
val tempFile = play.api.libs.Files.SingletonTemporaryFileCreator.create("prefix", "txt")
val file = FilePart("upload", "hello.txt", Option("text/plain"), tempFile)
val controller = new LoadController(controllerComponents)
val formData = MultipartFormData(
dataParts = Map(),
files = Seq(file),
badParts = Seq())
val response = controller.upload.apply(FakeRequest(POST, "/upload").
withHeaders(HeaderNames.CONTENT_TYPE -> "multipart/form-data; boundary=------------------------968e587c4173725c").
withMultipartFormDataBody(formData))
The controller receives the file to upload in the upload
key. If I test the controller with curl, postman or another rest client it works, but in the test I am getting always the message [Unexpected end of input]
as if the upload
key was empty.
Thanks in advance
Upvotes: 1
Views: 529
Reputation: 96
I faced the same issue and found out that you need to set the dataParts
property of MultipartFormData
:
val formData = MultipartFormData(
dataParts = Map("" -> Seq("dummydata")),
files = Seq(file),
badParts = Seq())
This way I also didn't have to set the content-type/boundary header.
Upvotes: 3