Sending file with POST request with HttpUnit

假如想象 提交于 2019-12-10 21:18:36

问题


we have a REST service, which we would like to test. I thought about using HttpUnit for this purpose. We sent POST request to a resource URL and after receiving the request we retrieve the file from request. In our server code we have something like this:

MultipartFormData body = request().body().asMultipartFormData();
FilePart file = body.getFile("upfile");
File pictureFile = file.getFile();

In my test I wrote:

WebConversation wc = new WebConversation();
WebRequest wr = new PostMethodWebRequest("http://linkToOurResource");
File f = new File("testFile.jpg");
wr.selectFile("upfile", f, "multipart/form-data;");
    WebResponse response = wc.getResponse(wr);

but I'm getting the following error:

Test functional.AcceptanceTests.testAddingNewClient failed: Parameter 'upfile' is not a file parameter and may not be set to a file value.

Any suggestions how to send the file in the POST request to our server?


回答1:


You might want to read the httpunit developer FAQ - just search httpunit's Unit Tests to find a fitting source code example:

https://sourceforge.net/mailarchive/forum.php?thread_name=5051BBF6.70700%40bitplan.com&forum_name=httpunit-develop



来源:https://stackoverflow.com/questions/10685883/sending-file-with-post-request-with-httpunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!