1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.Arrays;
33 import java.util.List;
34
35 import org.apache.http.entity.ContentType;
36 import org.apache.http.entity.mime.content.FileBody;
37 import org.apache.http.entity.mime.content.InputStreamBody;
38 import org.apache.http.entity.mime.content.StringBody;
39 import org.junit.Assert;
40 import org.junit.Test;
41
42 public class TestFormBodyPartBuilder {
43
44 @Test
45 public void testBuildBodyPartBasics() throws Exception {
46 final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
47 final FormBodyPart bodyPart = FormBodyPartBuilder.create()
48 .setName("blah")
49 .setBody(stringBody)
50 .build();
51 Assert.assertNotNull(bodyPart);
52 Assert.assertEquals("blah", bodyPart.getName());
53 Assert.assertEquals(stringBody, bodyPart.getBody());
54 final Header header = bodyPart.getHeader();
55 Assert.assertNotNull(header);
56 assertFields(Arrays.asList(
57 new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
58 new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
59 new MinimalField("Content-Transfer-Encoding", "8bit")),
60 header.getFields());
61 }
62
63 @Test
64 public void testCharacterStuffing() throws Exception {
65 final FormBodyPartBuilder builder = FormBodyPartBuilder.create();
66 final InputStreamBody fileBody = new InputStreamBody(new ByteArrayInputStream(
67 "hello world".getBytes("UTF-8")), "stuff_with \"quotes\" and \\slashes\\.bin");
68 final FormBodyPart bodyPart2 = builder
69 .setName("yada_with \"quotes\" and \\slashes\\")
70 .setBody(fileBody)
71 .build();
72
73 Assert.assertNotNull(bodyPart2);
74 Assert.assertEquals("yada_with \"quotes\" and \\slashes\\", bodyPart2.getName());
75 Assert.assertEquals(fileBody, bodyPart2.getBody());
76 final Header header2 = bodyPart2.getHeader();
77 Assert.assertNotNull(header2);
78 assertFields(Arrays.asList(
79 new MinimalField("Content-Disposition", "form-data; name=\"yada_with \\\"quotes\\\" " +
80 "and \\\\slashes\\\\\"; filename=\"stuff_with \\\"quotes\\\" and \\\\slashes\\\\.bin\""),
81 new MinimalField("Content-Type", "application/octet-stream"),
82 new MinimalField("Content-Transfer-Encoding", "binary")),
83 header2.getFields());
84 }
85
86 @Test
87 public void testBuildBodyPartMultipleBuilds() throws Exception {
88 final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
89 final FormBodyPartBuilder builder = FormBodyPartBuilder.create();
90 final FormBodyPart bodyPart1 = builder
91 .setName("blah")
92 .setBody(stringBody)
93 .build();
94 Assert.assertNotNull(bodyPart1);
95 Assert.assertEquals("blah", bodyPart1.getName());
96 Assert.assertEquals(stringBody, bodyPart1.getBody());
97 final Header header1 = bodyPart1.getHeader();
98 Assert.assertNotNull(header1);
99 assertFields(Arrays.asList(
100 new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
101 new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
102 new MinimalField("Content-Transfer-Encoding", "8bit")),
103 header1.getFields());
104 final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
105 final FormBodyPart bodyPart2 = builder
106 .setName("yada")
107 .setBody(fileBody)
108 .build();
109
110 Assert.assertNotNull(bodyPart2);
111 Assert.assertEquals("yada", bodyPart2.getName());
112 Assert.assertEquals(fileBody, bodyPart2.getBody());
113 final Header header2 = bodyPart2.getHeader();
114 Assert.assertNotNull(header2);
115 assertFields(Arrays.asList(
116 new MinimalField("Content-Disposition", "form-data; name=\"yada\"; filename=\"stuff.bin\""),
117 new MinimalField("Content-Type", "application/octet-stream"),
118 new MinimalField("Content-Transfer-Encoding", "binary")),
119 header2.getFields());
120 }
121
122 @Test
123 public void testBuildBodyPartCustomHeaders() throws Exception {
124 final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
125 final FormBodyPartBuilder builder = FormBodyPartBuilder.create("blah", stringBody);
126 final FormBodyPart bodyPart1 = builder
127 .addField("header1", "blah")
128 .addField("header3", "blah")
129 .addField("header3", "blah")
130 .addField("header3", "blah")
131 .addField("header3", "blah")
132 .addField("header3", "blah")
133 .build();
134
135 Assert.assertNotNull(bodyPart1);
136 final Header header1 = bodyPart1.getHeader();
137 Assert.assertNotNull(header1);
138
139 assertFields(Arrays.asList(
140 new MinimalField("header1", "blah"),
141 new MinimalField("header3", "blah"),
142 new MinimalField("header3", "blah"),
143 new MinimalField("header3", "blah"),
144 new MinimalField("header3", "blah"),
145 new MinimalField("header3", "blah"),
146 new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
147 new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
148 new MinimalField("Content-Transfer-Encoding", "8bit")),
149 header1.getFields());
150
151 final FormBodyPart bodyPart2 = builder
152 .setField("header2", "yada")
153 .removeFields("header3")
154 .build();
155
156 Assert.assertNotNull(bodyPart2);
157 final Header header2 = bodyPart2.getHeader();
158 Assert.assertNotNull(header2);
159
160 assertFields(Arrays.asList(
161 new MinimalField("header1", "blah"),
162 new MinimalField("header2", "yada"),
163 new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
164 new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
165 new MinimalField("Content-Transfer-Encoding", "8bit")),
166 header2.getFields());
167
168 final FormBodyPart bodyPart3 = builder
169 .addField("Content-Disposition", "disposition stuff")
170 .addField("Content-Type", "type stuff")
171 .addField("Content-Transfer-Encoding", "encoding stuff")
172 .build();
173
174 Assert.assertNotNull(bodyPart3);
175 final Header header3 = bodyPart3.getHeader();
176 Assert.assertNotNull(header3);
177
178 assertFields(Arrays.asList(
179 new MinimalField("header1", "blah"),
180 new MinimalField("header2", "yada"),
181 new MinimalField("Content-Disposition", "disposition stuff"),
182 new MinimalField("Content-Type", "type stuff"),
183 new MinimalField("Content-Transfer-Encoding", "encoding stuff")),
184 header3.getFields());
185
186 }
187
188 private static void assertFields(final List<MinimalField> expected, final List<MinimalField> result) {
189 Assert.assertNotNull(result);
190 Assert.assertEquals(expected.size(), result.size());
191 for (int i = 0; i < expected.size(); i++) {
192 final MinimalField expectedField = expected.get(i);
193 final MinimalField resultField = result.get(i);
194 Assert.assertNotNull(resultField);
195 Assert.assertEquals(expectedField.getName(), resultField.getName());
196 Assert.assertEquals(expectedField.getBody(), resultField.getBody());
197 }
198 }
199
200 }