Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody(s1, Charset.forName("ISO-8859-1")));
FormBodyPart p2 = new FormBodyPart(
"field2",
new StringBody(s2, Charset.forName("KOI8-R")));
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
multipart.writeTo(out1);
out1.close();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
out2.write((
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n").getBytes("US-ASCII"));
out2.write(s1.getBytes("ISO-8859-1"));
out2.write(("\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"\r\n" +
"Content-Type: text/plain; charset=KOI8-R\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n").getBytes("US-ASCII"));
out2.write(s2.getBytes("KOI8-R"));
out2.write(("\r\n" +
"--foo--\r\n").getBytes("US-ASCII"));
out2.close();
byte[] actual = out1.toByteArray();
byte[] expected = out2.toByteArray();
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; i++) {
assertEquals(expected[i], actual[i]);
}
assertEquals(expected.length, multipart.getTotalLength());
}