    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 */
    017package org.apache.commons.fileupload2.core;
    018
    019import static org.junit.jupiter.api.Assertions.assertEquals;
    020import static org.junit.jupiter.api.Assertions.assertFalse;
    021import static org.junit.jupiter.api.Assertions.assertThrows;
    022import static org.junit.jupiter.api.Assertions.assertTrue;
    023
    024import java.io.UnsupportedEncodingException;
    025
    026import org.junit.jupiter.api.Test;
    027
    028/**
    029 * The expected characters are encoded in UTF16, while the actual characters may be encoded in UTF-8/ISO-8859-1
    030 *
    031 * 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
    032 */
    033public final class RFC2231UtilityTestCase {
    034
    035    private static void assertEncoded(final String expected, final String encoded) throws Exception {
    036        assertEquals(expected, RFC2231Utils.decodeText(encoded));
    037    }
    038
    039    @Test
    040    public void testDecodeInvalidEncoding() throws Exception {
    041        assertThrows(UnsupportedEncodingException.class, () -> RFC2231Utils.decodeText("abc'en'hello"));
    042    }
    043
    044    @Test
    045    public void testDecodeIso88591() throws Exception {
    046        assertEncoded("\u00A3 rate", "iso-8859-1'en'%A3%20rate"); // "£ rate"
    047    }
    048
    049    @Test
    050    public void testDecodeUtf8() throws Exception {
    051        assertEncoded("\u00a3 \u0061\u006e\u0064 \u20ac \u0072\u0061\u0074\u0065\u0073", "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates"); // "£ and € rates"
    052    }
    053
    054    @Test
    055    public void testHasEncodedValue() {
    056        final var nameWithAsteriskAtEnd = "paramname*";
    057        assertTrue(RFC2231Utils.hasEncodedValue(nameWithAsteriskAtEnd));
    058
    059        final var nameWithAsteriskNotAtEnd = "param*name";
    060        assertFalse(RFC2231Utils.hasEncodedValue(nameWithAsteriskNotAtEnd));
    061
    062        final var nameWithoutAsterisk = "paramname";
    063        assertFalse(RFC2231Utils.hasEncodedValue(nameWithoutAsterisk));
    064    }
    065
    066    @Test
    067    public void testNoNeedToDecode() throws Exception {
    068        assertEncoded("abc", "abc");
    069    }
    070
    071    @Test
    072    public void testStripDelimiter() {
    073        final var nameWithAsteriskAtEnd = "paramname*";
    074        assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithAsteriskAtEnd));
    075
    076        final var nameWithAsteriskNotAtEnd = "param*name";
    077        assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithAsteriskNotAtEnd));
    078
    079        final var nameWithTwoAsterisks = "param*name*";
    080        assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithTwoAsterisks));
    081
    082        final var nameWithoutAsterisk = "paramname";
    083        assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithoutAsterisk));
    084    }
    085}