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 junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.methods.MultipartPostMethod;
38 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
39 import org.apache.commons.httpclient.methods.multipart.FilePart;
40 import org.apache.commons.httpclient.methods.multipart.StringPart;
41
42 /***
43 * Webapp tests specific to the MultiPostMethod.
44 *
45 * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
46 */
47 public class TestWebappMultiPostMethod extends TestWebappBase {
48
49 HttpClient httpClient;
50 final String paramsPath = "/" + getWebappContext() + "/params";
51 final String bodyPath = "/" + getWebappContext() + "/body";
52
53 public TestWebappMultiPostMethod(String testName) {
54 super(testName);
55 }
56
57 public static Test suite() {
58 TestSuite suite = new TestSuite(TestWebappMultiPostMethod.class);
59 return suite;
60 }
61
62 public static void main(String args[]) {
63 String[] testCaseName = { TestWebappMultiPostMethod.class.getName() };
64 junit.textui.TestRunner.main(testCaseName);
65 }
66
67 public void setUp() {
68 httpClient = createHttpClient();
69 }
70
71
72
73 /***
74 * Test that the body consisting of a string part can be posted.
75 */
76
77 public void testPostStringPart() throws Exception {
78 MultipartPostMethod method = new MultipartPostMethod(bodyPath);
79 method.addPart(new StringPart("param", "Hello", "ISO-8859-1"));
80
81 httpClient.executeMethod(method);
82
83 assertEquals(200,method.getStatusCode());
84 String body = method.getResponseBodyAsString();
85 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
86 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
87 assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
88 assertTrue(body.indexOf("Hello") >= 0);
89 }
90
91
92 /***
93 * Test that the body consisting of a file part can be posted.
94 */
95 public void testPostFilePart() throws Exception {
96 MultipartPostMethod method = new MultipartPostMethod(bodyPath);
97 byte[] content = "Hello".getBytes();
98 method.addPart(
99 new FilePart(
100 "param1",
101 new ByteArrayPartSource("filename.txt", content),
102 "text/plain",
103 "ISO-8859-1"));
104
105 httpClient.executeMethod(method);
106
107 assertEquals(200,method.getStatusCode());
108 String body = method.getResponseBodyAsString();
109 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
110 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
111 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
112 assertTrue(body.indexOf("Hello") >= 0);
113 }
114 }
115