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