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   * Tests for {@link ParameterParser}.
    28   */
    29  public class ParameterParserTest {
    30
    31      @Test
    32      public void testContentTypeParsing() {
    33          final String s = "text/plain; Charset=UTF-8";
    34          final ParameterParser parser = new ParameterParser();
    35          parser.setLowerCaseNames(true);
    36          final Map<String, String> params = parser.parse(s, ';');
    37          assertEquals("UTF-8", params.get("charset"));
    38      }
    39
    40      // See: http://issues.apache.org/jira/browse/FILEUPLOAD-139
    41      @Test
    42      public void testFileUpload139() {
    43          final ParameterParser parser = new ParameterParser();
    44          String s = "Content-type: multipart/form-data , boundary=AaB03x";
    45          Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
    46          assertEquals("AaB03x", params.get("boundary"));
    47
    48          s = "Content-type: multipart/form-data, boundary=AaB03x";
    49          params = parser.parse(s, new char[] { ';', ',' });
    50          assertEquals("AaB03x", params.get("boundary"));
    51
    52          s = "Content-type: multipart/mixed, boundary=BbC04y";
    53          params = parser.parse(s, new char[] { ',', ';' });
    54          assertEquals("BbC04y", params.get("boundary"));
    55      }
    56
    57      /**
    58       * Tests <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a>.
    59       */
    60      @Test
    61      public void testFileUpload199() {
    62          final ParameterParser parser = new ParameterParser();
    63          final String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n";
    64          final Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
    65          assertEquals("If you can read this you understand the example.", params.get("filename"));
    66      }
    67
    68      /**
    69       * Tests <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-274">FILEUPLOAD-274</a>.
    70       */
    71      @Test
    72      public void testFileUpload274() {
    73          final ParameterParser parser = new ParameterParser();
    74
    75          // Should parse a UTF-8 charset
    76          String s = "Content-Disposition: form-data; name=\"file\"; filename*=UTF-8\'\'%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF\r\n";
    77          Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
    78          assertEquals("\u3053\u3093\u306B\u3061\u306F", params.get("filename")); //filename = "こんにちは" in japanese
    79
    80          // Should parse ISO-8859-1 charset
    81          s = "Content-Disposition: form-data; name=\"file\"; filename*=UTF-8\'\'%70%C3%A2%74%C3%A9\r\n";
    82          params = parser.parse(s, new char[] { ',', ';' });
    83          assertEquals("\u0070\u00e2\u0074\u00e9", params.get("filename")); //filename = "pâté" in french
    84
    85          // Should not decode if '*' is not at the end of param-name
    86          s = "Content-Disposition: form-data; name=\"file\"; file*name=UTF-8\'\'%61%62%63\r\n";
    87          params = parser.parse(s, new char[] { ',', ';' });
    88          assertEquals("UTF-8\'\'%61%62%63", params.get("file*name"));
    89
    90          // Should not decode if param-value does not follow <charset>'<lang>'<encoded>
    91          s = "Content-Disposition: form-data; name=\"file\"; filename*=a\'bc\r\n";
    92          params = parser.parse(s, new char[] { ',', ';' });
    93          assertEquals("a\'bc", params.get("filename"));
    94
    95          // Should not decode if param-name doesn't have '*' at end
    96          s = "Content-Disposition: form-data; name=\"file\"; filename=a\'b\'c\r\n";
    97          params = parser.parse(s, new char[] { ',', ';' });
    98          assertEquals("a\'b\'c", params.get("filename"));
    99      }
    100
    101     @Test
    102     public void testParsing() {
    103         String s =
    104             "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
    105         final ParameterParser parser = new ParameterParser();
    106         Map<String, String> params = parser.parse(s, ';');
    107         assertEquals(null, params.get("test"));
    108         assertEquals("stuff", params.get("test1"));
    109         assertEquals("stuff; stuff", params.get("test2"));
    110         assertEquals("\"stuff", params.get("test3"));
    111
    112         params = parser.parse(s, new char[] { ',', ';' });
    113         assertEquals(null, params.get("test"));
    114         assertEquals("stuff", params.get("test1"));
    115         assertEquals("stuff; stuff", params.get("test2"));
    116         assertEquals("\"stuff", params.get("test3"));
    117
    118         s = "  test  , test1=stuff   ,  , test2=, test3, ";
    119         params = parser.parse(s, ',');
    120         assertEquals(null, params.get("test"));
    121         assertEquals("stuff", params.get("test1"));
    122         assertEquals(null, params.get("test2"));
    123         assertEquals(null, params.get("test3"));
    124
    125         s = "  test";
    126         params = parser.parse(s, ';');
    127         assertEquals(null, params.get("test"));
    128
    129         s = "  ";
    130         params = parser.parse(s, ';');
    131         assertEquals(0, params.size());
    132
    133         s = " = stuff ";
    134         params = parser.parse(s, ';');
    135         assertEquals(0, params.size());
    136     }
    137
    138     @Test
    139     public void testParsingEscapedChars() {
    140         String s = "param = \"stuff\\\"; more stuff\"";
    141         final ParameterParser parser = new ParameterParser();
    142         Map<String, String> params = parser.parse(s, ';');
    143         assertEquals(1, params.size());
    144         assertEquals("stuff\\\"; more stuff", params.get("param"));
    145
    146         s = "param = \"stuff\\\\\"; anotherparam";
    147         params = parser.parse(s, ';');
    148         assertEquals(2, params.size());
    149         assertEquals("stuff\\\\", params.get("param"));
    150         assertNull(params.get("anotherparam"));
    151     }
    152
    153 }