1   /*
2    * $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 $
3    * $Revision: 332125 $
4    * $Date: 2005-11-09 14:41:40 -0500 (Wed, 09 Nov 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  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   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient;
33  
34  import java.io.ByteArrayInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  
38  import junit.framework.Test;
39  import junit.framework.TestSuite;
40  
41  import org.apache.commons.httpclient.methods.PostMethod;
42  import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
43  import org.apache.commons.httpclient.methods.multipart.FilePart;
44  import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
45  import org.apache.commons.httpclient.methods.multipart.Part;
46  import org.apache.commons.httpclient.methods.multipart.PartSource;
47  import org.apache.commons.httpclient.methods.multipart.StringPart;
48  
49  /***
50   * Webapp tests specific to the MultiPostMethod.
51   *
52   * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
53   */
54  public class TestMultipartPost extends HttpClientTestBase {
55  
56      public TestMultipartPost(final String testName) throws IOException {
57          super(testName);
58      }
59  
60      public static Test suite() {
61          TestSuite suite = new TestSuite(TestMultipartPost.class);
62          ProxyTestDecorator.addTests(suite);
63          return suite;
64      }
65  
66      public static void main(String args[]) {
67          String[] testCaseName = { TestMultipartPost.class.getName() };
68          junit.textui.TestRunner.main(testCaseName);
69      }
70  
71      // ------------------------------------------------------------------ Tests
72      
73      /***
74       * Test that the body consisting of a string part can be posted.
75       */
76      public void testPostStringPart() throws Exception {
77          
78          this.server.setHttpService(new EchoService());
79          
80          PostMethod method = new PostMethod();
81          MultipartRequestEntity entity = new MultipartRequestEntity(
82              new Part[] { new StringPart("param", "Hello", "ISO-8859-1") },
83              method.getParams());
84          method.setRequestEntity(entity);
85          client.executeMethod(method);
86  
87          assertEquals(200,method.getStatusCode());
88          String body = method.getResponseBodyAsString();
89          assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
90          assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
91          assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
92          assertTrue(body.indexOf("Hello") >= 0);
93      }
94  
95  
96      /***
97       * Test that the body consisting of a file part can be posted.
98       */
99      public void testPostFilePart() throws Exception {
100         
101         this.server.setHttpService(new EchoService());
102 
103         PostMethod method = new PostMethod();
104         byte[] content = "Hello".getBytes();
105         MultipartRequestEntity entity = new MultipartRequestEntity(
106             new Part[] { 
107                 new FilePart(
108                     "param1", 
109                     new ByteArrayPartSource("filename.txt", content), 
110                     "text/plain", 
111                     "ISO-8859-1") },
112             method.getParams());
113         method.setRequestEntity(entity);
114 
115         client.executeMethod(method);
116 
117         assertEquals(200,method.getStatusCode());
118         String body = method.getResponseBodyAsString();
119         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
120         assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
121         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
122         assertTrue(body.indexOf("Hello") >= 0);
123     }
124 
125     /***
126      * Test that the body consisting of a file part of unknown length can be posted.
127      */
128 
129     public class TestPartSource implements PartSource {
130         private String fileName;
131         private byte[] data;
132 
133         public TestPartSource(String fileName, byte[] data) {
134             this.fileName = fileName;
135             this.data = data;
136         }
137 
138         public long getLength() {
139             return -1;
140         }
141 
142         public String getFileName() {
143             return fileName;
144         }
145 
146         public InputStream createInputStream() throws IOException {
147             return new ByteArrayInputStream(data);
148         }
149 
150     }
151     
152     public void testPostFilePartUnknownLength() throws Exception {
153         
154         this.server.setHttpService(new EchoService());
155 
156         String enc = "ISO-8859-1";
157         PostMethod method = new PostMethod();
158         byte[] content = "Hello".getBytes(enc);
159         MultipartRequestEntity entity = new MultipartRequestEntity(
160             new Part[] { 
161                 new FilePart(
162                     "param1", 
163                     new TestPartSource("filename.txt", content), 
164                      "text/plain", 
165                      enc) },
166              method.getParams());
167         method.setRequestEntity(entity);
168 
169         client.executeMethod(method);
170 
171         assertEquals(200,method.getStatusCode());
172         String body = method.getResponseBodyAsString();
173         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
174         assertTrue(body.indexOf("Content-Type: text/plain; charset="+enc) >= 0);
175         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
176         assertTrue(body.indexOf("Hello") >= 0);
177     }
178     
179 }