    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.assertThrows;
    021
    022import java.io.UnsupportedEncodingException;
    023
    024import org.junit.jupiter.api.Test;
    025
    026/**
    027 * Use the online <a href="http://dogmamix.com/MimeHeadersDecoder/">MimeHeadersDecoder</a> to validate expected values.
    028 */
    029public final class MimeUtilityTestCase {
    030
    031    private static void assertEncoded(final String expected, final String encoded) throws Exception {
    032        assertEquals(expected, MimeUtils.decodeText(encoded));
    033    }
    034
    035    @Test
    036    public void testDecodeInvalidEncoding() {
    037        assertThrows(UnsupportedEncodingException.class, () -> MimeUtils.decodeText("=?invalid?B?xyz-?="));
    038    }
    039
    040    @Test
    041    public void testDecodeIso88591Base64Encoded() throws Exception {
    042        assertEncoded("If you can read this you understand the example.",
    043                "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= " + "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
    044    }
    045
    046    @Test
    047    public void testDecodeIso88591Base64EncodedWithWhiteSpace() throws Exception {
    048        assertEncoded("If you can read this you understand the example.",
    049                "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\t  \r\n   =?ISO-8859-" + "2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
    050    }
    051
    052    @Test
    053    public void testDecodeUtf8Base64Encoded() throws Exception {
    054        assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?=");
    055    }
    056
    057    @Test
    058    public void testDecodeUtf8QuotedPrintableEncoded() throws Exception {
    059        assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?=");
    060    }
    061
    062    @Test
    063    public void testNoNeedToDecode() throws Exception {
    064        assertEncoded("abc", "abc");
    065    }
    066}