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.Before;
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      private ServletFileUpload upload;
43  
44      @Before
45      public void setUp() {
46          upload = new ServletFileUpload(new DiskFileItemFactory());
47      }
48  
49      /**
50       * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">
51       */
52      @Test
53      public void parseParameterMap()
54              throws Exception {
55          String text = "-----1234\r\n" +
56                        "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
57                        "Content-Type: text/whatever\r\n" +
58                        "\r\n" +
59                        "This is the content of the file\n" +
60                        "\r\n" +
61                        "-----1234\r\n" +
62                        "Content-Disposition: form-data; name=\"field\"\r\n" +
63                        "\r\n" +
64                        "fieldValue\r\n" +
65                        "-----1234\r\n" +
66                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
67                        "\r\n" +
68                        "value1\r\n" +
69                        "-----1234\r\n" +
70                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
71                        "\r\n" +
72                        "value2\r\n" +
73                        "-----1234--\r\n";
74          byte[] bytes = text.getBytes("US-ASCII");
75          HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
76  
77          Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request);
78          assertTrue(mappedParameters.containsKey("file"));
79          assertEquals(1, mappedParameters.get("file").size());
80  
81          assertTrue(mappedParameters.containsKey("field"));
82          assertEquals(1, mappedParameters.get("field").size());
83  
84          assertTrue(mappedParameters.containsKey("multi"));
85          assertEquals(2, mappedParameters.get("multi").size());
86      }
87  
88  }