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.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestMultipartEntityBuilder {
39  
40      @Test
41      public void testBasics() throws Exception {
42          final MultipartFormEntity entity = MultipartEntityBuilder.create().buildEntity();
43          Assert.assertNotNull(entity);
44          Assert.assertTrue(entity.getMultipart() instanceof HttpStrictMultipart);
45          Assert.assertEquals(0, entity.getMultipart().getBodyParts().size());
46      }
47  
48      @Test
49      public void testMultipartOptions() throws Exception {
50          final MultipartFormEntity entity = MultipartEntityBuilder.create()
51                  .setBoundary("blah-blah")
52                  .setCharset(Consts.UTF_8)
53                  .setLaxMode()
54                  .buildEntity();
55          Assert.assertNotNull(entity);
56          Assert.assertTrue(entity.getMultipart() instanceof HttpBrowserCompatibleMultipart);
57          Assert.assertEquals("blah-blah", entity.getMultipart().getBoundary());
58          Assert.assertEquals(Consts.UTF_8, entity.getMultipart().getCharset());
59      }
60  
61      @Test
62      public void testAddBodyParts() throws Exception {
63          final MultipartFormEntity entity = MultipartEntityBuilder.create()
64                  .addTextBody("p1", "stuff")
65                  .addBinaryBody("p2", new File("stuff"))
66                  .addBinaryBody("p3", new byte[] {})
67                  .addBinaryBody("p4", new ByteArrayInputStream(new byte[] {}))
68                  .buildEntity();
69          Assert.assertNotNull(entity);
70          final List<FormBodyPart> bodyParts = entity.getMultipart().getBodyParts();
71          Assert.assertNotNull(bodyParts);
72          Assert.assertEquals(4, bodyParts.size());
73      }
74  
75  }