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: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 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.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.multipart.ByteArrayPartSource;
41  import org.apache.commons.httpclient.methods.multipart.FilePart;
42  import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
43  import org.apache.commons.httpclient.methods.multipart.Part;
44  import org.apache.commons.httpclient.methods.multipart.StringPart;
45  
46  /***
47   * Webapp tests specific to the MultiPostMethod.
48   *
49   * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
50   */
51  public class TestMultipartPost extends HttpClientTestBase {
52  
53      public TestMultipartPost(final String testName) throws IOException {
54          super(testName);
55      }
56  
57      public static Test suite() {
58          TestSuite suite = new TestSuite(TestMultipartPost.class);
59          ProxyTestDecorator.addTests(suite);
60          return suite;
61      }
62  
63      public static void main(String args[]) {
64          String[] testCaseName = { TestMultipartPost.class.getName() };
65          junit.textui.TestRunner.main(testCaseName);
66      }
67  
68      // ------------------------------------------------------------------ Tests
69      
70      /***
71       * Test that the body consisting of a string part can be posted.
72       */
73      public void testPostStringPart() throws Exception {
74          
75          this.server.setHttpService(new EchoService());
76          
77          PostMethod method = new PostMethod();
78          MultipartRequestEntity entity = new MultipartRequestEntity(
79              new Part[] { new StringPart("param", "Hello", "ISO-8859-1") },
80              method.getParams());
81          method.setRequestEntity(entity);
82          client.executeMethod(method);
83  
84          assertEquals(200,method.getStatusCode());
85          String body = method.getResponseBodyAsString();
86          assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
87          assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
88          assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
89          assertTrue(body.indexOf("Hello") >= 0);
90      }
91  
92  
93      /***
94       * Test that the body consisting of a file part can be posted.
95       */
96      public void testPostFilePart() throws Exception {
97          
98          this.server.setHttpService(new EchoService());
99  
100         PostMethod method = new PostMethod();
101         byte[] content = "Hello".getBytes();
102         MultipartRequestEntity entity = new MultipartRequestEntity(
103             new Part[] { 
104                 new FilePart(
105                     "param1", 
106                     new ByteArrayPartSource("filename.txt", content), 
107                     "text/plain", 
108                     "ISO-8859-1") },
109             method.getParams());
110         method.setRequestEntity(entity);
111 
112         client.executeMethod(method);
113 
114         assertEquals(200,method.getStatusCode());
115         String body = method.getResponseBodyAsString();
116         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
117         assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
118         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
119         assertTrue(body.indexOf("Hello") >= 0);
120     }
121 }
122