001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.fileupload;
018    
019    import junit.framework.Test;
020    import junit.framework.TestCase;
021    import junit.framework.TestSuite;
022    import java.util.Map;
023    
024    /**
025     * Unit tests for {@link ParameterParser}.
026     *
027     * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
028     */
029    public class ParameterParserTest extends TestCase
030    {
031    
032        // ------------------------------------------------------------ Constructor
033        public ParameterParserTest(String testName)
034        {
035            super(testName);
036        }
037    
038        // ------------------------------------------------------------------- Main
039        public static void main(String args[])
040        {
041            String[] testCaseName = { ParameterParserTest.class.getName()};
042            junit.textui.TestRunner.main(testCaseName);
043        }
044    
045        // ------------------------------------------------------- TestCase Methods
046    
047        public static Test suite()
048        {
049            return new TestSuite(ParameterParserTest.class);
050        }
051    
052        public void testParsing()
053        {
054            String s =
055                "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
056            ParameterParser parser = new ParameterParser();
057            Map params = parser.parse(s, ';');
058            assertEquals(null, params.get("test"));
059            assertEquals("stuff", params.get("test1"));
060            assertEquals("stuff; stuff", params.get("test2"));
061            assertEquals("\"stuff", params.get("test3"));
062    
063            params = parser.parse(s, new char[] { ',', ';' });
064            assertEquals(null, params.get("test"));
065            assertEquals("stuff", params.get("test1"));
066            assertEquals("stuff; stuff", params.get("test2"));
067            assertEquals("\"stuff", params.get("test3"));
068    
069            s = "  test  , test1=stuff   ,  , test2=, test3, ";
070            params = parser.parse(s, ',');
071            assertEquals(null, params.get("test"));
072            assertEquals("stuff", params.get("test1"));
073            assertEquals(null, params.get("test2"));
074            assertEquals(null, params.get("test3"));
075    
076            s = "  test";
077            params = parser.parse(s, ';');
078            assertEquals(null, params.get("test"));
079    
080            s = "  ";
081            params = parser.parse(s, ';');
082            assertEquals(0, params.size());
083    
084            s = " = stuff ";
085            params = parser.parse(s, ';');
086            assertEquals(0, params.size());
087        }
088    
089        public void testContentTypeParsing()
090        {
091            String s = "text/plain; Charset=UTF-8";
092            ParameterParser parser = new ParameterParser();
093            parser.setLowerCaseNames(true);
094            Map params = parser.parse(s, ';');
095            assertEquals("UTF-8", params.get("charset"));
096        }
097    
098        public void testParsingEscapedChars()
099        {
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    }