Added an unit test to show how to send multi part form request entity to the 
server


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f01a7366
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f01a7366
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f01a7366

Branch: refs/heads/master
Commit: f01a736657903e83d2ee7b05daa851342690ba5a
Parents: d62ac0f
Author: Willem Jiang <[email protected]>
Authored: Mon Jun 16 10:08:46 2014 +0800
Committer: Willem Jiang <[email protected]>
Committed: Mon Jun 16 21:27:04 2014 +0800

----------------------------------------------------------------------
 .../component/jetty/MultiPartFormTest.java      | 53 +++++++++++++-------
 1 file changed, 35 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f01a7366/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java
 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java
index 201ebd7..7c81101 100644
--- 
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java
+++ 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.jetty;
 
 import java.io.File;
+
 import javax.activation.DataHandler;
 
 import org.apache.camel.Exchange;
@@ -25,28 +26,32 @@ import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.commons.httpclient.methods.multipart.FilePart;
 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
 import org.apache.commons.httpclient.methods.multipart.Part;
 import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.junit.Test;
 
 public class MultiPartFormTest extends BaseJettyTest {
+    private RequestEntity createMultipartRequestEntity() throws Exception {
+        File file = new File("src/main/resources/META-INF/NOTICE.txt");
+
+        Part[] parts = {new StringPart("comment", "A binary file of some 
kind"),
+                        new FilePart(file.getName(), file)};
+
+        return new MultipartRequestEntity(parts, new HttpMethodParams());
+
+    }
 
     @Test
     public void testSendMultiPartForm() throws Exception {
         HttpClient httpclient = new HttpClient();
 
-        File file = new File("src/main/resources/META-INF/NOTICE.txt");
-
         PostMethod httppost = new PostMethod("http://localhost:"; + getPort() + 
"/test");
-        Part[] parts = {
-            new StringPart("comment", "A binary file of some kind"),
-            new FilePart(file.getName(), file)
-        };
-
-        MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, 
httppost.getParams());
-        httppost.setRequestEntity(reqEntity);
+        
+        httppost.setRequestEntity(createMultipartRequestEntity());
 
         int status = httpclient.executeMethod(httppost);
 
@@ -57,15 +62,23 @@ public class MultiPartFormTest extends BaseJettyTest {
 
     }
 
+    @Test
+    public void testSendMultiPartFormFromCamelHttpComponnent() throws 
Exception {
+        String result = template.requestBody("http://localhost:"; + getPort() + 
"/test", createMultipartRequestEntity(), String.class);
+        assertEquals("Get a wrong result", "A binary file of some kind", 
result);
+    }
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
                 // START SNIPPET: e1
-                // Set the jetty temp directory which store the file for multi 
part form
-                // camel-jetty will clean up the file after it handled the 
request.
+                // Set the jetty temp directory which store the file for multi
+                // part form
+                // camel-jetty will clean up the file after it handled the
+                // request.
                 // The option works rightly from Camel 2.4.0
                 getContext().getProperties().put("CamelJettyTempDir", 
"target");
-                
+
                 from("jetty://http://localhost:{{port}}/test";).process(new 
Processor() {
 
                     public void process(Exchange exchange) throws Exception {
@@ -75,16 +88,21 @@ public class MultiPartFormTest extends BaseJettyTest {
                         DataHandler data = in.getAttachment("NOTICE.txt");
 
                         assertNotNull("Should get the DataHandle NOTICE.txt", 
data);
-                        // This assert is wrong, but the correct content-type 
(application/octet-stream)
-                        // will not be returned until Jetty makes it available 
- currently the content-type
-                        // returned is just the default for FileDataHandler 
(for the implentation being used)
-                        //assertEquals("Get a wrong content type", 
"text/plain", data.getContentType());
+                        // This assert is wrong, but the correct content-type
+                        // (application/octet-stream)
+                        // will not be returned until Jetty makes it available 
-
+                        // currently the content-type
+                        // returned is just the default for FileDataHandler 
(for
+                        // the implentation being used)
+                        // assertEquals("Get a wrong content type",
+                        // "text/plain", data.getContentType());
                         assertEquals("Got the wrong name", "NOTICE.txt", 
data.getName());
 
                         assertTrue("We should get the data from the 
DataHandle", data.getDataSource()
                             .getInputStream().available() > 0);
 
-                        // The other form date can be get from the message 
header
+                        // The other form date can be get from the message
+                        // header
                         exchange.getOut().setBody(in.getHeader("comment"));
                     }
 
@@ -94,5 +112,4 @@ public class MultiPartFormTest extends BaseJettyTest {
         };
     }
 
-
 }

Reply via email to