我想在我的应用程序中测试文件上传。上传本身的处理方式如http://www.playframework.org/documentation/2.0/JavaFileUpload上直接上传文件部分所述。
我正在使用最新的Play20版本,并按照here所述进行构建。
我当前的代码看起来像这样,但显然缺少部分,它会在请求中添加一个测试文件:
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();
}
答案 0 :(得分:1)
我的Play 2 Java文件上传测试解决方案是创建一个扩展FakeRequest的UploadFakeRequest。在课堂上我有自己的withRawBody方法,有类似的东西:
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 :(得分:0)
根据source code,FakeRequest调用的签名是:
case class FakeRequest[A](method: String, uri: String, headers: FakeHeaders, body: A) extends Request[A]
您应该能够检查将文件上传到应用程序的POST请求的正文,并将该正文作为参数复制到测试代码中。另一种方法是在多部分POST上按照RFC description构建它。