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 package org.apache.commons.httpclient;
32
33 import junit.framework.*;
34 import org.apache.commons.httpclient.methods.*;
35 import java.io.*;
36
37 /***
38 * Webapp tests specific to the PostMethod.
39 *
40 * @author <a href="jsdever@apache.org">Jeff Dever</a>
41 * @version $Id: TestPostMethod.java 608014 2008-01-02 05:48:53Z rolandw $
42 */
43 public class TestPostMethod extends HttpClientTestBase {
44
45 public TestPostMethod(String testName) throws IOException {
46 super(testName);
47 }
48
49 public static Test suite() {
50 TestSuite suite = new TestSuite(TestPostMethod.class);
51 return suite;
52 }
53
54 public static void main(String args[]) {
55 String[] testCaseName = { TestPostMethod.class.getName() };
56 junit.textui.TestRunner.main(testCaseName);
57 }
58
59
60
61 /***
62 * Test that the body can be set as a array of parameters
63 */
64 public void testParametersBodyToParamServlet() throws Exception {
65 PostMethod method = new PostMethod("/");
66 NameValuePair[] parametersBody = new NameValuePair[] {
67 new NameValuePair("pname1","pvalue1"),
68 new NameValuePair("pname2","pvalue2")
69 };
70 method.setRequestBody(parametersBody);
71 this.server.setHttpService(new EchoService());
72 try {
73 this.client.executeMethod(method);
74 assertEquals(200, method.getStatusCode());
75 String body = method.getResponseBodyAsString();
76 assertEquals("pname1=pvalue1&pname2=pvalue2", body);
77 } finally {
78 method.releaseConnection();
79 }
80 }
81
82 /***
83 * Test that the body can be set as a String
84 */
85 public void testStringBodyToParamServlet() throws Exception {
86 PostMethod method = new PostMethod("/");
87 String stringBody = "pname1=pvalue1&pname2=pvalue2";
88 method.setRequestEntity(
89 new StringRequestEntity(stringBody, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
90 this.server.setHttpService(new EchoService());
91 try {
92 this.client.executeMethod(method);
93 assertEquals(200, method.getStatusCode());
94 String body = method.getResponseBodyAsString();
95 assertEquals("pname1=pvalue1&pname2=pvalue2", body);
96 } finally {
97 method.releaseConnection();
98 }
99 }
100
101 /***
102 * Test that the body can be set as a String without an explict
103 * content type
104 */
105 public void testStringBodyToBodyServlet() throws Exception {
106 PostMethod method = new PostMethod("/");
107 String stringBody = "pname1=pvalue1&pname2=pvalue2";
108
109 method.setRequestEntity(new StringRequestEntity(stringBody, null, null));
110 this.server.setHttpService(new EchoService());
111 try {
112 this.client.executeMethod(method);
113 assertEquals(200, method.getStatusCode());
114 String body = method.getResponseBodyAsString();
115 assertEquals("pname1=pvalue1&pname2=pvalue2", body);
116 } finally {
117 method.releaseConnection();
118 }
119 }
120
121 /***
122 * Test that parameters can be added.
123 */
124 public void testAddParametersToParamServlet() throws Exception {
125 PostMethod method = new PostMethod("/");
126
127 method.addParameter(new NameValuePair("pname1","pvalue1"));
128 method.addParameter(new NameValuePair("pname2","pvalue2"));
129
130 this.server.setHttpService(new EchoService());
131 try {
132 this.client.executeMethod(method);
133 assertEquals(200, method.getStatusCode());
134 String body = method.getResponseBodyAsString();
135 assertEquals("pname1=pvalue1&pname2=pvalue2", body);
136 } finally {
137 method.releaseConnection();
138 }
139 }
140
141 /***
142 * Test that parameters can be added and removed.
143 */
144 public void testAddRemoveParametersToParamServlet() throws Exception {
145 PostMethod method = new PostMethod("/");
146
147 method.addParameter(new NameValuePair("pname0","pvalue0"));
148 method.addParameter(new NameValuePair("pname1","pvalue1"));
149 method.addParameter(new NameValuePair("pname2","pvalue2"));
150 method.addParameter(new NameValuePair("pname3","pvalue3"));
151 method.removeParameter("pname0");
152 method.removeParameter("pname3");
153
154 this.server.setHttpService(new EchoService());
155 try {
156 this.client.executeMethod(method);
157 assertEquals(200, method.getStatusCode());
158 String body = method.getResponseBodyAsString();
159 assertEquals("pname1=pvalue1&pname2=pvalue2", body);
160 } finally {
161 method.releaseConnection();
162 }
163 }
164
165 /***
166 * Test the return value of the PostMethod#removeParameter.
167 */
168 public void testRemoveParameterReturnValue() throws Exception {
169 PostMethod method = new PostMethod("/");
170
171 method.addParameter("param", "whatever");
172 assertTrue("Return value of the method is expected to be true", method.removeParameter("param"));
173 assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
174 }
175
176 private String getRequestAsString(RequestEntity entity) throws Exception {
177 ByteArrayOutputStream bos = new ByteArrayOutputStream();
178 entity.writeRequest(bos);
179 return new String(bos.toByteArray(), "UTF-8");
180 }
181
182 /***
183 * Test if setParameter overwrites existing parameter values.
184 */
185 public void testAddParameterFollowedBySetParameter() throws Exception {
186 PostMethod method = new PostMethod("/");
187
188 method.addParameter("param", "a");
189 method.addParameter("param", "b");
190 method.addParameter("param", "c");
191 assertEquals("param=a¶m=b¶m=c", getRequestAsString(method.getRequestEntity()));
192 method.setParameter("param", "a");
193 assertEquals("param=a", getRequestAsString(method.getRequestEntity()));
194 }
195
196 }
197