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.hc.client5.http.entity.mime;
29  
30  import java.io.File;
31  import java.util.Arrays;
32  import java.util.List;
33  
34  import org.apache.hc.core5.http.ContentType;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestMultipartPartBuilder {
39  
40      @Test
41      public void testBuildBodyPartBasics() throws Exception {
42          final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
43          final MultipartPart part = MultipartPartBuilder.create()
44                  .setBody(stringBody)
45                  .build();
46          Assert.assertNotNull(part);
47          Assert.assertEquals(stringBody, part.getBody());
48          final Header header = part.getHeader();
49          Assert.assertNotNull(header);
50          assertFields(Arrays.asList(
51                          new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
52                  header.getFields());
53      }
54  
55      @Test
56      public void testBuildBodyPartMultipleBuilds() throws Exception {
57          final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
58          final MultipartPartBuilder builder = MultipartPartBuilder.create();
59          final MultipartPart part1 = builder
60                  .setBody(stringBody)
61                  .build();
62          Assert.assertNotNull(part1);
63          Assert.assertEquals(stringBody, part1.getBody());
64          final Header header1 = part1.getHeader();
65          Assert.assertNotNull(header1);
66          assertFields(Arrays.asList(
67                          new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
68                  header1.getFields());
69          final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
70          final MultipartPart part2 = builder
71                  .setBody(fileBody)
72                  .build();
73  
74          Assert.assertNotNull(part2);
75          Assert.assertEquals(fileBody, part2.getBody());
76          final Header header2 = part2.getHeader();
77          Assert.assertNotNull(header2);
78          assertFields(Arrays.asList(
79                          new MimeField("Content-Type", "application/octet-stream")),
80                  header2.getFields());
81      }
82  
83      @Test
84      public void testBuildBodyPartCustomHeaders() throws Exception {
85          final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
86          final MultipartPartBuilder builder = MultipartPartBuilder.create(stringBody);
87          final MultipartPart part1 = builder
88                  .addHeader("header1", "blah")
89                  .addHeader("header3", "blah")
90                  .addHeader("header3", "blah")
91                  .addHeader("header3", "blah")
92                  .addHeader("header3", "blah")
93                  .addHeader("header3", "blah")
94                  .build();
95  
96          Assert.assertNotNull(part1);
97          final Header header1 = part1.getHeader();
98          Assert.assertNotNull(header1);
99  
100         assertFields(Arrays.asList(
101                 new MimeField("header1", "blah"),
102                 new MimeField("header3", "blah"),
103                 new MimeField("header3", "blah"),
104                 new MimeField("header3", "blah"),
105                 new MimeField("header3", "blah"),
106                 new MimeField("header3", "blah"),
107                 new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
108                 header1.getFields());
109 
110         final MultipartPart part2 = builder
111                 .addHeader("header2", "yada")
112                 .removeHeaders("header3")
113                 .build();
114 
115         Assert.assertNotNull(part2);
116         final Header header2 = part2.getHeader();
117         Assert.assertNotNull(header2);
118 
119         assertFields(Arrays.asList(
120                         new MimeField("header1", "blah"),
121                         new MimeField("header2", "yada"),
122                         new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
123                 header2.getFields());
124 
125         final MultipartPart part3 = builder
126                 .addHeader("Content-Disposition", "disposition stuff")
127                 .addHeader("Content-Type", "type stuff")
128                 .addHeader("Content-Transfer-Encoding", "encoding stuff")
129                 .build();
130 
131         Assert.assertNotNull(part3);
132         final Header header3 = part3.getHeader();
133         Assert.assertNotNull(header3);
134 
135         assertFields(Arrays.asList(
136                         new MimeField("header1", "blah"),
137                         new MimeField("header2", "yada"),
138                         new MimeField("Content-Disposition", "disposition stuff"),
139                         new MimeField("Content-Type", "type stuff"),
140                         new MimeField("Content-Transfer-Encoding", "encoding stuff")),
141                 header3.getFields());
142 
143     }
144 
145     private static void assertFields(final List<MimeField> expected, final List<MimeField> result) {
146         Assert.assertNotNull(result);
147         Assert.assertEquals(expected.size(), result.size());
148         for (int i = 0; i < expected.size(); i++) {
149             final MimeField expectedField = expected.get(i);
150             final MimeField resultField = result.get(i);
151             Assert.assertNotNull(resultField);
152             Assert.assertEquals(expectedField.getName(), resultField.getName());
153             Assert.assertEquals(expectedField.getBody(), resultField.getBody());
154         }
155     }
156 
157 }