View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.entity.mime;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.nio.charset.Charset;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.NameValuePair;
37  import org.apache.http.entity.mime.HttpMultipartMode;
38  import org.apache.http.entity.mime.MultipartEntity;
39  import org.apache.http.entity.mime.content.InputStreamBody;
40  import org.apache.http.entity.mime.content.StringBody;
41  import org.junit.Assert;
42  import org.junit.Test;
43  
44  public class TestMultipartFormHttpEntity {
45  
46      @Test
47      public void testExplictContractorParams() throws Exception {
48          MultipartEntity entity = new MultipartEntity(
49                  HttpMultipartMode.BROWSER_COMPATIBLE,
50                  "whatever",
51                  Charset.forName("UTF-8"));
52  
53          Assert.assertNull(entity.getContentEncoding());
54          Assert.assertNotNull(entity.getContentType());
55          Header header = entity.getContentType();
56          HeaderElement[] elems = header.getElements();
57          Assert.assertNotNull(elems);
58          Assert.assertEquals(1, elems.length);
59  
60          HeaderElement elem = elems[0];
61          Assert.assertEquals("multipart/form-data", elem.getName());
62          NameValuePair p1 = elem.getParameterByName("boundary");
63          Assert.assertNotNull(p1);
64          Assert.assertEquals("whatever", p1.getValue());
65          NameValuePair p2 = elem.getParameterByName("charset");
66          Assert.assertNotNull(p2);
67          Assert.assertEquals("UTF-8", p2.getValue());
68      }
69  
70      @Test
71      public void testImplictContractorParams() throws Exception {
72          MultipartEntity entity = new MultipartEntity();
73          Assert.assertNull(entity.getContentEncoding());
74          Assert.assertNotNull(entity.getContentType());
75          Header header = entity.getContentType();
76          HeaderElement[] elems = header.getElements();
77          Assert.assertNotNull(elems);
78          Assert.assertEquals(1, elems.length);
79  
80          HeaderElement elem = elems[0];
81          Assert.assertEquals("multipart/form-data", elem.getName());
82          NameValuePair p1 = elem.getParameterByName("boundary");
83          Assert.assertNotNull(p1);
84  
85          String boundary = p1.getValue();
86          Assert.assertNotNull(boundary);
87  
88          Assert.assertTrue(boundary.length() >= 30);
89          Assert.assertTrue(boundary.length() <= 40);
90  
91          NameValuePair p2 = elem.getParameterByName("charset");
92          Assert.assertNull(p2);
93      }
94  
95      @Test
96      public void testRepeatable() throws Exception {
97          MultipartEntity entity = new MultipartEntity();
98          entity.addPart("p1", new StringBody("blah blah"));
99          entity.addPart("p2", new StringBody("yada yada"));
100         Assert.assertTrue(entity.isRepeatable());
101         Assert.assertFalse(entity.isChunked());
102         Assert.assertFalse(entity.isStreaming());
103 
104         long len = entity.getContentLength();
105         Assert.assertTrue(len == entity.getContentLength());
106 
107         ByteArrayOutputStream out = new ByteArrayOutputStream();
108         entity.writeTo(out);
109         out.close();
110 
111         byte[] bytes = out.toByteArray();
112         Assert.assertNotNull(bytes);
113         Assert.assertTrue(bytes.length == len);
114 
115         Assert.assertTrue(len == entity.getContentLength());
116 
117         out = new ByteArrayOutputStream();
118         entity.writeTo(out);
119         out.close();
120 
121         bytes = out.toByteArray();
122         Assert.assertNotNull(bytes);
123         Assert.assertTrue(bytes.length == len);
124     }
125 
126     @Test
127     public void testNonRepeatable() throws Exception {
128         MultipartEntity entity = new MultipartEntity();
129         entity.addPart("p1", new InputStreamBody(
130                 new ByteArrayInputStream("blah blah".getBytes()), null));
131         entity.addPart("p2", new InputStreamBody(
132                 new ByteArrayInputStream("yada yada".getBytes()), null));
133         Assert.assertFalse(entity.isRepeatable());
134         Assert.assertTrue(entity.isChunked());
135         Assert.assertTrue(entity.isStreaming());
136 
137         Assert.assertTrue(entity.getContentLength() == -1);
138     }
139 
140 }