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: public void testWithInvalidRequest() {
034: FileUploadBase fu = null;
035:
036: fu = new DiskFileUpload();
037:
038: HttpServletRequest req = HttpServletRequestFactory
039: .createInvalidHttpServletRequest();
040:
041: try {
042: fu.parseRequest(req);
043: fail("testWithInvalidRequest: expected exception was not thrown");
044: } catch (FileUploadException expected) {
045: // this exception is expected
046: }
047:
048: }
049:
050: public void testWithNullContentType() {
051: FileUploadBase fu = new DiskFileUpload();
052:
053: HttpServletRequest req = HttpServletRequestFactory
054: .createHttpServletRequestWithNullContentType();
055:
056: try {
057: fu.parseRequest(req);
058: fail("testWithNullContentType: expected exception was not thrown");
059: } catch (DiskFileUpload.InvalidContentTypeException expected) {
060: // this exception is expected
061: } catch (FileUploadException unexpected) {
062: fail("testWithNullContentType: unexpected exception was thrown");
063: }
064:
065: }
066:
067: public void testFileUpload() throws IOException,
068: FileUploadException {
069: List fileItems = parseUpload("-----1234\r\n"
070: + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n"
071: + "Content-Type: text/whatever\r\n" + "\r\n"
072: + "This is the content of the file\n" + "\r\n"
073: + "-----1234\r\n"
074: + "Content-Disposition: form-data; name=\"field\"\r\n"
075: + "\r\n" + "fieldValue\r\n" + "-----1234\r\n"
076: + "Content-Disposition: form-data; name=\"multi\"\r\n"
077: + "\r\n" + "value1\r\n" + "-----1234\r\n"
078: + "Content-Disposition: form-data; name=\"multi\"\r\n"
079: + "\r\n" + "value2\r\n" + "-----1234--\r\n");
080: assertEquals(4, fileItems.size());
081:
082: FileItem file = (FileItem) fileItems.get(0);
083: assertEquals("file", file.getFieldName());
084: assertFalse(file.isFormField());
085: assertEquals("This is the content of the file\n", file
086: .getString());
087: assertEquals("text/whatever", file.getContentType());
088: assertEquals("foo.tab", file.getName());
089:
090: FileItem field = (FileItem) fileItems.get(1);
091: assertEquals("field", field.getFieldName());
092: assertTrue(field.isFormField());
093: assertEquals("fieldValue", field.getString());
094:
095: FileItem multi0 = (FileItem) fileItems.get(2);
096: assertEquals("multi", multi0.getFieldName());
097: assertTrue(multi0.isFormField());
098: assertEquals("value1", multi0.getString());
099:
100: FileItem multi1 = (FileItem) fileItems.get(3);
101: assertEquals("multi", multi1.getFieldName());
102: assertTrue(multi1.isFormField());
103: assertEquals("value2", multi1.getString());
104: }
105:
106: public void testFilenameCaseSensitivity() throws IOException,
107: FileUploadException {
108: List fileItems = parseUpload("-----1234\r\n"
109: + "Content-Disposition: form-data; name=\"FiLe\"; filename=\"FOO.tab\"\r\n"
110: + "Content-Type: text/whatever\r\n" + "\r\n"
111: + "This is the content of the file\n" + "\r\n"
112: + "-----1234--\r\n");
113: assertEquals(1, fileItems.size());
114:
115: FileItem file = (FileItem) fileItems.get(0);
116: assertEquals("FiLe", file.getFieldName());
117: assertEquals("FOO.tab", file.getName());
118: }
119:
120: /**
121: * This is what the browser does if you submit the form without choosing a file.
122: */
123: public void testEmptyFile() throws UnsupportedEncodingException,
124: FileUploadException {
125: List fileItems = parseUpload("-----1234\r\n"
126: + "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n"
127: + "\r\n" + "\r\n" + "-----1234--\r\n");
128: assertEquals(1, fileItems.size());
129:
130: FileItem file = (FileItem) fileItems.get(0);
131: assertFalse(file.isFormField());
132: assertEquals("", file.getString());
133: assertEquals("", file.getName());
134: }
135:
136: /**
137: * Internet Explorer 5 for the Mac has a bug where the carriage
138: * return is missing on any boundary line immediately preceding
139: * an input with type=image. (type=submit does not have the bug.)
140: */
141: public void testIE5MacBug() throws UnsupportedEncodingException,
142: FileUploadException {
143: List fileItems = parseUpload("-----1234\r\n"
144: + "Content-Disposition: form-data; name=\"field1\"\r\n"
145: + "\r\n"
146: + "fieldValue\r\n"
147: + "-----1234\n"
148: + // NOTE \r missing
149: "Content-Disposition: form-data; name=\"submitName.x\"\r\n"
150: + "\r\n"
151: + "42\r\n"
152: + "-----1234\n"
153: + // NOTE \r missing
154: "Content-Disposition: form-data; name=\"submitName.y\"\r\n"
155: + "\r\n" + "21\r\n" + "-----1234\r\n"
156: + "Content-Disposition: form-data; name=\"field2\"\r\n"
157: + "\r\n" + "fieldValue2\r\n" + "-----1234--\r\n");
158:
159: assertEquals(4, fileItems.size());
160:
161: FileItem field1 = (FileItem) fileItems.get(0);
162: assertEquals("field1", field1.getFieldName());
163: assertTrue(field1.isFormField());
164: assertEquals("fieldValue", field1.getString());
165:
166: FileItem submitX = (FileItem) fileItems.get(1);
167: assertEquals("submitName.x", submitX.getFieldName());
168: assertTrue(submitX.isFormField());
169: assertEquals("42", submitX.getString());
170:
171: FileItem submitY = (FileItem) fileItems.get(2);
172: assertEquals("submitName.y", submitY.getFieldName());
173: assertTrue(submitY.isFormField());
174: assertEquals("21", submitY.getString());
175:
176: FileItem field2 = (FileItem) fileItems.get(3);
177: assertEquals("field2", field2.getFieldName());
178: assertTrue(field2.isFormField());
179: assertEquals("fieldValue2", field2.getString());
180: }
181:
182: /**
183: * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD</a>
184: */
185: public void testFILEUPLOAD62() throws Exception {
186: final String contentType = "multipart/form-data; boundary=AaB03x";
187: final String request = "--AaB03x\r\n"
188: + "content-disposition: form-data; name=\"field1\"\r\n"
189: + "\r\n"
190: + "Joe Blow\r\n"
191: + "--AaB03x\r\n"
192: + "content-disposition: form-data; name=\"pics\"\r\n"
193: + "Content-type: multipart/mixed; boundary=BbC04y\r\n"
194: + "\r\n"
195: + "--BbC04y\r\n"
196: + "Content-disposition: attachment; filename=\"file1.txt\"\r\n"
197: + "Content-Type: text/plain\r\n"
198: + "\r\n"
199: + "... contents of file1.txt ...\r\n"
200: + "--BbC04y\r\n"
201: + "Content-disposition: attachment; filename=\"file2.gif\"\r\n"
202: + "Content-type: image/gif\r\n"
203: + "Content-Transfer-Encoding: binary\r\n" + "\r\n"
204: + "...contents of file2.gif...\r\n" + "--BbC04y--\r\n"
205: + "--AaB03x--";
206: List fileItems = parseUpload(request.getBytes("US-ASCII"),
207: contentType);
208: assertEquals(3, fileItems.size());
209: FileItem item0 = (FileItem) fileItems.get(0);
210: assertEquals("field1", item0.getFieldName());
211: assertNull(item0.getName());
212: assertEquals("Joe Blow", new String(item0.get()));
213: FileItem item1 = (FileItem) fileItems.get(1);
214: assertEquals("pics", item1.getFieldName());
215: assertEquals("file1.txt", item1.getName());
216: assertEquals("... contents of file1.txt ...", new String(item1
217: .get()));
218: FileItem item2 = (FileItem) fileItems.get(2);
219: assertEquals("pics", item2.getFieldName());
220: assertEquals("file2.gif", item2.getName());
221: assertEquals("...contents of file2.gif...", new String(item2
222: .get()));
223: }
224:
225: public void testFoldedHeaders() throws IOException,
226: FileUploadException {
227: List fileItems = parseUpload("-----1234\r\n"
228: + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n"
229: + "Content-Type: text/whatever\r\n" + "\r\n"
230: + "This is the content of the file\n" + "\r\n"
231: + "-----1234\r\n"
232: + "Content-Disposition: form-data; \r\n"
233: + "\tname=\"field\"\r\n" + "\r\n" + "fieldValue\r\n"
234: + "-----1234\r\n"
235: + "Content-Disposition: form-data;\r\n"
236: + " name=\"multi\"\r\n" + "\r\n" + "value1\r\n"
237: + "-----1234\r\n"
238: + "Content-Disposition: form-data; name=\"multi\"\r\n"
239: + "\r\n" + "value2\r\n" + "-----1234--\r\n");
240: assertEquals(4, fileItems.size());
241:
242: FileItem file = (FileItem) fileItems.get(0);
243: assertEquals("file", file.getFieldName());
244: assertFalse(file.isFormField());
245: assertEquals("This is the content of the file\n", file
246: .getString());
247: assertEquals("text/whatever", file.getContentType());
248: assertEquals("foo.tab", file.getName());
249:
250: FileItem field = (FileItem) fileItems.get(1);
251: assertEquals("field", field.getFieldName());
252: assertTrue(field.isFormField());
253: assertEquals("fieldValue", field.getString());
254:
255: FileItem multi0 = (FileItem) fileItems.get(2);
256: assertEquals("multi", multi0.getFieldName());
257: assertTrue(multi0.isFormField());
258: assertEquals("value1", multi0.getString());
259:
260: FileItem multi1 = (FileItem) fileItems.get(3);
261: assertEquals("multi", multi1.getFieldName());
262: assertTrue(multi1.isFormField());
263: assertEquals("value2", multi1.getString());
264: }
265: }
|