@@ -18,16 +18,18 @@ public class MultipartUtilsTest {
18
18
public void testEmptyMultipartPayload ()throws IOException {
19
19
final MultipartPayload mP =new MultipartPayload ();
20
20
21
- final StringBuilder sb =new StringBuilder ();
21
+ final StringBuilder headersString =new StringBuilder ();
22
22
for (Map .Entry <String ,String >header :mP .getHeaders ().entrySet ()) {
23
- sb .append (header .getKey ())
23
+ headersString .append (header .getKey ())
24
24
.append (": " )
25
25
.append (header .getValue ())
26
26
.append ("\r \n " );
27
27
}
28
28
29
- sb .append ("\r \n " ).append (MultipartUtils .getPayload (mP ).toString ());
30
- assertEquals ("Content-Type: multipart/form-data; boundary=\" " +mP .getBoundary () +"\" \r \n \r \n " ,sb .toString ());
29
+ assertEquals ("Content-Type: multipart/form-data; boundary=\" " +mP .getBoundary () +"\" \r \n " ,
30
+ headersString .toString ());
31
+
32
+ assertEquals ("" ,MultipartUtils .getPayload (mP ).toString ());
31
33
}
32
34
33
35
@ Test
@@ -51,36 +53,34 @@ public void testSimpleMultipartPayload() throws IOException {
51
53
52
54
mP .setEpilogue ("This is the epilogue. It is also to be ignored." );
53
55
54
- final StringBuilder sb =new StringBuilder ();
56
+ final StringBuilder headersString =new StringBuilder ();
55
57
for (Map .Entry <String ,String >header :mP .getHeaders ().entrySet ()) {
56
- sb .append (header .getKey ())
58
+ headersString .append (header .getKey ())
57
59
.append (": " )
58
60
.append (header .getValue ())
59
61
.append ("\r \n " );
60
62
}
61
- sb . append ( " \r \n " ). append ( MultipartUtils . getPayload ( mP ). toString ());
63
+
62
64
assertEquals ("X-Header: X-Value\r \n "
63
65
+"Content-Disposition: Content-Disposition-Value\r \n "
64
- +"Content-Type: multipart/mixed; boundary=\" simple boundary\" \r \n "
65
- +"\r \n "
66
- +"This is the preamble. It is to be ignored, though it\n "
66
+ +"Content-Type: multipart/mixed; boundary=\" simple boundary\" \r \n " ,
67
+ headersString .toString ());
68
+
69
+ assertEquals ("This is the preamble. It is to be ignored, though it\n "
67
70
+"is a handy place for composition agents to include an\n "
68
71
+"explanatory note to non-MIME conformant readers."
69
- +"\r \n "
70
- +"--simple boundary\r \n "
72
+ +"\r \n --simple boundary\r \n "
71
73
+"\r \n "
72
74
+"This is implicitly typed plain US-ASCII text.\n "
73
75
+"It does NOT end with a linebreak."
74
- +"\r \n "
75
- +"--simple boundary\r \n "
76
+ +"\r \n --simple boundary\r \n "
76
77
+"Content-Type: text/plain; charset=us-ascii\r \n "
77
78
+"\r \n "
78
79
+"This is explicitly typed plain US-ASCII text.\n "
79
80
+"It DOES end with a linebreak.\n "
80
- +"\r \n "
81
- +"--simple boundary--\r \n "
82
- +"This is the epilogue. It is also to be ignored.\r \n " ,
83
- sb .toString ());
81
+ +"\r \n --simple boundary--"
82
+ +"\r \n This is the epilogue. It is also to be ignored." ,
83
+ MultipartUtils .getPayload (mP ).toString ());
84
84
}
85
85
86
86
@ Test
@@ -92,67 +92,58 @@ public void testCRLFMultipartPayload() throws IOException {
92
92
mP .addBodyPart ("It does end with a\\ r\\ n linebreak.\r \n " .getBytes ());
93
93
mP .addBodyPart ("the last one" .getBytes ());
94
94
95
- final StringBuilder sb =new StringBuilder ();
95
+ final StringBuilder headersString =new StringBuilder ();
96
96
for (Map .Entry <String ,String >header :mP .getHeaders ().entrySet ()) {
97
- sb .append (header .getKey ())
97
+ headersString .append (header .getKey ())
98
98
.append (": " )
99
99
.append (header .getValue ())
100
100
.append ("\r \n " );
101
101
}
102
- sb .append ("\r \n " ).append (MultipartUtils .getPayload (mP ).toString ());
103
- assertEquals ("Content-Type: multipart/form-data; boundary=\" simple-boundary\" \r \n "
104
- +"\r \n "
105
- +"\r \n "
106
- +"--simple-boundary\r \n "
102
+
103
+ assertEquals ("Content-Type: multipart/form-data; boundary=\" simple-boundary\" \r \n " ,headersString .toString ());
104
+
105
+ assertEquals ("--simple-boundary\r \n "
107
106
+"\r \n "
108
107
+"It does NOT end with a linebreak."
109
- +"\r \n "
110
- +"--simple-boundary\r \n "
108
+ +"\r \n --simple-boundary\r \n "
111
109
+"\r \n "
112
110
+"It does end with a\\ r linebreak.\r "
113
- +"\r \n "
114
- +"--simple-boundary\r \n "
111
+ +"\r \n --simple-boundary\r \n "
115
112
+"\r \n "
116
113
+"It does end with a\\ n linebreak.\n "
117
- +"\r \n "
118
- +"--simple-boundary\r \n "
114
+ +"\r \n --simple-boundary\r \n "
119
115
+"\r \n "
120
116
+"It does end with a\\ r\\ n linebreak.\r \n "
121
- +"\r \n "
122
- +"--simple-boundary\r \n "
117
+ +"\r \n --simple-boundary\r \n "
123
118
+"\r \n "
124
119
+"the last one"
125
- +"\r \n "
126
- +"--simple-boundary--\r \n " ,
127
- sb .toString ());
120
+ +"\r \n --simple-boundary--" ,
121
+ MultipartUtils .getPayload (mP ).toString ());
128
122
}
129
123
130
124
@ Test
131
125
public void testFileByteArrayBodyPartPayloadMultipartPayload ()throws IOException {
132
126
final MultipartPayload mP =new MultipartPayload ("testFileByteArrayBodyPartPayloadMultipartPayload boundary" );
133
127
mP .addBodyPart (new FileByteArrayBodyPartPayload ("fileContent" .getBytes (),"name" ,"filename.ext" ));
134
128
135
- final StringBuilder sb =new StringBuilder ();
129
+ final StringBuilder headersString =new StringBuilder ();
136
130
for (Map .Entry <String ,String >header :mP .getHeaders ().entrySet ()) {
137
- sb .append (header .getKey ())
131
+ headersString .append (header .getKey ())
138
132
.append (": " )
139
133
.append (header .getValue ())
140
134
.append ("\r \n " );
141
135
}
142
136
143
- sb .append ("\r \n " ).append (MultipartUtils .getPayload (mP ).toString ());
144
137
assertEquals ("Content-Type: multipart/form-data; "
145
- +"boundary=\" testFileByteArrayBodyPartPayloadMultipartPayload boundary\" \r \n "
146
- + " \r \n "
147
- + " \r \n "
148
- + "--testFileByteArrayBodyPartPayloadMultipartPayload boundary\r \n "
138
+ +"boundary=\" testFileByteArrayBodyPartPayloadMultipartPayload boundary\" \r \n " ,
139
+ headersString . toString ());
140
+
141
+ assertEquals ( "--testFileByteArrayBodyPartPayloadMultipartPayload boundary\r \n "
149
142
+"Content-Disposition: form-data; name=\" name\" ; filename=\" filename.ext\" \r \n "
150
143
+"\r \n "
151
144
+"fileContent"
152
- +"\r \n "
153
- +"--testFileByteArrayBodyPartPayloadMultipartPayload boundary--\r \n " ,
154
- sb .toString ()
155
- );
145
+ +"\r \n --testFileByteArrayBodyPartPayloadMultipartPayload boundary--" ,
146
+ MultipartUtils .getPayload (mP ).toString ());
156
147
}
157
148
158
149
@ Test
@@ -201,63 +192,54 @@ public void testComplexMultipartPayload() throws IOException {
201
192
+"\n "
202
193
+"... Additional text in ISO-8859-1 goes here ...\n " ).getBytes (),"message/rfc822" );
203
194
204
- final StringBuilder sb =new StringBuilder ();
195
+ final StringBuilder headersString =new StringBuilder ();
205
196
for (Map .Entry <String ,String >header :mP .getHeaders ().entrySet ()) {
206
- sb .append (header .getKey ())
197
+ headersString .append (header .getKey ())
207
198
.append (": " )
208
199
.append (header .getValue ())
209
200
.append ("\r \n " );
210
201
}
211
- sb .append ("\r \n " ).append (MultipartUtils .getPayload (mP ).toString ());
212
- assertEquals ("Content-Type: multipart/mixed; boundary=\" unique-boundary-1\" \r \n "
213
- +"\r \n "
214
- +"This is the preamble area of a multipart message.\n "
202
+ assertEquals ("Content-Type: multipart/mixed; boundary=\" unique-boundary-1\" \r \n " ,headersString .toString ());
203
+
204
+ assertEquals ("This is the preamble area of a multipart message.\n "
215
205
+"Mail readers that understand multipart format\n "
216
206
+"should ignore this preamble.\n "
217
207
+"\n "
218
208
+"If you are reading this text, you might want to\n "
219
209
+"consider changing to a mail reader that understands\n "
220
210
+"how to properly display multipart messages.\n "
221
- +"\r \n "
222
- +"--unique-boundary-1\r \n "
211
+ +"\r \n --unique-boundary-1\r \n "
223
212
+"\r \n "
224
213
+"... Some text appears here ..."
225
- +"\r \n "
226
- +"--unique-boundary-1\r \n "
214
+ +"\r \n --unique-boundary-1\r \n "
227
215
+"Content-Type: text/plain; charset=US-ASCII\r \n "
228
216
+"\r \n "
229
217
+"This could have been part of the previous part, but\n "
230
218
+"illustrates explicit versus implicit typing of body\n "
231
219
+"parts.\n "
232
- +"\r \n "
233
- +"--unique-boundary-1\r \n "
220
+ +"\r \n --unique-boundary-1\r \n "
234
221
+"Content-Type: multipart/parallel; boundary=\" unique-boundary-2\" \r \n "
235
- +"\r \n "
236
- +"--unique-boundary-2\r \n "
222
+ +"\r \n --unique-boundary-2\r \n "
237
223
+"Content-Type: audio/basic\r \n "
238
224
+"Content-Transfer-Encoding: base64\r \n "
239
225
+"\r \n "
240
226
+"... base64-encoded 8000 Hz single-channel\n "
241
227
+" mu-law-format audio data goes here ..."
242
- +"\r \n "
243
- +"--unique-boundary-2\r \n "
228
+ +"\r \n --unique-boundary-2\r \n "
244
229
+"Content-Type: image/jpeg\r \n "
245
230
+"Content-Transfer-Encoding: base64\r \n "
246
231
+"\r \n "
247
232
+"... base64-encoded image data goes here ..."
248
- +"\r \n "
249
- +"--unique-boundary-2--\r \n "
250
- +"\r \n "
251
- +"--unique-boundary-1\r \n "
233
+ +"\r \n --unique-boundary-2--"
234
+ +"\r \n --unique-boundary-1\r \n "
252
235
+"Content-Type: text/enriched\r \n "
253
236
+"\r \n "
254
237
+"This is <bold><italic>enriched.</italic></bold>\n "
255
238
+"<smaller>as defined in RFC 1896</smaller>\n "
256
239
+"\n "
257
240
+"Isn't it\n "
258
241
+"<bigger><bigger>cool?</bigger></bigger>\n "
259
- +"\r \n "
260
- +"--unique-boundary-1\r \n "
242
+ +"\r \n --unique-boundary-1\r \n "
261
243
+"Content-Type: message/rfc822\r \n "
262
244
+"\r \n "
263
245
+"From: (mailbox in US-ASCII)\n "
@@ -267,9 +249,8 @@ public void testComplexMultipartPayload() throws IOException {
267
249
+"Content-Transfer-Encoding: Quoted-printable\n "
268
250
+"\n "
269
251
+"... Additional text in ISO-8859-1 goes here ...\n "
270
- +"\r \n "
271
- +"--unique-boundary-1--\r \n " ,
272
- sb .toString ());
252
+ +"\r \n --unique-boundary-1--" ,
253
+ MultipartUtils .getPayload (mP ).toString ());
273
254
}
274
255
275
256
@ Test