001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient;
031:
032: import java.io.ByteArrayOutputStream;
033: import java.io.IOException;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.commons.httpclient.methods.PostMethod;
040: import org.apache.commons.httpclient.methods.RequestEntity;
041: import org.apache.commons.httpclient.methods.StringRequestEntity;
042:
043: /**
044: * Tests basic method functionality.
045: *
046: * @author Remy Maucherat
047: * @author Rodney Waldhoff
048: *
049: * @version $Id: TestPostParameterEncoding.java 480424 2006-11-29 05:56:49Z bayard $
050: */
051: public class TestPostParameterEncoding extends TestCase {
052:
053: static final String NAME = "name", VALUE = "value";
054: static final String NAME0 = "name0", VALUE0 = "value0";
055: static final String NAME1 = "name1", VALUE1 = "value1";
056: static final String NAME2 = "name2", VALUE2 = "value2";
057:
058: static final NameValuePair PAIR = new NameValuePair(NAME, VALUE);
059: static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0);
060: static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
061: static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
062:
063: public TestPostParameterEncoding(final String testName)
064: throws IOException {
065: super (testName);
066: }
067:
068: public static Test suite() {
069: return new TestSuite(TestPostParameterEncoding.class);
070: }
071:
072: public static void main(String args[]) {
073: String[] testCaseName = { TestPostParameterEncoding.class
074: .getName() };
075: junit.textui.TestRunner.main(testCaseName);
076: }
077:
078: private String getRequestAsString(RequestEntity entity)
079: throws Exception {
080: ByteArrayOutputStream bos = new ByteArrayOutputStream();
081: entity.writeRequest(bos);
082: return new String(bos.toByteArray(), "UTF-8");
083: }
084:
085: public void testPostParametersEncoding() throws Exception {
086: PostMethod post = new PostMethod();
087: post.setRequestBody(new NameValuePair[] { PAIR });
088: assertEquals("name=value", getRequestAsString(post
089: .getRequestEntity()));
090:
091: post.setRequestBody(new NameValuePair[] { PAIR, PAIR1, PAIR2 });
092: assertEquals("name=value&name1=value1&name2=value2",
093: getRequestAsString(post.getRequestEntity()));
094:
095: post.setRequestBody(new NameValuePair[] { PAIR, PAIR1, PAIR2,
096: new NameValuePair("hasSpace", "a b c d") });
097: assertEquals(
098: "name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
099: getRequestAsString(post.getRequestEntity()));
100:
101: post
102: .setRequestBody(new NameValuePair[] { new NameValuePair(
103: "escaping",
104: ",.-\u00f6\u00e4\u00fc!+@#*&()=?:;}{[]$") });
105: assertEquals(
106: "escaping=%2C.-%F6%E4%FC%21%2B%40%23*%26%28%29%3D%3F%3A%3B%7D%7B%5B%5D%24",
107: getRequestAsString(post.getRequestEntity()));
108:
109: }
110:
111: public void testPostSetRequestBody() throws Exception {
112: PostMethod post = new PostMethod("/foo");
113: String body = "this+is+the+body";
114: post
115: .setRequestEntity(new StringRequestEntity(body, null,
116: null));
117: assertEquals(body, getRequestAsString(post.getRequestEntity()));
118: }
119:
120: }
|