2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.internal.http.multipart;
19 import junit.framework.TestCase;
20 import org.apache.http.Header;
21 import org.apache.http.util.EncodingUtils;
23 import java.io.BufferedWriter;
24 import java.io.ByteArrayOutputStream;
26 import java.io.FileWriter;
28 public class MultipartTest extends TestCase {
30 public void testParts() throws Exception {
31 StringBuffer filebuffer = new StringBuffer();
32 String filepartStr = "this is file part";
33 filebuffer.append(filepartStr);
34 File upload = File.createTempFile("Multipart", "test");
36 FileWriter outFile = new FileWriter(upload);
37 BufferedWriter out = new BufferedWriter(outFile);
39 out.write(filebuffer.toString());
45 Part[] parts = new Part[3];
46 parts[0] = new StringPart("stringpart", "PART1!!");
47 parts[1] = new FilePart(upload.getName(), upload);
48 parts[2] = new StringPart("stringpart", "PART2!!");
50 MultipartEntity me = new MultipartEntity(parts);
51 ByteArrayOutputStream os = new ByteArrayOutputStream();
53 Header h = me.getContentType();
54 String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary());
55 StringBuffer contentType = new StringBuffer("multipart/form-data");
56 contentType.append("; boundary=");
57 contentType.append(boundry);
58 assertEquals("Multipart content type error", contentType.toString(), h.getValue());
59 final String CRLF = "\r\n";
60 StringBuffer output = new StringBuffer();
63 output.append(boundry);
66 output.append("Content-Disposition: form-data; name=\"stringpart\"");
68 output.append("Content-Type: text/plain; charset=US-ASCII");
70 output.append("Content-Transfer-Encoding: 8bit");
73 output.append("PART1!!");
77 output.append(boundry);
80 output.append("Content-Disposition: form-data; name=\"");
81 output.append(upload.getName());
82 output.append("\"; filename=\"");
83 output.append(upload.getName());
87 output.append("Content-Type: application/octet-stream; charset=ISO-8859-1");
89 output.append("Content-Transfer-Encoding: binary");
92 output.append(filepartStr);
96 output.append(boundry);
99 output.append("Content-Disposition: form-data; name=\"stringpart\"");
101 output.append("Content-Type: text/plain; charset=US-ASCII");
103 output.append("Content-Transfer-Encoding: 8bit");
106 output.append("PART2!!");
110 output.append(boundry);
113 // System.out.print(output.toString());
114 assertEquals("Multipart content error", output.toString(), os.toString());
116 // System.out.print(os.toString());