001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestMultipartPost.java,v 1.3 2004/11/01 02:21:15 mbecke Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: package org.apache.commons.httpclient;
034:
035: import java.io.ByteArrayInputStream;
036: import java.io.IOException;
037: import java.io.InputStream;
038:
039: import junit.framework.Test;
040: import junit.framework.TestSuite;
041:
042: import org.apache.commons.httpclient.methods.PostMethod;
043: import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
044: import org.apache.commons.httpclient.methods.multipart.FilePart;
045: import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
046: import org.apache.commons.httpclient.methods.multipart.Part;
047: import org.apache.commons.httpclient.methods.multipart.PartSource;
048: import org.apache.commons.httpclient.methods.multipart.StringPart;
049:
050: /**
051: * Webapp tests specific to the MultiPostMethod.
052: *
053: * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
054: */
055: public class TestMultipartPost extends HttpClientTestBase {
056:
057: public TestMultipartPost(final String testName) throws IOException {
058: super (testName);
059: }
060:
061: public static Test suite() {
062: TestSuite suite = new TestSuite(TestMultipartPost.class);
063: ProxyTestDecorator.addTests(suite);
064: return suite;
065: }
066:
067: public static void main(String args[]) {
068: String[] testCaseName = { TestMultipartPost.class.getName() };
069: junit.textui.TestRunner.main(testCaseName);
070: }
071:
072: // ------------------------------------------------------------------ Tests
073:
074: /**
075: * Test that the body consisting of a string part can be posted.
076: */
077: public void testPostStringPart() throws Exception {
078:
079: this .server.setHttpService(new EchoService());
080:
081: PostMethod method = new PostMethod();
082: MultipartRequestEntity entity = new MultipartRequestEntity(
083: new Part[] { new StringPart("param", "Hello",
084: "ISO-8859-1") }, method.getParams());
085: method.setRequestEntity(entity);
086: client.executeMethod(method);
087:
088: assertEquals(200, method.getStatusCode());
089: String body = method.getResponseBodyAsString();
090: assertTrue(body
091: .indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
092: assertTrue(body
093: .indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
094: assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
095: assertTrue(body.indexOf("Hello") >= 0);
096: }
097:
098: /**
099: * Test that the body consisting of a file part can be posted.
100: */
101: public void testPostFilePart() throws Exception {
102:
103: this .server.setHttpService(new EchoService());
104:
105: PostMethod method = new PostMethod();
106: byte[] content = "Hello".getBytes();
107: MultipartRequestEntity entity = new MultipartRequestEntity(
108: new Part[] { new FilePart(
109: "param1",
110: new ByteArrayPartSource("filename.txt", content),
111: "text/plain", "ISO-8859-1") }, method
112: .getParams());
113: method.setRequestEntity(entity);
114:
115: client.executeMethod(method);
116:
117: assertEquals(200, method.getStatusCode());
118: String body = method.getResponseBodyAsString();
119: assertTrue(body
120: .indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
121: assertTrue(body
122: .indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
123: assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
124: assertTrue(body.indexOf("Hello") >= 0);
125: }
126:
127: /**
128: * Test that the body consisting of a file part of unknown length can be posted.
129: */
130:
131: public class TestPartSource implements PartSource {
132: private String fileName;
133: private byte[] data;
134:
135: public TestPartSource(String fileName, byte[] data) {
136: this .fileName = fileName;
137: this .data = data;
138: }
139:
140: public long getLength() {
141: return -1;
142: }
143:
144: public String getFileName() {
145: return fileName;
146: }
147:
148: public InputStream createInputStream() throws IOException {
149: return new ByteArrayInputStream(data);
150: }
151:
152: }
153:
154: public void testPostFilePartUnknownLength() throws Exception {
155:
156: this .server.setHttpService(new EchoService());
157:
158: String enc = "ISO-8859-1";
159: PostMethod method = new PostMethod();
160: byte[] content = "Hello".getBytes(enc);
161: MultipartRequestEntity entity = new MultipartRequestEntity(
162: new Part[] { new FilePart("param1", new TestPartSource(
163: "filename.txt", content), "text/plain", enc) },
164: method.getParams());
165: method.setRequestEntity(entity);
166:
167: client.executeMethod(method);
168:
169: assertEquals(200, method.getStatusCode());
170: String body = method.getResponseBodyAsString();
171: assertTrue(body
172: .indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
173: assertTrue(body.indexOf("Content-Type: text/plain; charset="
174: + enc) >= 0);
175: assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
176: assertTrue(body.indexOf("Hello") >= 0);
177: }
178:
179: }
|