Testing AJAX File Download

I want to test file upload in my application. The download itself is performed as described in the Direct file download section at http://www.playframework.org/documentation/2.0/JavaFileUpload .

I am using the latest version of Play20 and building it as described here .

My current code looks like this, but obviously this part is missing, which adds a test file to the request:

Test.java

FakeRequest request = fakeRequest(POST, "/measurement/123/file");
// how to append test file to this request?    
Result result = routeAndCall(request);
assertOK(result);

Controller.java

public static uploadFile() {
    RequestBody body = request().body();
    if (body != null) {
        RawBuffer rawBuffer = body.asRaw();
        if (rawBuffer != null) {
            File uploadedFile = rawBuffer.asFile();
            // ...
        }
    }
    return ok();
}
+3
source share
2 answers

Play 2 Java , UploadFakeRequest, FakeRequest. RawBody - :

 RawBuffer raw = new RawBuffer(1000, data.getBytes()); // No clue what is the correct value here...
 AnyContentAsRaw content = new AnyContentAsRaw(raw);
 fake = new play.api.test.FakeRequest(POST, fake.path(), new play.api.test.FakeHeaders(Scala.asScala(map)), content, "127.0.0.1");
+1

FakeRequest :

case class FakeRequest[A](method: String, uri: String, headers: FakeHeaders, body: A) extends Request[A] 

POST, . RFC POST.

0

All Articles