    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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   * [Additional notices, if required by prior licensing conditions]
    30   *
    31   */
    32
    33  package org.apache.commons.httpclient;
    34
    35  import java.io.ByteArrayInputStream;
    36  import java.io.IOException;
    37  import java.io.InputStream;
    38
    39  import junit.framework.Test;
    40  import junit.framework.TestSuite;
    41
    42  import org.apache.commons.httpclient.methods.PostMethod;
    43  import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
    44  import org.apache.commons.httpclient.methods.multipart.FilePart;
    45  import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
    46  import org.apache.commons.httpclient.methods.multipart.Part;
    47  import org.apache.commons.httpclient.methods.multipart.PartSource;
    48  import org.apache.commons.httpclient.methods.multipart.StringPart;
    49
    50  /**
    51   * Webapp tests specific to the MultiPostMethod.
    52   *
    53   * @author <a href="oleg@ural.ru">Oleg Kalnichevski</a>
    54   */
    55  public class TestMultipartPost extends HttpClientTestBase {
    56
    57      public TestMultipartPost(final String testName) throws IOException {
    58          super(testName);
    59      }
    60
    61      public static Test suite() {
    62          TestSuite suite = new TestSuite(TestMultipartPost.class);
    63          ProxyTestDecorator.addTests(suite);
    64          return suite;
    65      }
    66
    67      public static void main(String args[]) {
    68          String[] testCaseName = { TestMultipartPost.class.getName() };
    69          junit.textui.TestRunner.main(testCaseName);
    70      }
    71
    72      // ------------------------------------------------------------------ Tests
    73
    74      /**
    75       * Test that the body consisting of a string part can be posted.
    76       */
    77      public void testPostStringPart() throws Exception {
    78
    79          this.server.setHttpService(new EchoService());
    80
    81          PostMethod method = new PostMethod();
    82          MultipartRequestEntity entity = new MultipartRequestEntity(
    83              new Part[] { new StringPart("param", "Hello", "ISO-8859-1") },
    84              method.getParams());
    85          method.setRequestEntity(entity);
    86          client.executeMethod(method);
    87
    88          assertEquals(200,method.getStatusCode());
    89          String body = method.getResponseBodyAsString();
    90          assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
    91          assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
    92          assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
    93          assertTrue(body.indexOf("Hello") >= 0);
    94      }
    95
    96
    97      /**
    98       * Test that the body consisting of a file part can be posted.
    99       */
    100     public void testPostFilePart() throws Exception {
    101
    102         this.server.setHttpService(new EchoService());
    103
    104         PostMethod method = new PostMethod();
    105         byte[] content = "Hello".getBytes();
    106         MultipartRequestEntity entity = new MultipartRequestEntity(
    107             new Part[] {
    108                 new FilePart(
    109                     "param1",
    110                     new ByteArrayPartSource("filename.txt", content),
    111                     "text/plain",
    112                     "ISO-8859-1") },
    113             method.getParams());
    114         method.setRequestEntity(entity);
    115
    116         client.executeMethod(method);
    117
    118         assertEquals(200,method.getStatusCode());
    119         String body = method.getResponseBodyAsString();
    120         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
    121         assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
    122         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
    123         assertTrue(body.indexOf("Hello") >= 0);
    124     }
    125
    126     /**
    127      * Test that the body consisting of a file part of unknown length can be posted.
    128      */
    129
    130     public class TestPartSource implements PartSource {
    131         private String fileName;
    132         private byte[] data;
    133
    134         public TestPartSource(String fileName, byte[] data) {
    135             this.fileName = fileName;
    136             this.data = data;
    137         }
    138
    139         public long getLength() {
    140             return -1;
    141         }
    142
    143         public String getFileName() {
    144             return fileName;
    145         }
    146
    147         public InputStream createInputStream() throws IOException {
    148             return new ByteArrayInputStream(data);
    149         }
    150
    151     }
    152
    153     public void testPostFilePartUnknownLength() throws Exception {
    154
    155         this.server.setHttpService(new EchoService());
    156
    157         String enc = "ISO-8859-1";
    158         PostMethod method = new PostMethod();
    159         byte[] content = "Hello".getBytes(enc);
    160         MultipartRequestEntity entity = new MultipartRequestEntity(
    161             new Part[] {
    162                 new FilePart(
    163                     "param1",
    164                     new TestPartSource("filename.txt", content),
    165                      "text/plain",
    166                      enc) },
    167              method.getParams());
    168         method.setRequestEntity(entity);
    169
    170         client.executeMethod(method);
    171
    172         assertEquals(200,method.getStatusCode());
    173         String body = method.getResponseBodyAsString();
    174         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
    175         assertTrue(body.indexOf("Content-Type: text/plain; charset="+enc) >= 0);
    176         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
    177         assertTrue(body.indexOf("Hello") >= 0);
    178     }
    179
    180 }