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.File;
32  import java.util.List;
33  
34  import org.apache.http.Consts;
35  import org.apache.http.Header;
36  import org.apache.http.entity.ContentType;
37  import org.apache.http.message.BasicNameValuePair;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  public class TestMultipartEntityBuilder {
42  
43      @Test
44      public void testBasics() throws Exception {
45          final MultipartFormEntity entity = MultipartEntityBuilder.create().buildEntity();
46          Assert.assertNotNull(entity);
47          Assert.assertTrue(entity.getMultipart() instanceof HttpStrictMultipart);
48          Assert.assertEquals(0, entity.getMultipart().getBodyParts().size());
49      }
50  
51      @Test
52      public void testMultipartOptions() throws Exception {
53          final MultipartFormEntity entity = MultipartEntityBuilder.create()
54                  .setBoundary("blah-blah")
55                  .setCharset(Consts.UTF_8)
56                  .setLaxMode()
57                  .buildEntity();
58          Assert.assertNotNull(entity);
59          Assert.assertTrue(entity.getMultipart() instanceof HttpBrowserCompatibleMultipart);
60          Assert.assertEquals("blah-blah", entity.getMultipart().boundary);
61          Assert.assertEquals(Consts.UTF_8, entity.getMultipart().charset);
62      }
63  
64      @Test
65      public void testAddBodyParts() throws Exception {
66          final MultipartFormEntity entity = MultipartEntityBuilder.create()
67                  .addTextBody("p1", "stuff")
68                  .addBinaryBody("p2", new File("stuff"))
69                  .addBinaryBody("p3", new byte[]{})
70                  .addBinaryBody("p4", new ByteArrayInputStream(new byte[]{}))
71                  .buildEntity();
72          Assert.assertNotNull(entity);
73          final List<FormBodyPart> bodyParts = entity.getMultipart().getBodyParts();
74          Assert.assertNotNull(bodyParts);
75          Assert.assertEquals(4, bodyParts.size());
76      }
77  
78      @Test
79      public void testMultipartCustomContentType() throws Exception {
80          final MultipartFormEntity entity = MultipartEntityBuilder.create()
81                  .setContentType(ContentType.APPLICATION_XML)
82                  .setBoundary("blah-blah")
83                  .setCharset(Consts.UTF_8)
84                  .setLaxMode()
85                  .buildEntity();
86          Assert.assertNotNull(entity);
87          final Header contentType = entity.getContentType();
88          Assert.assertNotNull(contentType);
89          Assert.assertEquals("application/xml; boundary=blah-blah; charset=UTF-8", contentType.getValue());
90      }
91  
92      @Test
93      public void testMultipartContentTypeParameter() throws Exception {
94          final MultipartFormEntity entity = MultipartEntityBuilder.create()
95                  .setContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
96                          new BasicNameValuePair("boundary", "yada-yada"),
97                          new BasicNameValuePair("charset", "ascii")))
98                  .buildEntity();
99          Assert.assertNotNull(entity);
100         final Header contentType = entity.getContentType();
101         Assert.assertNotNull(contentType);
102         Assert.assertEquals("multipart/form-data; boundary=yada-yada; charset=US-ASCII",
103                 contentType.getValue());
104         Assert.assertEquals("yada-yada", entity.getMultipart().boundary);
105         Assert.assertEquals(Consts.ASCII, entity.getMultipart().charset);
106     }
107 
108     @Test
109     public void testMultipartCustomContentTypeParameterOverrides() throws Exception {
110         final MultipartFormEntity entity = MultipartEntityBuilder.create()
111                 .setContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
112                         new BasicNameValuePair("boundary", "yada-yada"),
113                         new BasicNameValuePair("charset", "ascii"),
114                         new BasicNameValuePair("my", "stuff")))
115                 .setBoundary("blah-blah")
116                 .setCharset(Consts.UTF_8)
117                 .setLaxMode()
118                 .buildEntity();
119         Assert.assertNotNull(entity);
120         final Header contentType = entity.getContentType();
121         Assert.assertNotNull(contentType);
122         Assert.assertEquals("multipart/form-data; boundary=blah-blah; charset=UTF-8; my=stuff",
123                 contentType.getValue());
124     }
125 
126 }