1   /*
2    * Copyright 2001-2004 The Apache Software Foundation
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  package org.apache.commons.fileupload;
17  
18  import java.io.IOException;
19  import java.io.UnsupportedEncodingException;
20  import java.util.List;
21  import javax.servlet.http.HttpServletRequest;
22  import junit.framework.TestCase;
23  
24  /***
25   * Unit tests {@link org.apache.commons.fileupload.DiskFileUpload}.
26   *
27   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
28   * @author Sean C. Sullivan
29   *
30   */
31  public class ServletFileUploadTest extends TestCase
32  {
33  	public void testWithInvalidRequest()
34      {
35      	FileUploadBase fu = null;
36  
37      	fu = new DiskFileUpload();
38  
39      	HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
40  
41  
42      	try
43      	{
44      		fu.parseRequest(req);
45      		fail("testWithInvalidRequest: expected exception was not thrown");
46      	}
47      	catch (FileUploadException expected)
48      	{
49      		// this exception is expected
50      	}
51  
52      }
53  
54  
55  	public void testWithNullContentType()
56      {
57      	FileUploadBase fu = new DiskFileUpload();
58  
59      	HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
60  
61      	try
62      	{
63      		fu.parseRequest(req);
64      		fail("testWithNullContentType: expected exception was not thrown");
65      	}
66      	catch (DiskFileUpload.InvalidContentTypeException expected)
67      	{
68      		// this exception is expected
69      	}
70          catch (FileUploadException unexpected)
71          {
72      		fail("testWithNullContentType: unexpected exception was thrown");
73          }
74  
75      }
76  
77  
78      public void testFileUpload()
79              throws IOException, FileUploadException
80      {
81          List fileItems = parseUpload("-----1234\r\n" +
82                          "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
83                          "Content-Type: text/whatever\r\n" +
84                          "\r\n" +
85                          "This is the content of the file\n" +
86                          "\r\n" +
87                          "-----1234\r\n" +
88                          "Content-Disposition: form-data; name=\"field\"\r\n" +
89                          "\r\n" +
90                          "fieldValue\r\n" +
91                          "-----1234\r\n" +
92                          "Content-Disposition: form-data; name=\"multi\"\r\n" +
93                          "\r\n" +
94                          "value1\r\n" +
95                          "-----1234\r\n" +
96                          "Content-Disposition: form-data; name=\"multi\"\r\n" +
97                          "\r\n" +
98                          "value2\r\n" +
99                          "-----1234--\r\n");
100         assertEquals(4, fileItems.size());
101 
102         FileItem file = (FileItem) fileItems.get(0);
103         assertEquals("file", file.getFieldName());
104         assertFalse(file.isFormField());
105         assertEquals("This is the content of the file\n", file.getString());
106         assertEquals("text/whatever", file.getContentType());
107         assertEquals("foo.tab", file.getName());
108 
109         FileItem field = (FileItem) fileItems.get(1);
110         assertEquals("field", field.getFieldName());
111         assertTrue(field.isFormField());
112         assertEquals("fieldValue", field.getString());
113 
114         FileItem multi0 = (FileItem) fileItems.get(2);
115         assertEquals("multi", multi0.getFieldName());
116         assertTrue(multi0.isFormField());
117         assertEquals("value1", multi0.getString());
118 
119         FileItem multi1 = (FileItem) fileItems.get(3);
120         assertEquals("multi", multi1.getFieldName());
121         assertTrue(multi1.isFormField());
122         assertEquals("value2", multi1.getString());
123     }
124 
125     public void testFilenameCaseSensitivity() 
126             throws IOException, FileUploadException
127     {
128         List fileItems = parseUpload("-----1234\r\n" +
129                         "Content-Disposition: form-data; name=\"FiLe\"; filename=\"FOO.tab\"\r\n" +
130                         "Content-Type: text/whatever\r\n" +
131                         "\r\n" +
132                         "This is the content of the file\n" +
133                         "\r\n" +
134                         "-----1234--\r\n");
135         assertEquals(1, fileItems.size());
136 
137         FileItem file = (FileItem) fileItems.get(0);
138         assertEquals("FiLe", file.getFieldName());
139         assertEquals("FOO.tab", file.getName());
140     }
141 
142     /***
143      * This is what the browser does if you submit the form without choosing a file.
144      */
145     public void testEmptyFile()
146             throws UnsupportedEncodingException, FileUploadException
147     {
148         List fileItems = parseUpload ("-----1234\r\n" +
149                 "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" +
150                 "\r\n" +
151                 "\r\n" +
152                 "-----1234--\r\n");
153         assertEquals(1, fileItems.size());
154 
155         FileItem file = (FileItem) fileItems.get(0);
156         assertFalse(file.isFormField());
157         assertEquals("", file.getString());
158         assertEquals("", file.getName());
159     }
160 
161     /***
162      * Internet Explorer 5 for the Mac has a bug where the carriage
163      * return is missing on any boundary line immediately preceding
164      * an input with type=image. (type=submit does not have the bug.)
165      */
166     public void testIE5MacBug()
167             throws UnsupportedEncodingException, FileUploadException
168     {
169         List fileItems = parseUpload("-----1234\r\n" +
170                 "Content-Disposition: form-data; name=\"field1\"\r\n" +
171                 "\r\n" +
172                 "fieldValue\r\n" +
173                 "-----1234\n" + // NOTE \r missing
174                 "Content-Disposition: form-data; name=\"submitName.x\"\r\n" +
175                 "\r\n" +
176                 "42\r\n" +
177                 "-----1234\n" + // NOTE \r missing
178                 "Content-Disposition: form-data; name=\"submitName.y\"\r\n" +
179                 "\r\n" +
180                 "21\r\n" +
181                 "-----1234\r\n" +
182                 "Content-Disposition: form-data; name=\"field2\"\r\n" +
183                 "\r\n" +
184                 "fieldValue2\r\n" +
185                 "-----1234--\r\n");
186 
187         assertEquals(4, fileItems.size());
188 
189         FileItem field1 = (FileItem) fileItems.get(0);
190         assertEquals("field1", field1.getFieldName());
191         assertTrue(field1.isFormField());
192         assertEquals("fieldValue", field1.getString());
193 
194         FileItem submitX = (FileItem) fileItems.get(1);
195         assertEquals("submitName.x", submitX.getFieldName());
196         assertTrue(submitX.isFormField());
197         assertEquals("42", submitX.getString());
198 
199         FileItem submitY = (FileItem) fileItems.get(2);
200         assertEquals("submitName.y", submitY.getFieldName());
201         assertTrue(submitY.isFormField());
202         assertEquals("21", submitY.getString());
203 
204         FileItem field2 = (FileItem) fileItems.get(3);
205         assertEquals("field2", field2.getFieldName());
206         assertTrue(field2.isFormField());
207         assertEquals("fieldValue2", field2.getString());
208     }
209 
210     private List parseUpload(String content)
211             throws UnsupportedEncodingException, FileUploadException
212     {
213         byte[] bytes = content.getBytes("US-ASCII");
214 
215         String contentType = "multipart/form-data; boundary=---1234";
216 
217         FileUploadBase upload = new DiskFileUpload();
218         HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
219 
220         List fileItems = upload.parseRequest(request);
221         return fileItems;
222     }
223 
224 }