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