View Javadoc

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