Hello everyone,
I quite new to mockito and I am trying to mock my method which sends binary file and couple of strings as @POST request. The request is send as MultipartEntity. I am really not sure how to do that. I found couple of examples but none of them was useful.
Here is my method I want to:
public void sendPostRequest(Exchange exchange) throws IOException {
String pathToUploader = "https://" + exchange.getProperty("HOST").toString() + ":"
+ exchange.getProperty("PORT").toString()
+ exchange.getProperty("UPLOAD_API").toString();
String filename = exchange.getProperty("PATH_TO_FILE").toString();
String session_id = exchange.getProperty("SESSION_ID").toString();
String action = "A";
String output = "json";
File myfile = new File(filename);
if(myfile.exists()){
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(pathToUploader);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// For File parameters
entity.addPart("myfile", new FileBody(((File) myfile), "application/octet-stream"));
// For usual String parameters
entity.addPart("session_id", new StringBody(session_id, "text/plain", Charset.forName("UTF-8")));
entity.addPart("action", new StringBody(action, "text/plain", Charset.forName("UTF-8")));
entity.addPart("output", new StringBody(output, "text/plain", Charset.forName("UTF-8")));
post.setEntity(entity);
String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8");
exchange.getOut().setBody(response);
client.getConnectionManager().shutdown();
}else{
logger.error("File does not exist!!!");
throw new FileNotFoundException();
}
}
Thanks for any help
-Br, Roman
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.