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