    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 java.util.Iterator;
    20
    21  import org.apache.commons.fileupload.util.FileItemHeadersImpl;
    22
    23  import junit.framework.TestCase;
    24
    25  /**
    26   * Unit tests {@link FileItemHeaders} and
    27   * {@link FileItemHeadersImpl}.
    28   *
    29   * @author Michael C. Macaluso
    30   */
    31  public class FileItemHeadersTest extends TestCase {
    32      /**
    33       * @throws Exception
    34       */
    35      public void testFileItemHeaders() throws Exception {
    36      	FileItemHeadersImpl aMutableFileItemHeaders = new FileItemHeadersImpl();
    37      	aMutableFileItemHeaders.addHeader("Content-Disposition", "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
    38      	aMutableFileItemHeaders.addHeader("Content-Type", "text/plain");
    39
    40      	aMutableFileItemHeaders.addHeader("TestHeader", "headerValue1");
    41      	aMutableFileItemHeaders.addHeader("TestHeader", "headerValue2");
    42      	aMutableFileItemHeaders.addHeader("TestHeader", "headerValue3");
    43      	aMutableFileItemHeaders.addHeader("testheader", "headerValue4");
    44
    45      	Iterator headerNameEnumeration = aMutableFileItemHeaders.getHeaderNames();
    46      	assertEquals("content-disposition", (String) headerNameEnumeration.next());
    47      	assertEquals("content-type", (String) headerNameEnumeration.next());
    48      	assertEquals("testheader", (String) headerNameEnumeration.next());
    49      	assertFalse(headerNameEnumeration.hasNext());
    50
    51      	assertEquals(aMutableFileItemHeaders.getHeader("Content-Disposition"), "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
    52      	assertEquals(aMutableFileItemHeaders.getHeader("Content-Type"), "text/plain");
    53      	assertEquals(aMutableFileItemHeaders.getHeader("content-type"), "text/plain");
    54      	assertEquals(aMutableFileItemHeaders.getHeader("TestHeader"), "headerValue1");
    55      	assertNull(aMutableFileItemHeaders.getHeader("DummyHeader"));
    56
    57      	Iterator headerValueEnumeration;
    58
    59      	headerValueEnumeration = aMutableFileItemHeaders.getHeaders("Content-Type");
    60      	assertTrue(headerValueEnumeration.hasNext());
    61      	assertEquals(headerValueEnumeration.next(), "text/plain");
    62      	assertFalse(headerValueEnumeration.hasNext());
    63
    64      	headerValueEnumeration = aMutableFileItemHeaders.getHeaders("content-type");
    65      	assertTrue(headerValueEnumeration.hasNext());
    66      	assertEquals(headerValueEnumeration.next(), "text/plain");
    67      	assertFalse(headerValueEnumeration.hasNext());
    68
    69      	headerValueEnumeration = aMutableFileItemHeaders.getHeaders("TestHeader");
    70      	assertTrue(headerValueEnumeration.hasNext());
    71      	assertEquals(headerValueEnumeration.next(), "headerValue1");
    72      	assertTrue(headerValueEnumeration.hasNext());
    73      	assertEquals(headerValueEnumeration.next(), "headerValue2");
    74      	assertTrue(headerValueEnumeration.hasNext());
    75      	assertEquals(headerValueEnumeration.next(), "headerValue3");
    76      	assertTrue(headerValueEnumeration.hasNext());
    77      	assertEquals(headerValueEnumeration.next(), "headerValue4");
    78      	assertFalse(headerValueEnumeration.hasNext());
    79
    80      	headerValueEnumeration = aMutableFileItemHeaders.getHeaders("DummyHeader");
    81      	assertFalse(headerValueEnumeration.hasNext());
    82      }
    83
    84  }