1 32 33 package org.apache.commons.httpclient; 34 35 import java.io.ByteArrayInputStream ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 39 import junit.framework.Test; 40 import junit.framework.TestSuite; 41 42 import org.apache.commons.httpclient.methods.PostMethod; 43 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; 44 import org.apache.commons.httpclient.methods.multipart.FilePart; 45 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 46 import org.apache.commons.httpclient.methods.multipart.Part; 47 import org.apache.commons.httpclient.methods.multipart.PartSource; 48 import org.apache.commons.httpclient.methods.multipart.StringPart; 49 50 55 public class TestMultipartPost extends HttpClientTestBase { 56 57 public TestMultipartPost(final String testName) throws IOException { 58 super(testName); 59 } 60 61 public static Test suite() { 62 TestSuite suite = new TestSuite(TestMultipartPost.class); 63 ProxyTestDecorator.addTests(suite); 64 return suite; 65 } 66 67 public static void main(String args[]) { 68 String [] testCaseName = { TestMultipartPost.class.getName() }; 69 junit.textui.TestRunner.main(testCaseName); 70 } 71 72 74 77 public void testPostStringPart() throws Exception { 78 79 this.server.setHttpService(new EchoService()); 80 81 PostMethod method = new PostMethod(); 82 MultipartRequestEntity entity = new MultipartRequestEntity( 83 new Part[] { new StringPart("param", "Hello", "ISO-8859-1") }, 84 method.getParams()); 85 method.setRequestEntity(entity); 86 client.executeMethod(method); 87 88 assertEquals(200,method.getStatusCode()); 89 String body = method.getResponseBodyAsString(); 90 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0); 91 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0); 92 assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0); 93 assertTrue(body.indexOf("Hello") >= 0); 94 } 95 96 97 100 public void testPostFilePart() throws Exception { 101 102 this.server.setHttpService(new EchoService()); 103 104 PostMethod method = new PostMethod(); 105 byte[] content = "Hello".getBytes(); 106 MultipartRequestEntity entity = new MultipartRequestEntity( 107 new Part[] { 108 new FilePart( 109 "param1", 110 new ByteArrayPartSource("filename.txt", content), 111 "text/plain", 112 "ISO-8859-1") }, 113 method.getParams()); 114 method.setRequestEntity(entity); 115 116 client.executeMethod(method); 117 118 assertEquals(200,method.getStatusCode()); 119 String body = method.getResponseBodyAsString(); 120 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0); 121 assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0); 122 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0); 123 assertTrue(body.indexOf("Hello") >= 0); 124 } 125 126 129 130 public class TestPartSource implements PartSource { 131 private String fileName; 132 private byte[] data; 133 134 public TestPartSource(String fileName, byte[] data) { 135 this.fileName = fileName; 136 this.data = data; 137 } 138 139 public long getLength() { 140 return -1; 141 } 142 143 public String getFileName() { 144 return fileName; 145 } 146 147 public InputStream createInputStream() throws IOException { 148 return new ByteArrayInputStream (data); 149 } 150 151 } 152 153 public void testPostFilePartUnknownLength() throws Exception { 154 155 this.server.setHttpService(new EchoService()); 156 157 String enc = "ISO-8859-1"; 158 PostMethod method = new PostMethod(); 159 byte[] content = "Hello".getBytes(enc); 160 MultipartRequestEntity entity = new MultipartRequestEntity( 161 new Part[] { 162 new FilePart( 163 "param1", 164 new TestPartSource("filename.txt", content), 165 "text/plain", 166 enc) }, 167 method.getParams()); 168 method.setRequestEntity(entity); 169 170 client.executeMethod(method); 171 172 assertEquals(200,method.getStatusCode()); 173 String body = method.getResponseBodyAsString(); 174 assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0); 175 assertTrue(body.indexOf("Content-Type: text/plain; charset="+enc) >= 0); 176 assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0); 177 assertTrue(body.indexOf("Hello") >= 0); 178 } 179 180 } 181 | Popular Tags |