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.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37
38 import junit.framework.Test;
39 import junit.framework.TestSuite;
40
41 import org.apache.commons.httpclient.methods.PostMethod;
42 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
43 import org.apache.commons.httpclient.methods.multipart.FilePart;
44 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
45 import org.apache.commons.httpclient.methods.multipart.Part;
46 import org.apache.commons.httpclient.methods.multipart.PartSource;
47 import org.apache.commons.httpclient.methods.multipart.StringPart;
48
49 /***
50 * Webapp tests specific to the MultiPostMethod.
51 *
52 * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
53 */
54 public class TestMultipartPost extends HttpClientTestBase {
55
56 public TestMultipartPost(final String testName) throws IOException {
57 super(testName);
58 }
59
60 public static Test suite() {
61 TestSuite suite = new TestSuite(TestMultipartPost.class);
62 ProxyTestDecorator.addTests(suite);
63 return suite;
64 }
65
66 public static void main(String args[]) {
67 String[] testCaseName = { TestMultipartPost.class.getName() };
68 junit.textui.TestRunner.main(testCaseName);
69 }
70
71
72
73 /***
74 * Test that the body consisting of a string part can be posted.
75 */
76 public void testPostStringPart() throws Exception {
77
78 this.server.setHttpService(new EchoService());
79
80 PostMethod method = new PostMethod();
81 MultipartRequestEntity entity = new MultipartRequestEntity(
82 new Part[] { new StringPart("param", "Hello", "ISO-8859-1") },
83 method.getParams());
84 method.setRequestEntity(entity);
85 client.executeMethod(method);
86
87 assertEquals(200,method.getStatusCode());
88 String body = method.getResponseBodyAsString();
89 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
90 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
91 assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
92 assertTrue(body.indexOf("Hello") >= 0);
93 }
94
95
96 /***
97 * Test that the body consisting of a file part can be posted.
98 */
99 public void testPostFilePart() throws Exception {
100
101 this.server.setHttpService(new EchoService());
102
103 PostMethod method = new PostMethod();
104 byte[] content = "Hello".getBytes();
105 MultipartRequestEntity entity = new MultipartRequestEntity(
106 new Part[] {
107 new FilePart(
108 "param1",
109 new ByteArrayPartSource("filename.txt", content),
110 "text/plain",
111 "ISO-8859-1") },
112 method.getParams());
113 method.setRequestEntity(entity);
114
115 client.executeMethod(method);
116
117 assertEquals(200,method.getStatusCode());
118 String body = method.getResponseBodyAsString();
119 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
120 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
121 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
122 assertTrue(body.indexOf("Hello") >= 0);
123 }
124
125 /***
126 * Test that the body consisting of a file part of unknown length can be posted.
127 */
128
129 public class TestPartSource implements PartSource {
130 private String fileName;
131 private byte[] data;
132
133 public TestPartSource(String fileName, byte[] data) {
134 this.fileName = fileName;
135 this.data = data;
136 }
137
138 public long getLength() {
139 return -1;
140 }
141
142 public String getFileName() {
143 return fileName;
144 }
145
146 public InputStream createInputStream() throws IOException {
147 return new ByteArrayInputStream(data);
148 }
149
150 }
151
152 public void testPostFilePartUnknownLength() throws Exception {
153
154 this.server.setHttpService(new EchoService());
155
156 String enc = "ISO-8859-1";
157 PostMethod method = new PostMethod();
158 byte[] content = "Hello".getBytes(enc);
159 MultipartRequestEntity entity = new MultipartRequestEntity(
160 new Part[] {
161 new FilePart(
162 "param1",
163 new TestPartSource("filename.txt", content),
164 "text/plain",
165 enc) },
166 method.getParams());
167 method.setRequestEntity(entity);
168
169 client.executeMethod(method);
170
171 assertEquals(200,method.getStatusCode());
172 String body = method.getResponseBodyAsString();
173 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
174 assertTrue(body.indexOf("Content-Type: text/plain; charset="+enc) >= 0);
175 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
176 assertTrue(body.indexOf("Hello") >= 0);
177 }
178
179 }