1 package com.android.internal.http.multipart; 2 3 import junit.framework.TestCase; 4 import org.apache.http.Header; 5 import org.apache.http.util.EncodingUtils; 6 7 import java.io.BufferedWriter; 8 import java.io.ByteArrayOutputStream; 9 import java.io.File; 10 import java.io.FileWriter; 11 12 public class MultipartTest extends TestCase { 13 testParts()14 public void testParts() throws Exception { 15 StringBuffer filebuffer = new StringBuffer(); 16 String filepartStr = "this is file part"; 17 filebuffer.append(filepartStr); 18 File upload = File.createTempFile("Multipart", "test"); 19 20 FileWriter outFile = new FileWriter(upload); 21 BufferedWriter out = new BufferedWriter(outFile); 22 try { 23 out.write(filebuffer.toString()); 24 out.flush(); 25 } finally { 26 out.close(); 27 } 28 29 Part[] parts = new Part[3]; 30 parts[0] = new StringPart("stringpart", "PART1!!"); 31 parts[1] = new FilePart(upload.getName(), upload); 32 parts[2] = new StringPart("stringpart", "PART2!!"); 33 34 MultipartEntity me = new MultipartEntity(parts); 35 ByteArrayOutputStream os = new ByteArrayOutputStream(); 36 me.writeTo(os); 37 Header h = me.getContentType(); 38 String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary()); 39 StringBuffer contentType = new StringBuffer("multipart/form-data"); 40 contentType.append("; boundary="); 41 contentType.append(boundry); 42 assertEquals("Multipart content type error", contentType.toString(), h.getValue()); 43 final String CRLF = "\r\n"; 44 StringBuffer output = new StringBuffer(); 45 46 output.append("--"); 47 output.append(boundry); 48 output.append(CRLF); 49 50 output.append("Content-Disposition: form-data; name=\"stringpart\""); 51 output.append(CRLF); 52 output.append("Content-Type: text/plain; charset=US-ASCII"); 53 output.append(CRLF); 54 output.append("Content-Transfer-Encoding: 8bit"); 55 output.append(CRLF); 56 output.append(CRLF); 57 output.append("PART1!!"); 58 output.append(CRLF); 59 60 output.append("--"); 61 output.append(boundry); 62 output.append(CRLF); 63 64 output.append("Content-Disposition: form-data; name=\""); 65 output.append(upload.getName()); 66 output.append("\"; filename=\""); 67 output.append(upload.getName()); 68 output.append("\""); 69 70 output.append(CRLF); 71 output.append("Content-Type: application/octet-stream; charset=ISO-8859-1"); 72 output.append(CRLF); 73 output.append("Content-Transfer-Encoding: binary"); 74 output.append(CRLF); 75 output.append(CRLF); 76 output.append(filepartStr); 77 output.append(CRLF); 78 79 output.append("--"); 80 output.append(boundry); 81 output.append(CRLF); 82 83 output.append("Content-Disposition: form-data; name=\"stringpart\""); 84 output.append(CRLF); 85 output.append("Content-Type: text/plain; charset=US-ASCII"); 86 output.append(CRLF); 87 output.append("Content-Transfer-Encoding: 8bit"); 88 output.append(CRLF); 89 output.append(CRLF); 90 output.append("PART2!!"); 91 output.append(CRLF); 92 93 output.append("--"); 94 output.append(boundry); 95 output.append("--"); 96 output.append(CRLF); 97 // System.out.print(output.toString()); 98 assertEquals("Multipart content error", output.toString(), os.toString()); 99 100 // System.out.print(os.toString()); 101 } 102 } 103