1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 /***
126 * This is what the browser does if you submit the form without choosing a file.
127 */
128 public void testEmptyFile()
129 throws UnsupportedEncodingException, FileUploadException
130 {
131 List fileItems = parseUpload ("-----1234\r\n" +
132 "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" +
133 "\r\n" +
134 "\r\n" +
135 "-----1234--\r\n");
136 assertEquals(1, fileItems.size());
137
138 FileItem file = (FileItem) fileItems.get(0);
139 assertFalse(file.isFormField());
140 assertEquals("", file.getString());
141 assertEquals("", file.getName());
142 }
143
144 /***
145 * Internet Explorer 5 for the Mac has a bug where the carriage
146 * return is missing on any boundary line immediately preceding
147 * an input with type=image. (type=submit does not have the bug.)
148 */
149 public void testIE5MacBug()
150 throws UnsupportedEncodingException, FileUploadException
151 {
152 List fileItems = parseUpload("-----1234\r\n" +
153 "Content-Disposition: form-data; name=\"field1\"\r\n" +
154 "\r\n" +
155 "fieldValue\r\n" +
156 "-----1234\n" +
157 "Content-Disposition: form-data; name=\"submitName.x\"\r\n" +
158 "\r\n" +
159 "42\r\n" +
160 "-----1234\n" +
161 "Content-Disposition: form-data; name=\"submitName.y\"\r\n" +
162 "\r\n" +
163 "21\r\n" +
164 "-----1234\r\n" +
165 "Content-Disposition: form-data; name=\"field2\"\r\n" +
166 "\r\n" +
167 "fieldValue2\r\n" +
168 "-----1234--\r\n");
169
170 assertEquals(4, fileItems.size());
171
172 FileItem field1 = (FileItem) fileItems.get(0);
173 assertEquals("field1", field1.getFieldName());
174 assertTrue(field1.isFormField());
175 assertEquals("fieldValue", field1.getString());
176
177 FileItem submitX = (FileItem) fileItems.get(1);
178 assertEquals("submitName.x", submitX.getFieldName());
179 assertTrue(submitX.isFormField());
180 assertEquals("42", submitX.getString());
181
182 FileItem submitY = (FileItem) fileItems.get(2);
183 assertEquals("submitName.y", submitY.getFieldName());
184 assertTrue(submitY.isFormField());
185 assertEquals("21", submitY.getString());
186
187 FileItem field2 = (FileItem) fileItems.get(3);
188 assertEquals("field2", field2.getFieldName());
189 assertTrue(field2.isFormField());
190 assertEquals("fieldValue2", field2.getString());
191 }
192
193 private List parseUpload(String content)
194 throws UnsupportedEncodingException, FileUploadException
195 {
196 byte[] bytes = content.getBytes("US-ASCII");
197
198 String contentType = "multipart/form-data; boundary=---1234";
199
200 FileUploadBase upload = new DiskFileUpload();
201 HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
202
203 List fileItems = upload.parseRequest(request);
204 return fileItems;
205 }
206
207 }