6

Playing MultipartFormData: A basic example to handle and test MultipartFormData...

 3 years ago
source link: https://blog.knoldus.com/playing-multipartform/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Reading Time: 2 minutes

Playing MultipartFormData

The following blog and attached code represent an example to upload file using MultipartFormData request and Testing it into Play Application.

A basic example to handle and test MultipartFormData request in Play Framework 2.3.8

The standard way to upload files in a web application is to use a form with a special multipart/form-data encoding, which lets you mix standard form data with file attachment data. Please note: the HTTP method used to submit the form must be POST (not GET).


Code in action for Controller and Service


  val uploadService: UploadService

  def upload = Action(parse.multipartFormData) { implicit request =>
    val result = uploadService.uploadFile(request)
    Redirect(routes.Application.index).flashing("message" -> result)
  }
  /**
   * Get file from the request and move it in your location
   *
   * @param request
   * @return
   */
  def uploadFile(request: Request[MultipartFormData[TemporaryFile]]): String = {
    log.error("Called uploadFile function" + request)
    request.body.file("file").map { picture =>
      import java.io.File
      val filename = picture.filename
      val contentType = picture.contentType
      log.error(s"File name : $filename, content type : $contentType")
      picture.ref.moveTo(new File(s"/tmp/$filename"))
      "File uploaded"
    }.getOrElse {
      "Missing file"
    }
  }


Test Code for Controller and Service


ApplicationSpec.scala

"should be valid" in new WithApplication {
  val request = mock[Request[MultipartFormData[TemporaryFile]]]
  mockedUploadService.uploadFile(request) returns "File Uploaded"
  val result: Future[Result] = TestController.upload().apply(request)
  status(result) must equalTo(SEE_OTHER)
}

UploadServiceSpec.scala

"UploadService" should {
    "uploadFile returns (File uploaded)" in new WithApplication {
      val files = Seq[FilePart[TemporaryFile]](FilePart("file", "UploadServiceSpec.scala", None, TemporaryFile("file", "spec")))
      val multipartBody = MultipartFormData(Map[String, Seq[String]](), files, Seq[BadPart](), Seq[MissingFilePart]())
      val fakeRequest = FakeRequest[MultipartFormData[Files.TemporaryFile]]("POST", "/", FakeHeaders(), multipartBody)
      val success = UploadService.uploadFile(fakeRequest)
      success must beEqualTo("File uploaded")
    }

    "uploadFile returns (Missing file)" in new WithApplication {
      val files = Seq[FilePart[TemporaryFile]]()
      val multipartBody = MultipartFormData(Map[String, Seq[String]](), files, Seq[BadPart](), Seq[MissingFilePart]())
      val fakeRequest = FakeRequest[MultipartFormData[Files.TemporaryFile]]("POST", "/", FakeHeaders(), multipartBody)
      val success = UploadService.uploadFile(fakeRequest)
      success must beEqualTo("Missing file")
    }
}

Build and Run the application :-


  • To run the Play Framework, you need JDK 6 or later
  • Install Typesafe Activator if you do not have it already. You can get it from here
  • Execute ./activator clean compile to build the product
  • Execute ./activator run to execute the product
  • playing-multipartform should now be accessible at localhost:9000

Test the application with code coverage


  • Execute $ ./activator clean coverage test to test
  • Execute $ ./activator coverageReport to generate coverage report

File Upload Form

Test Coverage


References :-


Code on GitHub


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK