[英]How do I test multipart form data requests for file uploads in Play Framework 2.0 using Java?
我了解您可以使用Scala API执行此操作,如下所示:
https://groups.google.com/forum/?fromgroups=#!topic/play-framework/1vNGW-lPi9I
但似乎没有办法使用Java,因为FakeRequests的withFormUrlEncodedBody方法只支持字符串值?
这是API中缺少的功能还是有任何解决方法? (仅使用Java)。
对于集成测试,您可以像我一样使用apache DefaultHttpCLient:
@Test
public void addFileItem() throws Exception {
File testFile = File.createTempFile("test","xml");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(URL_HOST + "/api/v1/items/file");
MultipartEntity entity = new MultipartEntity();
entity.addPart("description", new StringBody("This is my file",Charset.forName("UTF-8")));
entity.addPart(Constants.ITEMTYPE_KEY, new StringBody("FILE", Charset.forName("UTF-8")));
FileBody fileBody = new FileBody(testFile);
entity.addPart("file", fileBody);
method.setEntity(entity);
HttpResponse response = httpclient.execute(method);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(CREATED);
}
这要求您在测试中启动服务器:
public static FakeApplication app;
public static TestServer testServer;
@BeforeClass
public static void startApp() throws IOException {
app = Helpers.fakeApplication();
testServer = Helpers.testServer(PORT, app);
Helpers.start(testServer);
}
@AfterClass
public static void stopApp() {
Helpers.stop(testServer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.