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 junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import java.util.Map;
23  
24  /**
25   * Unit tests for {@link ParameterParser}.
26   *
27   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
28   */
29  public class ParameterParserTest extends TestCase
30  {
31  
32      // ------------------------------------------------------------ Constructor
33      public ParameterParserTest(String testName)
34      {
35          super(testName);
36      }
37  
38      // ------------------------------------------------------------------- Main
39      public static void main(String args[])
40      {
41          String[] testCaseName = { ParameterParserTest.class.getName()};
42          junit.textui.TestRunner.main(testCaseName);
43      }
44  
45      // ------------------------------------------------------- TestCase Methods
46  
47      public static Test suite()
48      {
49          return new TestSuite(ParameterParserTest.class);
50      }
51  
52      public void testParsing()
53      {
54          String s =
55              "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
56          ParameterParser parser = new ParameterParser();
57          Map params = parser.parse(s, ';');
58          assertEquals(null, params.get("test"));
59          assertEquals("stuff", params.get("test1"));
60          assertEquals("stuff; stuff", params.get("test2"));
61          assertEquals("\"stuff", params.get("test3"));
62  
63          params = parser.parse(s, new char[] { ',', ';' });
64          assertEquals(null, params.get("test"));
65          assertEquals("stuff", params.get("test1"));
66          assertEquals("stuff; stuff", params.get("test2"));
67          assertEquals("\"stuff", params.get("test3"));
68  
69          s = "  test  , test1=stuff   ,  , test2=, test3, ";
70          params = parser.parse(s, ',');
71          assertEquals(null, params.get("test"));
72          assertEquals("stuff", params.get("test1"));
73          assertEquals(null, params.get("test2"));
74          assertEquals(null, params.get("test3"));
75  
76          s = "  test";
77          params = parser.parse(s, ';');
78          assertEquals(null, params.get("test"));
79  
80          s = "  ";
81          params = parser.parse(s, ';');
82          assertEquals(0, params.size());
83  
84          s = " = stuff ";
85          params = parser.parse(s, ';');
86          assertEquals(0, params.size());
87      }
88  
89      public void testContentTypeParsing()
90      {
91          String s = "text/plain; Charset=UTF-8";
92          ParameterParser parser = new ParameterParser();
93          parser.setLowerCaseNames(true);
94          Map params = parser.parse(s, ';');
95          assertEquals("UTF-8", params.get("charset"));
96      }
97  
98      public void testParsingEscapedChars()
99      {
100         String s = "param = \"stuff\\\"; more stuff\"";
101         ParameterParser parser = new ParameterParser();
102         Map params = parser.parse(s, ';');
103         assertEquals(1, params.size());
104         assertEquals("stuff\\\"; more stuff", params.get("param"));
105 
106         s = "param = \"stuff\\\\\"; anotherparam";
107         params = parser.parse(s, ';');
108         assertEquals(2, params.size());
109         assertEquals("stuff\\\\", params.get("param"));
110         assertNull(params.get("anotherparam"));
111     }
112 
113     // See: http://issues.apache.org/jira/browse/FILEUPLOAD-139
114     public void testFileUpload139() 
115     {
116         ParameterParser parser = new ParameterParser();
117         String s = "Content-type: multipart/form-data , boundary=AaB03x";
118         Map params = parser.parse(s, new char[] { ',', ';' });
119         assertEquals("AaB03x", params.get("boundary"));
120 
121         s = "Content-type: multipart/form-data, boundary=AaB03x";
122         params = parser.parse(s, new char[] { ';', ',' });
123         assertEquals("AaB03x", params.get("boundary"));
124 
125         s = "Content-type: multipart/mixed, boundary=BbC04y";
126         params = parser.parse(s, new char[] { ',', ';' });
127         assertEquals("BbC04y", params.get("boundary"));
128     }
129 }