1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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 608014 2008-01-02 05:48:53Z rolandw $
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 }