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.fileupload2.core;
    18
    19  import static org.junit.jupiter.api.Assertions.assertEquals;
    20  import static org.junit.jupiter.api.Assertions.assertNull;
    21
    22  import java.nio.charset.StandardCharsets;
    23
    24  import org.junit.jupiter.api.Test;
    25
    26  /**
    27   * Unit tests for {@link ParameterParser}.
    28   */
    29  public class ParameterParserTest {
    30
    31      @Test
    32      public void testContentTypeParsing() {
    33          final var s = "text/plain; Charset=UTF-8";
    34          final var parser = new ParameterParser();
    35          parser.setLowerCaseNames(true);
    36          final var params = parser.parse(s, ';');
    37          assertEquals(StandardCharsets.UTF_8.name(), params.get("charset"));
    38      }
    39
    40      // See: https://issues.apache.org/jira/browse/FILEUPLOAD-139
    41      @Test
    42      public void testFileUpload139() {
    43          final var parser = new ParameterParser();
    44          var s = "Content-type: multipart/form-data , boundary=AaB03x";
    45          var 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       * Test for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a>
    59       */
    60      @Test
    61      public void testFileUpload199() {
    62          final var parser = new ParameterParser();
    63          final var s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-"
    64                  + "1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n";
    65          final var params = parser.parse(s, new char[] { ',', ';' });
    66          assertEquals("If you can read this you understand the example.", params.get("filename"));
    67      }
    68
    69      /**
    70       * Test for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-274">FILEUPLOAD-274</a>
    71       */
    72      @Test
    73      public void testFileUpload274() {
    74          final var parser = new ParameterParser();
    75
    76          // Should parse a UTF-8 charset
    77          var 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";
    78          var params = parser.parse(s, new char[] { ',', ';' });
    79          assertEquals("\u3053\u3093\u306B\u3061\u306F", params.get("filename")); // filename = "こんにちは" in japanese
    80
    81          // Should parse ISO-8859-1 charset
    82          s = "Content-Disposition: form-data; name=\"file\"; filename*=UTF-8''%70%C3%A2%74%C3%A9\r\n";
    83          params = parser.parse(s, new char[] { ',', ';' });
    84          assertEquals("\u0070\u00e2\u0074\u00e9", params.get("filename")); // filename = "pâté" in french
    85
    86          // Should not decode if '*' is not at the end of param-name
    87          s = "Content-Disposition: form-data; name=\"file\"; file*name=UTF-8''%61%62%63\r\n";
    88          params = parser.parse(s, new char[] { ',', ';' });
    89          assertEquals("UTF-8''%61%62%63", params.get("file*name"));
    90
    91          // Should not decode if param-value does not follow <charset>'<lang>'<encoded>
    92          s = "Content-Disposition: form-data; name=\"file\"; filename*=a'bc\r\n";
    93          params = parser.parse(s, new char[] { ',', ';' });
    94          assertEquals("a'bc", params.get("filename"));
    95
    96          // Should not decode if param-name doesn't have '*' at end
    97          s = "Content-Disposition: form-data; name=\"file\"; filename=a'b'c\r\n";
    98          params = parser.parse(s, new char[] { ',', ';' });
    99          assertEquals("a'b'c", params.get("filename"));
    100     }
    101
    102     @Test
    103     public void testParsing() {
    104         var s = "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
    105         final var parser = new ParameterParser();
    106         var params = parser.parse(s, ';');
    107         assertNull(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         assertNull(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         assertNull(params.get("test"));
    121         assertEquals("stuff", params.get("test1"));
    122         assertNull(params.get("test2"));
    123         assertNull(params.get("test3"));
    124
    125         s = "  test";
    126         params = parser.parse(s, ';');
    127         assertNull(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         var s = "param = \"stuff\\\"; more stuff\"";
    141         final var parser = new ParameterParser();
    142         var 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 }