I have bee trying the get a put method working with httpunit but not really
sure what I'm doing wrong. Currently my code
looks like:
Servlet
@PUT
@Path("/users/{userId}.json")
@Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})
public void updateUser(@PathParam("userId")String userId,
@FormParam("jsonAccount")String jsonAccount) {
log.info("in put");
}
and the testing code:
@Test
public void updateUserTest() {
System.out.println("\n" + new
Exception().getStackTrace()[0].getMethodName());
String uri = "http://localhost:8088/restApi/test/users/1.json";
String newAccount = "{\"_id\":\"1\",\"username\":\"bobby\"}";
String encoded = URLEncoder.encode(newAccount);
InputStream source = null;
try {
source = new ByteArrayInputStream(encoded.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String contentType = "application/x-www-form-urlencoded";
WebRequest req = new PutMethodWebRequest(uri, source, contentType);
try {
req.setParameter("jsonAccount", encoded );
WebResponse resp = wc.getResource(req);
System.out.println("resp " + resp.getText());
} catch (Exception e) {
fail(ERR_MSG + e);
}
}
Any help appreciated.
Thanks!
Erik
|