开发者

testing POST request in playframework

开发者 https://www.devze.com 2023-03-19 17:53 出处:网络
My controller method is something like this public static void addItem(byte[] xmlFile) { ... //process file

My controller method is something like this

public static void addItem(byte[] xmlFile) {
   ... //process file
}

and my ApplicationTest file

@Test
public void addItem() {
  Request request = newRequest();
  request.url = "/item";
  request.encoding = APPLICATION_X_WWW_FORM_URLENCODED;
  request.body = new ByteArrayInputStream(xmlFileBytes)
  Response response = POST(request, "/item")
.....
}

When i run this i get exceptions ..开发者_C百科 with root cause unsupported encoding: application/x-www-form-urlencoded

How does one resolve this...


I guess this is one more case of not knowing APIs.... :)

The FunctionalTest provide a POST method where one can submit a map of Files with a string key ...

POST(java.lang.Object url, Map<String,String> parameters,Map<String,File> files)

Sends a POST request to the application under tests as a multipart form. Designed for file upload testing.

My Solution ...

Request request = newRequest();
request.url = "/item";
Map<String, String> paramMap = Maps.newHashMap();
Map<String, File> fileMap = new HashMap<String, File>();
fileMap.put("xmlFile", new File("test/item.xml);
Response response = POST("/item", paramMap, fileMap);
assertIsOk(response)


The usual way I handle this is by using "multipart/form-data" on the form and a File argument to the controller. That's also the documented way, as far as I know...

0

精彩评论

暂无评论...
验证码 换一张
取 消