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.Assert;
35  import org.junit.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          Assert.assertTrue(hei.hasNext());
54          HeaderElement elem = hei.next();
55          Assert.assertEquals("The two header values must be equal",
56                  "value0", elem.getName());
57  
58          Assert.assertTrue(hei.hasNext());
59          elem = hei.next();
60          Assert.assertEquals("The two header values must be equal",
61                  "value1", elem.getName());
62  
63          Assert.assertFalse(hei.hasNext());
64          try {
65              hei.next();
66              Assert.fail("NoSuchElementException should have been thrown");
67          } catch (final NoSuchElementException ex) {
68              // expected
69          }
70  
71          Assert.assertFalse(hei.hasNext());
72          try {
73              hei.next();
74              Assert.fail("NoSuchElementException should have been thrown");
75          } catch (final NoSuchElementException ex) {
76              // expected
77          }
78      }
79  
80      @Test
81      public void testMultiHeaderSameLine() {
82          final Header[] headers = new Header[]{
83                  new BasicHeader("name", "value0,value1"),
84                  new BasicHeader("nAme", "cookie1=1,cookie2=2")
85          };
86  
87          final Iterator<HeaderElement> hei = new BasicHeaderElementIterator(
88                  new BasicHeaderIterator(headers, "Name"));
89  
90          HeaderElement elem = hei.next();
91          Assert.assertEquals("The two header values must be equal",
92                  "value0", elem.getName());
93          elem = hei.next();
94          Assert.assertEquals("The two header values must be equal",
95                  "value1", elem.getName());
96          elem = hei.next();
97          Assert.assertEquals("The two header values must be equal",
98                  "cookie1", elem.getName());
99          Assert.assertEquals("The two header values must be equal",
100                 "1", elem.getValue());
101 
102         elem = hei.next();
103         Assert.assertEquals("The two header values must be equal",
104                 "cookie2", elem.getName());
105         Assert.assertEquals("The two header values must be equal",
106                 "2", elem.getValue());
107     }
108 
109     @Test
110     public void testFringeCases() {
111         final Header[] headers = new Header[]{
112                 new BasicHeader("Name", null),
113                 new BasicHeader("Name", "    "),
114                 new BasicHeader("Name", ",,,")
115         };
116 
117         final Iterator<HeaderElement> hei = new BasicHeaderElementIterator(
118                 new BasicHeaderIterator(headers, "Name"));
119 
120         Assert.assertFalse(hei.hasNext());
121         try {
122             hei.next();
123             Assert.fail("NoSuchElementException should have been thrown");
124         } catch (final NoSuchElementException ex) {
125             // expected
126         }
127 
128         Assert.assertFalse(hei.hasNext());
129         try {
130             hei.next();
131             Assert.fail("NoSuchElementException should have been thrown");
132         } catch (final NoSuchElementException ex) {
133             // expected
134         }
135     }
136 
137 }