001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.fileupload; 018 019 import java.io.IOException; 020 import java.io.UnsupportedEncodingException; 021 import java.util.List; 022 023 import javax.servlet.http.HttpServletRequest; 024 025 /** 026 * Unit tests {@link org.apache.commons.fileupload.DiskFileUpload}. 027 * 028 * @author <a href="mailto:jmcnally@apache.org">John McNally</a> 029 * @author Sean C. Sullivan 030 * 031 */ 032 public class ServletFileUploadTest extends FileUploadTestCase 033 { 034 public void testWithInvalidRequest() 035 { 036 FileUploadBase fu = null; 037 038 fu = new DiskFileUpload(); 039 040 HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest(); 041 042 043 try 044 { 045 fu.parseRequest(req); 046 fail("testWithInvalidRequest: expected exception was not thrown"); 047 } 048 catch (FileUploadException expected) 049 { 050 // this exception is expected 051 } 052 053 } 054 055 056 public void testWithNullContentType() 057 { 058 FileUploadBase fu = new DiskFileUpload(); 059 060 HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType(); 061 062 try 063 { 064 fu.parseRequest(req); 065 fail("testWithNullContentType: expected exception was not thrown"); 066 } 067 catch (DiskFileUpload.InvalidContentTypeException expected) 068 { 069 // this exception is expected 070 } 071 catch (FileUploadException unexpected) 072 { 073 fail("testWithNullContentType: unexpected exception was thrown"); 074 } 075 076 } 077 078 079 public void testFileUpload() 080 throws IOException, FileUploadException 081 { 082 List fileItems = parseUpload("-----1234\r\n" + 083 "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + 084 "Content-Type: text/whatever\r\n" + 085 "\r\n" + 086 "This is the content of the file\n" + 087 "\r\n" + 088 "-----1234\r\n" + 089 "Content-Disposition: form-data; name=\"field\"\r\n" + 090 "\r\n" + 091 "fieldValue\r\n" + 092 "-----1234\r\n" + 093 "Content-Disposition: form-data; name=\"multi\"\r\n" + 094 "\r\n" + 095 "value1\r\n" + 096 "-----1234\r\n" + 097 "Content-Disposition: form-data; name=\"multi\"\r\n" + 098 "\r\n" + 099 "value2\r\n" + 100 "-----1234--\r\n"); 101 assertEquals(4, fileItems.size()); 102 103 FileItem file = (FileItem) fileItems.get(0); 104 assertEquals("file", file.getFieldName()); 105 assertFalse(file.isFormField()); 106 assertEquals("This is the content of the file\n", file.getString()); 107 assertEquals("text/whatever", file.getContentType()); 108 assertEquals("foo.tab", file.getName()); 109 110 FileItem field = (FileItem) fileItems.get(1); 111 assertEquals("field", field.getFieldName()); 112 assertTrue(field.isFormField()); 113 assertEquals("fieldValue", field.getString()); 114 115 FileItem multi0 = (FileItem) fileItems.get(2); 116 assertEquals("multi", multi0.getFieldName()); 117 assertTrue(multi0.isFormField()); 118 assertEquals("value1", multi0.getString()); 119 120 FileItem multi1 = (FileItem) fileItems.get(3); 121 assertEquals("multi", multi1.getFieldName()); 122 assertTrue(multi1.isFormField()); 123 assertEquals("value2", multi1.getString()); 124 } 125 126 public void testFilenameCaseSensitivity() 127 throws IOException, FileUploadException 128 { 129 List fileItems = parseUpload("-----1234\r\n" + 130 "Content-Disposition: form-data; name=\"FiLe\"; filename=\"FOO.tab\"\r\n" + 131 "Content-Type: text/whatever\r\n" + 132 "\r\n" + 133 "This is the content of the file\n" + 134 "\r\n" + 135 "-----1234--\r\n"); 136 assertEquals(1, fileItems.size()); 137 138 FileItem file = (FileItem) fileItems.get(0); 139 assertEquals("FiLe", file.getFieldName()); 140 assertEquals("FOO.tab", file.getName()); 141 } 142 143 /** 144 * This is what the browser does if you submit the form without choosing a file. 145 */ 146 public void testEmptyFile() 147 throws UnsupportedEncodingException, FileUploadException 148 { 149 List fileItems = parseUpload ("-----1234\r\n" + 150 "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" + 151 "\r\n" + 152 "\r\n" + 153 "-----1234--\r\n"); 154 assertEquals(1, fileItems.size()); 155 156 FileItem file = (FileItem) fileItems.get(0); 157 assertFalse(file.isFormField()); 158 assertEquals("", file.getString()); 159 assertEquals("", file.getName()); 160 } 161 162 /** 163 * Internet Explorer 5 for the Mac has a bug where the carriage 164 * return is missing on any boundary line immediately preceding 165 * an input with type=image. (type=submit does not have the bug.) 166 */ 167 public void testIE5MacBug() 168 throws UnsupportedEncodingException, FileUploadException 169 { 170 List fileItems = parseUpload("-----1234\r\n" + 171 "Content-Disposition: form-data; name=\"field1\"\r\n" + 172 "\r\n" + 173 "fieldValue\r\n" + 174 "-----1234\n" + // NOTE \r missing 175 "Content-Disposition: form-data; name=\"submitName.x\"\r\n" + 176 "\r\n" + 177 "42\r\n" + 178 "-----1234\n" + // NOTE \r missing 179 "Content-Disposition: form-data; name=\"submitName.y\"\r\n" + 180 "\r\n" + 181 "21\r\n" + 182 "-----1234\r\n" + 183 "Content-Disposition: form-data; name=\"field2\"\r\n" + 184 "\r\n" + 185 "fieldValue2\r\n" + 186 "-----1234--\r\n"); 187 188 assertEquals(4, fileItems.size()); 189 190 FileItem field1 = (FileItem) fileItems.get(0); 191 assertEquals("field1", field1.getFieldName()); 192 assertTrue(field1.isFormField()); 193 assertEquals("fieldValue", field1.getString()); 194 195 FileItem submitX = (FileItem) fileItems.get(1); 196 assertEquals("submitName.x", submitX.getFieldName()); 197 assertTrue(submitX.isFormField()); 198 assertEquals("42", submitX.getString()); 199 200 FileItem submitY = (FileItem) fileItems.get(2); 201 assertEquals("submitName.y", submitY.getFieldName()); 202 assertTrue(submitY.isFormField()); 203 assertEquals("21", submitY.getString()); 204 205 FileItem field2 = (FileItem) fileItems.get(3); 206 assertEquals("field2", field2.getFieldName()); 207 assertTrue(field2.isFormField()); 208 assertEquals("fieldValue2", field2.getString()); 209 } 210 211 /** 212 * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62</a> 213 */ 214 public void testFILEUPLOAD62() throws Exception { 215 final String contentType = "multipart/form-data; boundary=AaB03x"; 216 final String request = 217 "--AaB03x\r\n" + 218 "content-disposition: form-data; name=\"field1\"\r\n" + 219 "\r\n" + 220 "Joe Blow\r\n" + 221 "--AaB03x\r\n" + 222 "content-disposition: form-data; name=\"pics\"\r\n" + 223 "Content-type: multipart/mixed; boundary=BbC04y\r\n" + 224 "\r\n" + 225 "--BbC04y\r\n" + 226 "Content-disposition: attachment; filename=\"file1.txt\"\r\n" + 227 "Content-Type: text/plain\r\n" + 228 "\r\n" + 229 "... contents of file1.txt ...\r\n" + 230 "--BbC04y\r\n" + 231 "Content-disposition: attachment; filename=\"file2.gif\"\r\n" + 232 "Content-type: image/gif\r\n" + 233 "Content-Transfer-Encoding: binary\r\n" + 234 "\r\n" + 235 "...contents of file2.gif...\r\n" + 236 "--BbC04y--\r\n" + 237 "--AaB03x--"; 238 List fileItems = parseUpload(request.getBytes("US-ASCII"), contentType); 239 assertEquals(3, fileItems.size()); 240 FileItem item0 = (FileItem) fileItems.get(0); 241 assertEquals("field1", item0.getFieldName()); 242 assertNull(item0.getName()); 243 assertEquals("Joe Blow", new String(item0.get())); 244 FileItem item1 = (FileItem) fileItems.get(1); 245 assertEquals("pics", item1.getFieldName()); 246 assertEquals("file1.txt", item1.getName()); 247 assertEquals("... contents of file1.txt ...", new String(item1.get())); 248 FileItem item2 = (FileItem) fileItems.get(2); 249 assertEquals("pics", item2.getFieldName()); 250 assertEquals("file2.gif", item2.getName()); 251 assertEquals("...contents of file2.gif...", new String(item2.get())); 252 } 253 254 /** 255 * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD-111</a> 256 */ 257 public void testFoldedHeaders() 258 throws IOException, FileUploadException { 259 List fileItems = parseUpload("-----1234\r\n" + 260 "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + 261 "Content-Type: text/whatever\r\n" + 262 "\r\n" + 263 "This is the content of the file\n" + 264 "\r\n" + 265 "-----1234\r\n" + 266 "Content-Disposition: form-data; \r\n" + 267 "\tname=\"field\"\r\n" + 268 "\r\n" + 269 "fieldValue\r\n" + 270 "-----1234\r\n" + 271 "Content-Disposition: form-data;\r\n" + 272 " name=\"multi\"\r\n" + 273 "\r\n" + 274 "value1\r\n" + 275 "-----1234\r\n" + 276 "Content-Disposition: form-data; name=\"multi\"\r\n" + 277 "\r\n" + 278 "value2\r\n" + 279 "-----1234--\r\n"); 280 assertEquals(4, fileItems.size()); 281 282 FileItem file = (FileItem) fileItems.get(0); 283 assertEquals("file", file.getFieldName()); 284 assertFalse(file.isFormField()); 285 assertEquals("This is the content of the file\n", file.getString()); 286 assertEquals("text/whatever", file.getContentType()); 287 assertEquals("foo.tab", file.getName()); 288 289 FileItem field = (FileItem) fileItems.get(1); 290 assertEquals("field", field.getFieldName()); 291 assertTrue(field.isFormField()); 292 assertEquals("fieldValue", field.getString()); 293 294 FileItem multi0 = (FileItem) fileItems.get(2); 295 assertEquals("multi", multi0.getFieldName()); 296 assertTrue(multi0.isFormField()); 297 assertEquals("value1", multi0.getString()); 298 299 FileItem multi1 = (FileItem) fileItems.get(3); 300 assertEquals("multi", multi1.getFieldName()); 301 assertTrue(multi1.isFormField()); 302 assertEquals("value2", multi1.getString()); 303 } 304 }