1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java,v 1.3 2004/11/01 02:21:15 mbecke Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/11/01 02:21:15 $
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   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import java.io.ByteArrayOutputStream;
34  import java.io.IOException;
35  
36  import junit.framework.Test;
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: TestPostMethod.java,v 1.3 2004/11/01 02:21:15 mbecke Exp $
50   */
51  public class TestPostMethod extends HttpClientTestBase {
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 TestPostMethod(final String testName) throws IOException {
64          super(testName);
65      }
66  
67      public static Test suite() {
68          TestSuite suite = new TestSuite(TestPostMethod.class);
69          ProxyTestDecorator.addTests(suite);
70          return suite;
71      }
72  
73      public static void main(String args[]) {
74          String[] testCaseName = { TestPostMethod.class.getName() };
75          junit.textui.TestRunner.main(testCaseName);
76      }
77      
78      private String getRequestAsString(RequestEntity entity) throws Exception {
79          ByteArrayOutputStream bos = new ByteArrayOutputStream();
80          entity.writeRequest(bos);
81          return new String(bos.toByteArray(), "UTF-8");
82      }
83      
84      public void testPostParametersEncoding() throws Exception {
85          PostMethod post = new PostMethod();
86          post.setRequestBody(new NameValuePair[] { PAIR });
87          assertEquals("name=value", getRequestAsString(post.getRequestEntity()));
88  
89          post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
90          assertEquals("name=value&name1=value1&name2=value2", 
91              getRequestAsString(post.getRequestEntity()));
92  
93          post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") });
94          assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
95              getRequestAsString(post.getRequestEntity()));
96  
97      }
98  
99      public void testPostSetRequestBody() throws Exception {
100         PostMethod post = new PostMethod("/foo");
101         String body = "this+is+the+body";
102         post.setRequestEntity(new StringRequestEntity(body));
103         assertEquals(body, getRequestAsString(post.getRequestEntity()));
104     }
105     
106 }