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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  
22  import java.util.Map;
23  
24  import org.junit.Test;
25  
26  /**
27   * Unit tests for {@link ParameterParser}.
28   */
29  public class ParameterParserTest {
30  
31      @Test
32      public void testParsing() {
33          String s =
34              "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
35          ParameterParser parser = new ParameterParser();
36          Map<String, String> params = parser.parse(s, ';');
37          assertEquals(null, params.get("test"));
38          assertEquals("stuff", params.get("test1"));
39          assertEquals("stuff; stuff", params.get("test2"));
40          assertEquals("\"stuff", params.get("test3"));
41  
42          params = parser.parse(s, new char[] { ',', ';' });
43          assertEquals(null, params.get("test"));
44          assertEquals("stuff", params.get("test1"));
45          assertEquals("stuff; stuff", params.get("test2"));
46          assertEquals("\"stuff", params.get("test3"));
47  
48          s = "  test  , test1=stuff   ,  , test2=, test3, ";
49          params = parser.parse(s, ',');
50          assertEquals(null, params.get("test"));
51          assertEquals("stuff", params.get("test1"));
52          assertEquals(null, params.get("test2"));
53          assertEquals(null, params.get("test3"));
54  
55          s = "  test";
56          params = parser.parse(s, ';');
57          assertEquals(null, params.get("test"));
58  
59          s = "  ";
60          params = parser.parse(s, ';');
61          assertEquals(0, params.size());
62  
63          s = " = stuff ";
64          params = parser.parse(s, ';');
65          assertEquals(0, params.size());
66      }
67  
68      @Test
69      public void testContentTypeParsing() {
70          String s = "text/plain; Charset=UTF-8";
71          ParameterParser parser = new ParameterParser();
72          parser.setLowerCaseNames(true);
73          Map<String, String> params = parser.parse(s, ';');
74          assertEquals("UTF-8", params.get("charset"));
75      }
76  
77      @Test
78      public void testParsingEscapedChars() {
79          String s = "param = \"stuff\\\"; more stuff\"";
80          ParameterParser parser = new ParameterParser();
81          Map<String, String> params = parser.parse(s, ';');
82          assertEquals(1, params.size());
83          assertEquals("stuff\\\"; more stuff", params.get("param"));
84  
85          s = "param = \"stuff\\\\\"; anotherparam";
86          params = parser.parse(s, ';');
87          assertEquals(2, params.size());
88          assertEquals("stuff\\\\", params.get("param"));
89          assertNull(params.get("anotherparam"));
90      }
91  
92      // See: http://issues.apache.org/jira/browse/FILEUPLOAD-139
93      @Test
94      public void testFileUpload139() {
95          ParameterParser parser = new ParameterParser();
96          String s = "Content-type: multipart/form-data , boundary=AaB03x";
97          Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
98          assertEquals("AaB03x", params.get("boundary"));
99  
100         s = "Content-type: multipart/form-data, boundary=AaB03x";
101         params = parser.parse(s, new char[] { ';', ',' });
102         assertEquals("AaB03x", params.get("boundary"));
103 
104         s = "Content-type: multipart/mixed, boundary=BbC04y";
105         params = parser.parse(s, new char[] { ',', ';' });
106         assertEquals("BbC04y", params.get("boundary"));
107     }
108 
109     /**
110      * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a>
111      */
112     @Test
113     public void fileUpload199() {
114         ParameterParser parser = new ParameterParser();
115         String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n";
116         Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
117         assertEquals("If you can read this you understand the example.", params.get("filename"));
118     }
119 
120 }