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