MIME multipart/form-data: non-ASCII content and header tests

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@616730 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-01-30 12:00:31 +00:00
parent 275d00cb28
commit aa59688e66
1 changed files with 131 additions and 0 deletions

View File

@ -283,4 +283,135 @@ public class TestMultipartForm extends TestCase {
tmpfile.delete();
}
static final int SWISS_GERMAN_HELLO [] = {
0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
};
static final int RUSSIAN_HELLO [] = {
0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
0x432, 0x435, 0x442
};
private static String constructString(int [] unicodeChars) {
StringBuffer buffer = new StringBuffer();
if (unicodeChars != null) {
for (int i = 0; i < unicodeChars.length; i++) {
buffer.append((char)unicodeChars[i]);
}
}
return buffer.toString();
}
public void testMultipartFormBrowserCompatibleNonASCIIHeaders() throws Exception {
String s1 = constructString(SWISS_GERMAN_HELLO);
String s2 = constructString(RUSSIAN_HELLO);
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; charset=UTF-8; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
tmpfile.deleteOnExit();
Writer writer = new FileWriter(tmpfile);
try {
writer.append("some random whatever");
} finally {
writer.close();
}
HttpMultipart multipart = new HttpMultipart();
multipart.setParent(message);
FormBodyPart p1 = new FormBodyPart(
"field1",
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
FormBodyPart p2 = new FormBodyPart(
"field2",
new InputStreamBody(new FileInputStream(tmpfile), s2 + ".tmp"));
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
multipart.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipart.writeTo(out);
out.close();
String expected = "\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"; " +
"filename=\"" + s1 + ".tmp\"\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
"filename=\"" + s2 + ".tmp\"\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo--\r\n" +
"\r\n";
String s = out.toString("UTF-8");
assertEquals(expected, s);
tmpfile.delete();
}
public void testMultipartFormStringPartsMultiCharsets() throws Exception {
String s1 = constructString(SWISS_GERMAN_HELLO);
String s2 = constructString(RUSSIAN_HELLO);
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
message.setHeader(header);
Multipart multipart = new HttpMultipart();
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(("\r\n" +
"--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" +
"\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]);
}
}
}