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

后端 未结 7 1808
猫巷女王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:01

    Following EEColor's suggestion, I got the following to work:

    "Upload Photo" in {
    
    
        val file = scala.io.Source.fromFile(getClass().getResource("/photos/DSC03024.JPG").getFile())(scala.io.Codec.ISO8859).map(_.toByte).toArray
    
        val data = new MultipartFormData(Map(), List(
        FilePart("qqfile", "DSC03024.JPG", Some("image/jpeg"),
            file)
        ), List())
    
        val result = controllers.Photo.upload()(FakeRequest(POST, "/admin/photos/upload",FakeHeaders(),data))
    
        status(result) must equalTo(CREATED)
        headers(result) must haveKeys(LOCATION)
        contentType(result) must beSome("application/json")      
    
    
      }
    

提交回复
热议问题