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 package org.apache.commons.httpclient;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38 import org.apache.commons.httpclient.methods.PostMethod;
39 import org.apache.commons.httpclient.methods.RequestEntity;
40 import org.apache.commons.httpclient.methods.StringRequestEntity;
41
42 /***
43 * Tests basic method functionality.
44 *
45 * @author Remy Maucherat
46 * @author Rodney Waldhoff
47 *
48 * @version $Id: TestPostParameterEncoding.java 161333 2005-04-14 18:21:03Z olegk $
49 */
50 public class TestPostParameterEncoding extends TestCase {
51
52 static final String NAME = "name", VALUE = "value";
53 static final String NAME0 = "name0", VALUE0 = "value0";
54 static final String NAME1 = "name1", VALUE1 = "value1";
55 static final String NAME2 = "name2", VALUE2 = "value2";
56
57 static final NameValuePair PAIR = new NameValuePair(NAME, VALUE);
58 static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0);
59 static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
60 static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
61
62 public TestPostParameterEncoding(final String testName) throws IOException {
63 super(testName);
64 }
65
66 public static Test suite() {
67 return new TestSuite(TestPostParameterEncoding.class);
68 }
69
70 public static void main(String args[]) {
71 String[] testCaseName = { TestPostParameterEncoding.class.getName() };
72 junit.textui.TestRunner.main(testCaseName);
73 }
74
75 private String getRequestAsString(RequestEntity entity) throws Exception {
76 ByteArrayOutputStream bos = new ByteArrayOutputStream();
77 entity.writeRequest(bos);
78 return new String(bos.toByteArray(), "UTF-8");
79 }
80
81 public void testPostParametersEncoding() throws Exception {
82 PostMethod post = new PostMethod();
83 post.setRequestBody(new NameValuePair[] { PAIR });
84 assertEquals("name=value", getRequestAsString(post.getRequestEntity()));
85
86 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
87 assertEquals("name=value&name1=value1&name2=value2",
88 getRequestAsString(post.getRequestEntity()));
89
90 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") });
91 assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
92 getRequestAsString(post.getRequestEntity()));
93
94 post.setRequestBody(new NameValuePair[]{ new NameValuePair("escaping", ",.-\u00f6\u00e4\u00fc!+@#*&()=?:;}{[]$") });
95 assertEquals("escaping=%2C.-%F6%E4%FC%21%2B%40%23*%26%28%29%3D%3F%3A%3B%7D%7B%5B%5D%24",
96 getRequestAsString(post.getRequestEntity()));
97
98 }
99
100 public void testPostSetRequestBody() throws Exception {
101 PostMethod post = new PostMethod("/foo");
102 String body = "this+is+the+body";
103 post.setRequestEntity(new StringRequestEntity(body));
104 assertEquals(body, getRequestAsString(post.getRequestEntity()));
105 }
106
107 }