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.assertFalse;
    21  import static org.junit.jupiter.api.Assertions.assertThrows;
    22  import static org.junit.jupiter.api.Assertions.assertTrue;
    23
    24  import java.io.UnsupportedEncodingException;
    25
    26  import org.junit.jupiter.api.Test;
    27
    28  /**
    29   * The expected characters are encoded in UTF16, while the actual characters may be encoded in UTF-8/ISO-8859-1
    30   *
    31   * RFC 5987 recommends to support both UTF-8 and ISO 8859-1. Test values are taken from https://tools.ietf.org/html/rfc5987#section-3.2.2
    32   */
    33  public final class RFC2231UtilityTestCase {
    34
    35      private static void assertEncoded(final String expected, final String encoded) throws Exception {
    36          assertEquals(expected, RFC2231Utils.decodeText(encoded));
    37      }
    38
    39      @Test
    40      public void testDecodeInvalidEncoding() throws Exception {
    41          assertThrows(UnsupportedEncodingException.class, () -> RFC2231Utils.decodeText("abc'en'hello"));
    42      }
    43
    44      @Test
    45      public void testDecodeIso88591() throws Exception {
    46          assertEncoded("\u00A3 rate", "iso-8859-1'en'%A3%20rate"); // "£ rate"
    47      }
    48
    49      @Test
    50      public void testDecodeUtf8() throws Exception {
    51          assertEncoded("\u00a3 \u0061\u006e\u0064 \u20ac \u0072\u0061\u0074\u0065\u0073", "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates"); // "£ and € rates"
    52      }
    53
    54      @Test
    55      public void testHasEncodedValue() {
    56          final var nameWithAsteriskAtEnd = "paramname*";
    57          assertTrue(RFC2231Utils.hasEncodedValue(nameWithAsteriskAtEnd));
    58
    59          final var nameWithAsteriskNotAtEnd = "param*name";
    60          assertFalse(RFC2231Utils.hasEncodedValue(nameWithAsteriskNotAtEnd));
    61
    62          final var nameWithoutAsterisk = "paramname";
    63          assertFalse(RFC2231Utils.hasEncodedValue(nameWithoutAsterisk));
    64      }
    65
    66      @Test
    67      public void testNoNeedToDecode() throws Exception {
    68          assertEncoded("abc", "abc");
    69      }
    70
    71      @Test
    72      public void testStripDelimiter() {
    73          final var nameWithAsteriskAtEnd = "paramname*";
    74          assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithAsteriskAtEnd));
    75
    76          final var nameWithAsteriskNotAtEnd = "param*name";
    77          assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithAsteriskNotAtEnd));
    78
    79          final var nameWithTwoAsterisks = "param*name*";
    80          assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithTwoAsterisks));
    81
    82          final var nameWithoutAsterisk = "paramname";
    83          assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithoutAsterisk));
    84      }
    85  }