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