1

I have an API built using Spray that handles file uploads. I am trying to write a test for the upload functionality but I'm not getting anywhere fast. I'm nots sure how to structure the test to simulate a file upload. I have the following test...

"Valid POST Requests should return success" in {
  Post("/upload", HttpEntity(MediaTypes.`multipart/form-data`, """{"filename":"a.wav"}""")) ~> 
  sealRoute(uploadRoute) ~> check {
    response.status should be equalTo OK
    responseAs[String] === "..."
  }
}

Running this produces the following error message...

Content-Type with a multipart media type must have a non-empty 'boundary' parameter' is not equal to ...

This seems like an error message similar to how to mock POST/Upload requests using apache bench where you have to specify a post file and the boundary to separate the form items. I was hoping for something closer to how CURL works.

Either way, can anyone point me in the right direction as to how I correctly structure such a test?

Thanks

fatlog
  • 1,182
  • 2
  • 14
  • 28

2 Answers2

2

So I managed to get this working by cobbling together some code from a variety of posts I found - primarily posts relating to using spray-client to do file uploads. Probably not the prettiest but works for me! :)

"Valid POST Requests should return success" in {
  val file = new File("a.wav")
  val httpEntity = HttpEntity(MediaTypes.`multipart/form-data`, HttpData(file)).asInstanceOf[HttpEntity.NonEmpty]
  val formFile = FormFile("file", httpEntity)
  val mfd = MultipartFormData(Seq(BodyPart(formFile, "file")))
  Post("/upload", mfd) ~> sealRoute(uploadRoute) ~> check {
    response.status should be equalTo OK
    body.contentType.toString() === "application/json; charset=UTF-8"
    responseAs[String] === "Success!"
  }
}
fatlog
  • 1,182
  • 2
  • 14
  • 28
0

I have the same issue, or question.

Try adding a boundary by doing:

Post("/upload", HttpEntity(MediaTypes.multipart/form-data.withBoundary("-somerandomboundary"), """{"filename":"a.wav"}""")) ~>

Although, you might face the next bump I face, which is an error saying it requires a start boundary.

RDGary
  • 1
  • 1
  • Hi, I tried that as I had seen issues before but like you said just ran into another issue. Anyway, I actually just got this working (for me anyway!) and will post my solution as an answer – fatlog May 21 '15 at 11:05