    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.assertNull;
    022import static org.junit.jupiter.api.Assertions.assertTrue;
    023
    024import java.util.Iterator;
    025
    026import org.junit.jupiter.api.Test;
    027
    028/**
    029 * Unit tests {@link FileItemHeaders} and {@link FileItemHeadersImpl}.
    030 */
    031public class FileItemHeadersTest {
    032
    033    /**
    034     * @throws Exception
    035     */
    036    @Test
    037    public void testFileItemHeaders() throws Exception {
    038        final var mutableFileItemHeaders = new FileItemHeadersImpl();
    039        mutableFileItemHeaders.addHeader("Content-Disposition", "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
    040        mutableFileItemHeaders.addHeader("Content-Type", "text/plain");
    041
    042        mutableFileItemHeaders.addHeader("TestHeader", "headerValue1");
    043        mutableFileItemHeaders.addHeader("TestHeader", "headerValue2");
    044        mutableFileItemHeaders.addHeader("TestHeader", "headerValue3");
    045        mutableFileItemHeaders.addHeader("testheader", "headerValue4");
    046
    047        final var headerNameIterator = mutableFileItemHeaders.getHeaderNames();
    048        assertEquals("content-disposition", headerNameIterator.next());
    049        assertEquals("content-type", headerNameIterator.next());
    050        assertEquals("testheader", headerNameIterator.next());
    051        assertFalse(headerNameIterator.hasNext());
    052
    053        assertEquals(mutableFileItemHeaders.getHeader("Content-Disposition"), "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
    054        assertEquals(mutableFileItemHeaders.getHeader("Content-Type"), "text/plain");
    055        assertEquals(mutableFileItemHeaders.getHeader("content-type"), "text/plain");
    056        assertEquals(mutableFileItemHeaders.getHeader("TestHeader"), "headerValue1");
    057        assertNull(mutableFileItemHeaders.getHeader("DummyHeader"));
    058
    059        Iterator<String> headerValueEnumeration;
    060
    061        headerValueEnumeration = mutableFileItemHeaders.getHeaders("Content-Type");
    062        assertTrue(headerValueEnumeration.hasNext());
    063        assertEquals(headerValueEnumeration.next(), "text/plain");
    064        assertFalse(headerValueEnumeration.hasNext());
    065
    066        headerValueEnumeration = mutableFileItemHeaders.getHeaders("content-type");
    067        assertTrue(headerValueEnumeration.hasNext());
    068        assertEquals(headerValueEnumeration.next(), "text/plain");
    069        assertFalse(headerValueEnumeration.hasNext());
    070
    071        headerValueEnumeration = mutableFileItemHeaders.getHeaders("TestHeader");
    072        assertTrue(headerValueEnumeration.hasNext());
    073        assertEquals(headerValueEnumeration.next(), "headerValue1");
    074        assertTrue(headerValueEnumeration.hasNext());
    075        assertEquals(headerValueEnumeration.next(), "headerValue2");
    076        assertTrue(headerValueEnumeration.hasNext());
    077        assertEquals(headerValueEnumeration.next(), "headerValue3");
    078        assertTrue(headerValueEnumeration.hasNext());
    079        assertEquals(headerValueEnumeration.next(), "headerValue4");
    080        assertFalse(headerValueEnumeration.hasNext());
    081
    082        headerValueEnumeration = mutableFileItemHeaders.getHeaders("DummyHeader");
    083        assertFalse(headerValueEnumeration.hasNext());
    084    }
    085
    086}