1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.fileupload;
18  
19  import java.io.IOException;
20  import java.io.UnsupportedEncodingException;
21  import java.util.List;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  /**
26   * Unit tests {@link org.apache.commons.fileupload.DiskFileUpload}.
27   *
28   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
29   * @author Sean C. Sullivan
30   *
31   */
32  public class ServletFileUploadTest extends FileUploadTestCase
33  {
34  	public void testWithInvalidRequest()
35      {
36      	FileUploadBase fu = null;
37  
38      	fu = new DiskFileUpload();
39  
40      	HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
41  
42  
43      	try
44      	{
45      		fu.parseRequest(req);
46      		fail("testWithInvalidRequest: expected exception was not thrown");
47      	}
48      	catch (FileUploadException expected)
49      	{
50      		// this exception is expected
51      	}
52  
53      }
54  
55  
56  	public void testWithNullContentType()
57      {
58      	FileUploadBase fu = new DiskFileUpload();
59  
60      	HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
61  
62      	try
63      	{
64      		fu.parseRequest(req);
65      		fail("testWithNullContentType: expected exception was not thrown");
66      	}
67      	catch (DiskFileUpload.InvalidContentTypeException expected)
68      	{
69      		// this exception is expected
70      	}
71          catch (FileUploadException unexpected)
72          {
73      		fail("testWithNullContentType: unexpected exception was thrown");
74          }
75  
76      }
77  
78  
79      public void testFileUpload()
80              throws IOException, FileUploadException
81      {
82          List fileItems = parseUpload("-----1234\r\n" +
83                          "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
84                          "Content-Type: text/whatever\r\n" +
85                          "\r\n" +
86                          "This is the content of the file\n" +
87                          "\r\n" +
88                          "-----1234\r\n" +
89                          "Content-Disposition: form-data; name=\"field\"\r\n" +
90                          "\r\n" +
91                          "fieldValue\r\n" +
92                          "-----1234\r\n" +
93                          "Content-Disposition: form-data; name=\"multi\"\r\n" +
94                          "\r\n" +
95                          "value1\r\n" +
96                          "-----1234\r\n" +
97                          "Content-Disposition: form-data; name=\"multi\"\r\n" +
98                          "\r\n" +
99                          "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 }