[英]How to send Multipart request with Httpunit in Java
I am essentially asking the exact same question here . 我基本上是问的确切同样的问题在这里 。 As you can see, there are no solid answers.
如您所见,没有可靠的答案。 All I want to do is send a file using
HTTPUnit
to test a java servlet. 我想要做的就是使用
HTTPUnit
发送一个文件来测试java servlet。
So, I have a Java Servlet
with this code (dumbed down): 所以,我有一个带有这个代码的
Java Servlet
(dumbed down):
@WebServlet("/properties/StorePropertyAttachment")
@MultipartConfig
public class StorePropertyAttachment {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
final Part p = req.getPart("propertyImage");
....
}
}
Here is the important part of my test case: 这是我的测试用例的重要部分:
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);
When I run this, I get this error: 当我运行它时,我收到此错误:
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)
....
Just for kick, if I change line 68 to this (a normal parameter): 只是为了踢,如果我将第68行更改为此(正常参数):
webRequest.setParameter("propertyImage", "some string");
I get this error (from within my servlet my the way): 我得到了这个错误(从我的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)
....
I don't know why it wont let me add the file. 我不知道为什么它不会让我添加文件。
Any suggestions?? 有什么建议么??
EDIT: 编辑:
I am trying to submit this using a form from a local html file. 我试图使用本地html文件中的表单提交此内容。 I am loading the form in successfully but am getting a 404. Here is my form declaration.
我正在成功加载表单但是得到了404.这是我的表单声明。
<form method="POST" action="http://localhost/StorePropertyAttachment" enctype="multipart/form-data" name="propertyImageTest">
<input type="file" name="propertyImage" />
<input type="submit" />
</form>
Updated test code: 更新的测试代码:
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();
Should't the third UploadFileSpec constructor parameter be the content type and not the message type? 第三个UploadFileSpec构造函数参数不应该是内容类型而不是消息类型吗? In your case would be "image/jpeg".
在你的情况下将是“image / jpeg”。
You need a WebForm 你需要一个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});
You really need to dig into the testing framework documentation as suggested in the only answer on your first post. 您真的需要深入了解第一篇文章中唯一答案中建议的测试框架文档。
EDIT: the getPart()
method is not supported in the servlet implementation in ServletRunner, that's why you couldn't get any part on the other side and getting an AbtractMethodError. 编辑:ServletRunner中的servlet实现不支持
getPart()
方法,这就是为什么你不能在另一方获得任何部分并获得AbtractMethodError的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.