1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java,v 1.5 2004/12/12 10:02:38 olegk Exp $
3    * $Revision: 161333 $
4    * $Date: 2005-04-14 14:21:03 -0400 (Thu, 14 Apr 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
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 }