1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient;
33
34 import java.io.IOException;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38
39 import org.apache.commons.httpclient.methods.PostMethod;
40 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
41 import org.apache.commons.httpclient.methods.multipart.FilePart;
42 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
43 import org.apache.commons.httpclient.methods.multipart.Part;
44 import org.apache.commons.httpclient.methods.multipart.StringPart;
45
46 /***
47 * Webapp tests specific to the MultiPostMethod.
48 *
49 * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
50 */
51 public class TestMultipartPost extends HttpClientTestBase {
52
53 public TestMultipartPost(final String testName) throws IOException {
54 super(testName);
55 }
56
57 public static Test suite() {
58 TestSuite suite = new TestSuite(TestMultipartPost.class);
59 ProxyTestDecorator.addTests(suite);
60 return suite;
61 }
62
63 public static void main(String args[]) {
64 String[] testCaseName = { TestMultipartPost.class.getName() };
65 junit.textui.TestRunner.main(testCaseName);
66 }
67
68
69
70 /***
71 * Test that the body consisting of a string part can be posted.
72 */
73 public void testPostStringPart() throws Exception {
74
75 this.server.setHttpService(new EchoService());
76
77 PostMethod method = new PostMethod();
78 MultipartRequestEntity entity = new MultipartRequestEntity(
79 new Part[] { new StringPart("param", "Hello", "ISO-8859-1") },
80 method.getParams());
81 method.setRequestEntity(entity);
82 client.executeMethod(method);
83
84 assertEquals(200,method.getStatusCode());
85 String body = method.getResponseBodyAsString();
86 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
87 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
88 assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
89 assertTrue(body.indexOf("Hello") >= 0);
90 }
91
92
93 /***
94 * Test that the body consisting of a file part can be posted.
95 */
96 public void testPostFilePart() throws Exception {
97
98 this.server.setHttpService(new EchoService());
99
100 PostMethod method = new PostMethod();
101 byte[] content = "Hello".getBytes();
102 MultipartRequestEntity entity = new MultipartRequestEntity(
103 new Part[] {
104 new FilePart(
105 "param1",
106 new ByteArrayPartSource("filename.txt", content),
107 "text/plain",
108 "ISO-8859-1") },
109 method.getParams());
110 method.setRequestEntity(entity);
111
112 client.executeMethod(method);
113
114 assertEquals(200,method.getStatusCode());
115 String body = method.getResponseBodyAsString();
116 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
117 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
118 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
119 assertTrue(body.indexOf("Hello") >= 0);
120 }
121 }
122