View Javadoc
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.servlet;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.commons.fileupload.Constants;
28  import org.apache.commons.fileupload.FileItem;
29  import org.apache.commons.fileupload.FileUploadTest;
30  import org.apache.commons.fileupload.MockHttpServletRequest;
31  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
32  import org.junit.Test;
33  
34  /**
35   * Test for {@link ServletFileUpload}.
36   *
37   * @see FileUploadTest
38   * @since 1.4
39   */
40  public class ServletFileUploadTest {
41  
42      @Test
43      public void parseImpliedUtf8() throws Exception {
44          // utf8 encoded form-data without explicit content-type encoding
45          final String text = "-----1234\r\n" +
46                  "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
47                  "\r\n" +
48                  "Th�s �s the co�te�t of the f�le\n" +
49                  "\r\n" +
50                  "-----1234--\r\n";
51  
52          final byte[] bytes = text.getBytes("UTF-8");
53          final HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
54  
55          final DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
56          fileItemFactory.setDefaultCharset("UTF-8");
57          final ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
58          final List<FileItem> fileItems = upload.parseRequest(request);
59          final FileItem fileItem = fileItems.get(0);
60          assertTrue(fileItem.getString(), fileItem.getString().contains("co�te�t"));
61      }
62  
63  
64      /**
65       * Tests <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">FILEUPLOAD-210</a>.
66       */
67      @Test
68      public void parseParameterMap()
69              throws Exception {
70          final String text = "-----1234\r\n" +
71                        "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
72                        "Content-Type: text/whatever\r\n" +
73                        "\r\n" +
74                        "This is the content of the file\n" +
75                        "\r\n" +
76                        "-----1234\r\n" +
77                        "Content-Disposition: form-data; name=\"field\"\r\n" +
78                        "\r\n" +
79                        "fieldValue\r\n" +
80                        "-----1234\r\n" +
81                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
82                        "\r\n" +
83                        "value1\r\n" +
84                        "-----1234\r\n" +
85                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
86                        "\r\n" +
87                        "value2\r\n" +
88                        "-----1234--\r\n";
89          final byte[] bytes = text.getBytes("US-ASCII");
90          final HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
91  
92          final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
93          final Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request);
94          assertTrue(mappedParameters.containsKey("file"));
95          assertEquals(1, mappedParameters.get("file").size());
96  
97          assertTrue(mappedParameters.containsKey("field"));
98          assertEquals(1, mappedParameters.get("field").size());
99  
100         assertTrue(mappedParameters.containsKey("multi"));
101         assertEquals(2, mappedParameters.get("multi").size());
102     }
103 }