5

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.

So, I have a Java Servlet with this code (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):

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

I get this error (from within my servlet my the way):

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. I am loading the form in successfully but am getting a 404. Here is my form declaration.

<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();
Community
  • 1
  • 1
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • `new UploadFileSpec(new File("C:/test.jpg"), "multipart/form-data");`. The second parameter value seems to be invalid. Take a look at this source code: [link](http://grepcode.com/file/repo1.maven.org/maven2/org.kohsuke.httpunit/httpunit/1.7-patch-2/com/meterware/httpunit/protocol/UploadFileSpec.java) – Sowmiya Jun 10 '14 at 11:02

1 Answers1

2

Should't the third UploadFileSpec constructor parameter be the content type and not the message type? In your case would be "image/jpeg".

You need a 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.

ra2085
  • 814
  • 4
  • 13
  • In the first line of your example, what is `response`? – jlars62 Jun 10 '14 at 15:57
  • I just extended the code answer. You need an HTML resource on your site/server/app containing a form with "stockImageUpload" as name. HTML Forms with file parameters are submitted as multipart messages. – ra2085 Jun 10 '14 at 16:18
  • If you don't really want to implement any HTML resource, then perhaps you should change your Web client libraries. You can test an upload using plain HTTPUrlConnections. – ra2085 Jun 10 '14 at 16:21
  • You can also use a REST client library such as Jersey. – ra2085 Jun 10 '14 at 16:22
  • Do you know if there is a way to add a local html file? Or a way to just add static html in the code? I don't want to have to make an http request to get a form, if possible. – jlars62 Jun 10 '14 at 16:55
  • I am just simulating the server with ServletRunner – jlars62 Jun 10 '14 at 17:01
  • Then, it shouldn't be too difficult to implement a servlet with the Form you need as response. That's pretty much the way the library works. Here's a nice tutorial to follow http://httpunit.sourceforge.net/doc/tutorial/task1editor-initial.html – ra2085 Jun 10 '14 at 17:03
  • Or you can change your client library. HttpUnit is used to test both ends of a web app (http client and server at the same time). – ra2085 Jun 10 '14 at 17:05
  • You know what? why don't you try using a java.net.URL instead of a plain String as the first parameter of GetMethodWebRequest, and don't forget to use in the URL the java local file protocol: file://localhost/:/ – ra2085 Jun 10 '14 at 17:17
  • 1
    Yeah, actually that's what I was trying to do, and I got it working now. For whatever reason, it only works if there is only one slash after file - found that tip here... http://www.coderanch.com/t/95000/Testing/HttpUnit-Web-Server-howto-newbie. So I got the form now, and I am successfully setting parameters, but I am still getting a `404`. Not sure why yet. – jlars62 Jun 10 '14 at 17:24
  • I updated my question with my `form` declaration as well as my code for setting up my `ServletRunner`. – jlars62 Jun 10 '14 at 17:32
  • Did you applied the suggested changes on parameters in UploadFileSpec constructor? – ra2085 Jun 10 '14 at 17:36
  • also, please update the code of the test case that's shown here. – ra2085 Jun 10 '14 at 17:37
  • Yes. I should have shown the whole updated test code...Done now. – jlars62 Jun 10 '14 at 17:39
  • ok, you're not naming your form in the HTML, therefore is not being found at getFormWithName. Use response.getForms()[0] instead. – ra2085 Jun 10 '14 at 17:40
  • Ah sorry man, I actually just fixed that but forgot to update it here... my bad. See edit – jlars62 Jun 10 '14 at 17:42
  • still getting 404 on form submission? – ra2085 Jun 10 '14 at 17:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55388/discussion-between-ra2085-and-jlars62). – ra2085 Jun 10 '14 at 17:44