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