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  
33  import org.apache.http.Header;
34  import org.apache.http.HeaderElement;
35  import org.apache.http.HttpEntity;
36  import org.apache.http.NameValuePair;
37  import org.apache.http.entity.ContentType;
38  import org.apache.http.entity.mime.content.InputStreamBody;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  public class TestMultipartFormHttpEntity {
43  
44      @Test
45      public void testExplictContractorParams() throws Exception {
46          final HttpEntity entity = MultipartEntityBuilder.create()
47                  .setLaxMode()
48                  .setBoundary("whatever")
49                  .setCharset(MIME.UTF8_CHARSET)
50                  .build();
51  
52          Assert.assertNull(entity.getContentEncoding());
53          Assert.assertNotNull(entity.getContentType());
54          final Header header = entity.getContentType();
55          final HeaderElement[] elems = header.getElements();
56          Assert.assertNotNull(elems);
57          Assert.assertEquals(1, elems.length);
58  
59          final HeaderElement elem = elems[0];
60          Assert.assertEquals("multipart/form-data", elem.getName());
61          final NameValuePair p1 = elem.getParameterByName("boundary");
62          Assert.assertNotNull(p1);
63          Assert.assertEquals("whatever", p1.getValue());
64          final NameValuePair p2 = elem.getParameterByName("charset");
65          Assert.assertNotNull(p2);
66          Assert.assertEquals("UTF-8", p2.getValue());
67      }
68  
69      @Test
70      public void testImplictContractorParams() throws Exception {
71          final HttpEntity entity = MultipartEntityBuilder.create().build();
72          Assert.assertNull(entity.getContentEncoding());
73          Assert.assertNotNull(entity.getContentType());
74          final Header header = entity.getContentType();
75          final HeaderElement[] elems = header.getElements();
76          Assert.assertNotNull(elems);
77          Assert.assertEquals(1, elems.length);
78  
79          final HeaderElement elem = elems[0];
80          Assert.assertEquals("multipart/form-data", elem.getName());
81          final NameValuePair p1 = elem.getParameterByName("boundary");
82          Assert.assertNotNull(p1);
83  
84          final String boundary = p1.getValue();
85          Assert.assertNotNull(boundary);
86  
87          Assert.assertTrue(boundary.length() >= 30);
88          Assert.assertTrue(boundary.length() <= 40);
89  
90          final NameValuePair p2 = elem.getParameterByName("charset");
91          Assert.assertNull(p2);
92      }
93  
94      @Test
95      public void testRepeatable() throws Exception {
96          final HttpEntity entity = MultipartEntityBuilder.create()
97                  .addTextBody("p1", "blah blah", ContentType.DEFAULT_TEXT)
98                  .addTextBody("p2", "yada yada", ContentType.DEFAULT_TEXT)
99                  .build();
100         Assert.assertTrue(entity.isRepeatable());
101         Assert.assertFalse(entity.isChunked());
102         Assert.assertFalse(entity.isStreaming());
103 
104         final 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         final HttpEntity entity = MultipartEntityBuilder.create()
129             .addPart("p1", new InputStreamBody(
130                 new ByteArrayInputStream("blah blah".getBytes()), ContentType.DEFAULT_BINARY))
131             .addPart("p2", new InputStreamBody(
132                 new ByteArrayInputStream("yada yada".getBytes()), ContentType.DEFAULT_BINARY))
133             .build();
134         Assert.assertFalse(entity.isRepeatable());
135         Assert.assertTrue(entity.isChunked());
136         Assert.assertTrue(entity.isStreaming());
137 
138         Assert.assertTrue(entity.getContentLength() == -1);
139     }
140 
141 }