View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.http.message;
28  
29  import java.util.Iterator;
30  import java.util.NoSuchElementException;
31  
32  import org.apache.hc.core5.http.Header;
33  import org.apache.hc.core5.http.HeaderElement;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Tests for {@link BasicHeaderElementIterator}.
39   *
40   */
41  public class TestBasicHeaderElementIterator {
42  
43      @Test
44      public void testMultiHeader() {
45          final Header[] headers = new Header[]{
46                  new BasicHeader("Name", "value0"),
47                  new BasicHeader("Name", "value1")
48          };
49  
50          final Iterator<HeaderElement> hei = new BasicHeaderElementIterator(
51                  new BasicHeaderIterator(headers, "Name"));
52  
53          Assertions.assertTrue(hei.hasNext());
54          HeaderElement elem = hei.next();
55          Assertions.assertEquals("value0", elem.getName(), "The two header values must be equal");
56  
57          Assertions.assertTrue(hei.hasNext());
58          elem = hei.next();
59          Assertions.assertEquals("value1", elem.getName(), "The two header values must be equal");
60  
61          Assertions.assertFalse(hei.hasNext());
62          Assertions.assertThrows(NoSuchElementException.class, () -> hei.next());
63  
64          Assertions.assertFalse(hei.hasNext());
65          Assertions.assertThrows(NoSuchElementException.class, () -> hei.next());
66      }
67  
68      @Test
69      public void testMultiHeaderSameLine() {
70          final Header[] headers = new Header[]{
71                  new BasicHeader("name", "value0,value1"),
72                  new BasicHeader("nAme", "cookie1=1,cookie2=2")
73          };
74  
75          final Iterator<HeaderElement> hei = new BasicHeaderElementIterator(
76                  new BasicHeaderIterator(headers, "Name"));
77  
78          HeaderElement elem = hei.next();
79          Assertions.assertEquals("value0", elem.getName(), "The two header values must be equal");
80          elem = hei.next();
81          Assertions.assertEquals("value1", elem.getName(), "The two header values must be equal");
82          elem = hei.next();
83          Assertions.assertEquals("cookie1", elem.getName(), "The two header values must be equal");
84          Assertions.assertEquals("1", elem.getValue(), "The two header values must be equal");
85  
86          elem = hei.next();
87          Assertions.assertEquals("cookie2", elem.getName(), "The two header values must be equal");
88          Assertions.assertEquals("2", elem.getValue(), "The two header values must be equal");
89      }
90  
91      @Test
92      public void testFringeCases() {
93          final Header[] headers = new Header[]{
94                  new BasicHeader("Name", null),
95                  new BasicHeader("Name", "    "),
96                  new BasicHeader("Name", ",,,")
97          };
98  
99          final Iterator<HeaderElement> hei = new BasicHeaderElementIterator(
100                 new BasicHeaderIterator(headers, "Name"));
101 
102         Assertions.assertFalse(hei.hasNext());
103         Assertions.assertThrows(NoSuchElementException.class, () -> hei.next());
104         Assertions.assertFalse(hei.hasNext());
105         Assertions.assertThrows(NoSuchElementException.class, () -> hei.next());
106     }
107 
108 }