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  
28  package org.apache.hc.core5.http.message;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.util.Iterator;
35  
36  import org.apache.hc.core5.http.Header;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Unit tests for {@link HeaderGroup}.
42   *
43   */
44  public class TestHeaderGroup {
45  
46      @Test
47      public void testConstructor() {
48          final HeaderGroup headergroup = new HeaderGroup();
49          Assert.assertNotNull(headergroup.getAllHeaders());
50          Assert.assertEquals(0, headergroup.getAllHeaders().length);
51      }
52  
53      @Test
54      public void testClear() {
55          final HeaderGroup headergroup = new HeaderGroup();
56          headergroup.addHeader(new BasicHeader("name", "value"));
57          Assert.assertEquals(1, headergroup.getAllHeaders().length);
58          headergroup.clear();
59          Assert.assertEquals(0, headergroup.getAllHeaders().length);
60      }
61  
62      @Test
63      public void testAddRemoveHeader() {
64          final HeaderGroup headergroup = new HeaderGroup();
65          final Header header = new BasicHeader("name", "value");
66          headergroup.addHeader(header);
67          headergroup.addHeader(null);
68          Assert.assertEquals(1, headergroup.getAllHeaders().length);
69          headergroup.removeHeader(header);
70          headergroup.removeHeader(null);
71          Assert.assertEquals(0, headergroup.getAllHeaders().length);
72      }
73  
74      @Test
75      public void testAddRemoveHeaderWithDifferentButEqualHeaders() {
76          final HeaderGroup headergroup = new HeaderGroup();
77          final Header header = new BasicHeader("name", "value");
78          final Header header2 = new BasicHeader("name", "value");
79          headergroup.addHeader(header);
80          Assert.assertEquals(1, headergroup.getAllHeaders().length);
81          headergroup.removeHeader(header2);
82          Assert.assertEquals(0, headergroup.getAllHeaders().length);
83      }
84  
85      @Test
86      public void testUpdateHeader() {
87          final HeaderGroup headergroup = new HeaderGroup();
88          final Header header1 = new BasicHeader("name1", "value1");
89          final Header header2 = new BasicHeader("name2", "value2");
90          final Header header3 = new BasicHeader("name3", "value3");
91          headergroup.addHeader(header1);
92          headergroup.addHeader(header2);
93          headergroup.addHeader(header3);
94          headergroup.setHeader(new BasicHeader("name2", "newvalue"));
95          headergroup.setHeader(new BasicHeader("name4", "value4"));
96          headergroup.setHeader(null);
97          Assert.assertEquals(4, headergroup.getAllHeaders().length);
98          Assert.assertEquals("newvalue", headergroup.getFirstHeader("name2").getValue());
99      }
100 
101     @Test
102     public void testSetHeaders() {
103         final HeaderGroup headergroup = new HeaderGroup();
104         final Header header1 = new BasicHeader("name1", "value1");
105         final Header header2 = new BasicHeader("name2", "value2");
106         final Header header3 = new BasicHeader("name3", "value3");
107         headergroup.addHeader(header1);
108         headergroup.setHeaders(new Header[] { header2, header3 });
109         Assert.assertEquals(2, headergroup.getAllHeaders().length);
110         Assert.assertEquals(0, headergroup.getHeaders("name1").length);
111         Assert.assertFalse(headergroup.containsHeader("name1"));
112         Assert.assertEquals(1, headergroup.getHeaders("name2").length);
113         Assert.assertTrue(headergroup.containsHeader("name2"));
114         Assert.assertEquals(1, headergroup.getHeaders("name3").length);
115         Assert.assertTrue(headergroup.containsHeader("name3"));
116         headergroup.setHeaders(null);
117         Assert.assertEquals(0, headergroup.getAllHeaders().length);
118     }
119 
120     @Test
121     public void testFirstLastHeaders() {
122         final HeaderGroup headergroup = new HeaderGroup();
123         final Header header1 = new BasicHeader("name", "value1");
124         final Header header2 = new BasicHeader("name", "value2");
125         final Header header3 = new BasicHeader("name", "value3");
126         headergroup.setHeaders(new Header[] { header1, header2, header3 });
127 
128         Assert.assertNull(headergroup.getFirstHeader("whatever"));
129         Assert.assertNull(headergroup.getLastHeader("whatever"));
130 
131         Assert.assertEquals("value1", headergroup.getFirstHeader("name").getValue());
132         Assert.assertEquals("value3", headergroup.getLastHeader("name").getValue());
133     }
134 
135     @Test
136     public void testCondensedHeader() {
137         final HeaderGroup headergroup = new HeaderGroup();
138         Assert.assertNull(headergroup.getCondensedHeader("name"));
139 
140         final Header header1 = new BasicHeader("name", "value1");
141         final Header header2 = new BasicHeader("name", "value2");
142         final Header header3 = new BasicHeader("name", "value3");
143         headergroup.setHeaders(new Header[] { header1, header2, header3 });
144 
145         Assert.assertEquals("value1, value2, value3", headergroup.getCondensedHeader("name").getValue());
146 
147         headergroup.setHeaders(new Header[] { header1 });
148         Assert.assertEquals(header1, headergroup.getCondensedHeader("name"));
149     }
150 
151     @Test
152     public void testIterator() {
153         final HeaderGroup headergroup = new HeaderGroup();
154         final Iterator<Header> i = headergroup.headerIterator();
155         Assert.assertNotNull(i);
156         Assert.assertFalse(i.hasNext());
157     }
158 
159     @Test
160     public void testHeaderRemove() {
161         final HeaderGroup headergroup = new HeaderGroup();
162         final Header header1 = new BasicHeader("name", "value1");
163         final Header header2 = new BasicHeader("name", "value2");
164         final Header header3 = new BasicHeader("name", "value3");
165         headergroup.setHeaders(new Header[] { header1, header2, header3 });
166         final Iterator<Header> i = headergroup.headerIterator();
167         Assert.assertNotNull(i);
168         Assert.assertTrue(i.hasNext());
169         i.next();
170         Assert.assertTrue(i.hasNext());
171         i.next();
172         i.remove();
173         Assert.assertEquals(2, headergroup.getAllHeaders().length);
174         Assert.assertTrue(i.hasNext());
175         i.next();
176         i.remove();
177         Assert.assertEquals(1, headergroup.getAllHeaders().length);
178         Assert.assertFalse(i.hasNext());
179     }
180 
181     @Test
182     public void testSerialization() throws Exception {
183         final HeaderGroup orig = new HeaderGroup();
184         final Header header1 = new BasicHeader("name", "value1");
185         final Header header2 = new BasicHeader("name", "value2");
186         final Header header3 = new BasicHeader("name", "value3");
187         orig.setHeaders(new Header[] { header1, header2, header3 });
188         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
189         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
190         outstream.writeObject(orig);
191         outstream.close();
192         final byte[] raw = outbuffer.toByteArray();
193         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
194         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
195         final HeaderGroup clone = (HeaderGroup) instream.readObject();
196         final Header[] headers1 = orig.getAllHeaders();
197         final Header[] headers2 = clone.getAllHeaders();
198         Assert.assertNotNull(headers1);
199         Assert.assertNotNull(headers2);
200         Assert.assertEquals(headers1.length, headers2.length);
201         for (int i = 0; i < headers1.length; i++) {
202             Assert.assertEquals(headers1[i].getName(), headers2[i].getName());
203             Assert.assertEquals(headers1[i].getValue(), headers2[i].getValue());
204         }
205     }
206 
207 }