Writing a test case for file uploads in Play 2.1 and Scala

后端 未结 7 1802
猫巷女王i
猫巷女王i 2021-01-02 05:39

I found the following question/answer:

Test MultipartFormData in Play 2.0 FakeRequest

But it seems things have changed in Play 2.1. I\'ve tried adapting the

7条回答
  •  有刺的猬
    2021-01-02 06:00

    In Play 2.6.x you can write test cases in the following way to test file upload API:

    class HDFSControllerTest extends Specification {
      "HDFSController" should {
        "return 200 Status for file Upload" in new WithApplication {
    
          val tempFile = SingletonTemporaryFileCreator.create("txt","csv")
          tempFile.deleteOnExit()
    
          val data = new MultipartFormData[TemporaryFile](Map(),
          List(FilePart("metadata", "text1.csv", Some("text/plain"), tempFile)), List())
    
          val res: Option[Future[Result]] = route(app, FakeRequest(POST, "/api/hdfs").withMultipartFormDataBody(data))
          print(contentAsString(res.get))
          res must beSome.which(status(_) == OK)
       }
      }
    }
    

提交回复
热议问题