【问题标题】:Sending file with POST request with HttpUnit使用 HttpUnit 发送带有 POST 请求的文件
【发布时间】:2012-05-27 23:46:51
【问题描述】:

我们有一个 REST 服务,我们想对其进行测试。我考虑过为此目的使用 HttpUnit。 我们向资源 URL 发送 POST 请求,并在收到请求后从请求中检索文件。在我们的服务器代码中,我们有这样的内容:

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

在我的测试中我写道:

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);

但我收到以下错误:

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

关于如何将 POST 请求中的文件发送到我们的服务器有什么建议吗?

【问题讨论】:

    标签: java rest testing http-unit


    【解决方案1】:

    您可能想阅读 httpunit 开发人员常见问题解答 - 只需搜索 httpunit 的单元测试即可找到合适的源代码示例:

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

    【讨论】:

      猜你喜欢
      • 2022-08-14
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 2019-12-04
      相关资源
      最近更新 更多