1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.internal.http.multipart;
18
19import junit.framework.TestCase;
20import org.apache.http.Header;
21import org.apache.http.util.EncodingUtils;
22
23import java.io.BufferedWriter;
24import java.io.ByteArrayOutputStream;
25import java.io.File;
26import java.io.FileWriter;
27
28public class MultipartTest extends TestCase {
29
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");
35
36        FileWriter outFile = new FileWriter(upload);
37        BufferedWriter out = new BufferedWriter(outFile);
38        try {
39            out.write(filebuffer.toString());
40            out.flush();
41        } finally {
42            out.close();
43        }
44
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!!");
49
50        MultipartEntity me = new MultipartEntity(parts);
51        ByteArrayOutputStream os = new ByteArrayOutputStream();
52        me.writeTo(os);
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();
61
62        output.append("--");
63        output.append(boundry);
64        output.append(CRLF);
65
66        output.append("Content-Disposition: form-data; name=\"stringpart\"");
67        output.append(CRLF);
68        output.append("Content-Type: text/plain; charset=US-ASCII");
69        output.append(CRLF);
70        output.append("Content-Transfer-Encoding: 8bit");
71        output.append(CRLF);
72        output.append(CRLF);
73        output.append("PART1!!");
74        output.append(CRLF);
75
76        output.append("--");
77        output.append(boundry);
78        output.append(CRLF);
79
80        output.append("Content-Disposition: form-data; name=\"");
81        output.append(upload.getName());
82        output.append("\"; filename=\"");
83        output.append(upload.getName());
84        output.append("\"");
85
86        output.append(CRLF);
87        output.append("Content-Type: application/octet-stream; charset=ISO-8859-1");
88        output.append(CRLF);
89        output.append("Content-Transfer-Encoding: binary");
90        output.append(CRLF);
91        output.append(CRLF);
92        output.append(filepartStr);
93        output.append(CRLF);
94
95        output.append("--");
96        output.append(boundry);
97        output.append(CRLF);
98
99        output.append("Content-Disposition: form-data; name=\"stringpart\"");
100        output.append(CRLF);
101        output.append("Content-Type: text/plain; charset=US-ASCII");
102        output.append(CRLF);
103        output.append("Content-Transfer-Encoding: 8bit");
104        output.append(CRLF);
105        output.append(CRLF);
106        output.append("PART2!!");
107        output.append(CRLF);
108
109        output.append("--");
110        output.append(boundry);
111        output.append("--");
112        output.append(CRLF);
113        // System.out.print(output.toString());
114        assertEquals("Multipart content error", output.toString(), os.toString());
115
116        // System.out.print(os.toString());
117    }
118}
119