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