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