gpt4 book ai didi

java - 如何在 Java 中使用 Httpunit 发送多部分请求

转载 作者:搜寻专家 更新时间:2023-10-31 20:28:16 25 4
gpt4 key购买 nike

我本质上是在问完全相同的问题 here .如您所见,没有可靠的答案。我想要做的就是使用 HTTPUnit 发送一个文件来测试 java servlet。

所以,我有一个带有这段代码的 Java Servlet(经过简化):

@WebServlet("/properties/StorePropertyAttachment")
@MultipartConfig
public class StorePropertyAttachment {

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
final Part p = req.getPart("propertyImage");
....
}
}

这是我的测试用例的重要部分:

    ServletRunner servletRunner = new ServletRunner();
servletRunner.registerServlet("StorePropertyAttachment", StorePropertyAttachment.class.getName());

WebRequest webRequest = new PostMethodWebRequest(WEB_REQUEST_BASE_URL + STORE_PROPERTIES_ENDPOINT);
UploadFileSpec spec = new UploadFileSpec(new File("C:/test.jpg"), "multipart/form-data");
webRequest.setParameter("propertyImage", new UploadFileSpec[] {spec});
^^^^^ line 68 ^^^^^

ServletUnitClient servletClient = servletRunner.newClient();
WebResponse webResponse = servletClient.getResponse(webRequest);

当我运行它时,我得到这个错误:

com.meterware.httpunit.IllegalNonFileParameterException: Parameter 'propertyImage' is not a file parameter and may not be set to a file value.
at com.meterware.httpunit.WebRequest.setParameter(WebRequest.java:232)
at com.amsgeo.mspworkmanager.services.properties.PropertyAttachmentTest.test(PropertyAttachmentTest.java:68)
....

如果我将第 68 行更改为此(一个普通参数),只是为了踢球:

webRequest.setParameter("propertyImage", "some string");

我得到这个错误(从我的 servlet 中我的方式):

java.lang.AbstractMethodError: com.meterware.servletunit.ServletUnitHttpRequest.getPart(Ljava/lang/String;)Ljavax/servlet/http/Part;
at com.amsgeo.mspworkmanager.services.properties.StorePropertyAttachment.doPost(StorePropertyAttachment.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at com.amsgeo.webapi.services.ServiceStub.service(ServiceStub.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at com.meterware.servletunit.InvocationContextImpl.service(InvocationContextImpl.java:76)
at com.meterware.servletunit.ServletUnitClient.newResponse(ServletUnitClient.java:126)
at com.meterware.httpunit.WebClient.createResponse(WebClient.java:647)
at com.meterware.httpunit.WebWindow.getResource(WebWindow.java:220)
at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:181)
at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158)
at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122)
at com.amsgeo.mspworkmanager.services.properties.PropertyAttachmentTest.testNoParam(PropertyAttachmentTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....

我不知道为什么它不让我添加文件。

有什么建议吗??

编辑:

我正在尝试使用本地 html 文件中的表单提交此文件。我正在成功加载表单,但收到 404。这是我的表单声明。

<form method="POST" action="http://localhost/StorePropertyAttachment" enctype="multipart/form-data" name="propertyImageTest">
<input type="file" name="propertyImage" />
<input type="submit" />
</form>

更新的测试代码:

    ServletRunner servletRunner = new ServletRunner();
servletRunner.registerServlet("StorePropertyAttachment", StorePropertyAttachment.class.getName());

WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("file:/C:/test.html");
WebResponse response = conversation.getResponse(request);
WebForm form = response.getFormWithName("propertyImageTest");
UploadFileSpec uploadFileSpec = new UploadFileSpec(new File("C:/test.jpg"), "image/jpeg");
form.setParameter("propertyImage", new UploadFileSpec[] {uploadFileSpec});

WebResponse webResponse = form.submit();

最佳答案

第三个 UploadFileSpec 构造函数参数不应该是内容类型而不是消息类型吗?在你的情况下是“图像/jpeg”。

你需要一个WebForm

WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://your-site-to-test.com/path-to-your-upload-form");
WebResponse response = conversation.getResponse(request);
WebForm form = response.getFormWithName("stockImageUpload");
UploadFileSpec uploadFileSpec = new UploadFileSpec("test.jpg",new File("C:/test.jpg"), "image/jpeg");
form.setParameter("propertyImage", new UploadFileSpec[] {uploadFileSpec});

您确实需要按照第一篇文章唯一答案中的建议深入研究测试框架文档。

编辑:ServletRunner 中的 servlet 实现不支持 getPart() 方法,这就是为什么您无法在另一侧获取任何部分并获得 AbtractMethodError 的原因。

关于java - 如何在 Java 中使用 Httpunit 发送多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com