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          Assert.assertTrue(headerGroup.removeHeader(header));
70          Assert.assertFalse(headerGroup.removeHeader(null));
71          Assert.assertEquals(0, headerGroup.getAllHeaders().length);
72      }
73  
74      @Test
75      public void testAddRemoveHeaders() {
76          final HeaderGroup headergroup = new HeaderGroup();
77          final Header header = new BasicHeader("name", "value");
78          headergroup.addHeader(header);
79          headergroup.addHeader(header);
80          Assert.assertEquals(2, headergroup.getAllHeaders().length);
81          Assert.assertFalse(headergroup.removeHeaders((Header) null));
82          Assert.assertTrue(headergroup.removeHeaders(header));
83          Assert.assertFalse(headergroup.removeHeaders((Header) null));
84          Assert.assertEquals(0, headergroup.getAllHeaders().length);
85      }
86  
87      @Test
88      public void testAddRemoveHeaderWithDifferentButEqualHeaders() {
89          final HeaderGroup headergroup = new HeaderGroup();
90          final Header header = new BasicHeader("name", "value");
91          final Header header2 = new BasicHeader("name", "value");
92          headergroup.addHeader(header);
93          Assert.assertEquals(1, headergroup.getAllHeaders().length);
94          Assert.assertTrue(headergroup.removeHeader(header2));
95          Assert.assertEquals(0, headergroup.getAllHeaders().length);
96      }
97  
98      @Test
99      public void testUpdateHeader() {
100         final HeaderGroup headergroup = new HeaderGroup();
101         final Header header1 = new BasicHeader("name1", "value1");
102         final Header header2 = new BasicHeader("name2", "value2");
103         final Header header3 = new BasicHeader("name3", "value3");
104         headergroup.addHeader(header1);
105         headergroup.addHeader(header2);
106         headergroup.addHeader(header3);
107         headergroup.setHeader(new BasicHeader("name2", "newvalue"));
108         headergroup.setHeader(new BasicHeader("name4", "value4"));
109         headergroup.setHeader(null);
110         Assert.assertEquals(4, headergroup.getAllHeaders().length);
111         Assert.assertEquals("newvalue", headergroup.getFirstHeader("name2").getValue());
112     }
113 
114     @Test
115     public void testSetHeaders() {
116         final HeaderGroup headergroup = new HeaderGroup();
117         final Header header1 = new BasicHeader("name1", "value1");
118         final Header header2 = new BasicHeader("name2", "value2");
119         final Header header3 = new BasicHeader("name3", "value3");
120         headergroup.addHeader(header1);
121         headergroup.setHeaders(new Header[] { header2, header3 });
122         Assert.assertEquals(2, headergroup.getAllHeaders().length);
123         Assert.assertEquals(0, headergroup.getHeaders("name1").length);
124         Assert.assertFalse(headergroup.containsHeader("name1"));
125         Assert.assertEquals(1, headergroup.getHeaders("name2").length);
126         Assert.assertTrue(headergroup.containsHeader("name2"));
127         Assert.assertEquals(1, headergroup.getHeaders("name3").length);
128         Assert.assertTrue(headergroup.containsHeader("name3"));
129         headergroup.setHeaders(null);
130         Assert.assertEquals(0, headergroup.getAllHeaders().length);
131     }
132 
133     @Test
134     public void testFirstLastHeaders() {
135         final HeaderGroup headergroup = new HeaderGroup();
136         final Header header1 = new BasicHeader("name", "value1");
137         final Header header2 = new BasicHeader("name", "value2");
138         final Header header3 = new BasicHeader("name", "value3");
139         headergroup.setHeaders(new Header[] { header1, header2, header3 });
140 
141         Assert.assertNull(headergroup.getFirstHeader("whatever"));
142         Assert.assertNull(headergroup.getLastHeader("whatever"));
143 
144         Assert.assertEquals("value1", headergroup.getFirstHeader("name").getValue());
145         Assert.assertEquals("value3", headergroup.getLastHeader("name").getValue());
146     }
147 
148     @Test
149     public void testCondensedHeader() {
150         final HeaderGroup headergroup = new HeaderGroup();
151         Assert.assertNull(headergroup.getCondensedHeader("name"));
152 
153         final Header header1 = new BasicHeader("name", "value1");
154         final Header header2 = new BasicHeader("name", "value2");
155         final Header header3 = new BasicHeader("name", "value3");
156         headergroup.setHeaders(new Header[] { header1, header2, header3 });
157 
158         Assert.assertEquals("value1, value2, value3", headergroup.getCondensedHeader("name").getValue());
159 
160         headergroup.setHeaders(new Header[] { header1 });
161         Assert.assertEquals(header1, headergroup.getCondensedHeader("name"));
162     }
163 
164     @Test
165     public void testIterator() {
166         final HeaderGroup headergroup = new HeaderGroup();
167         final Iterator<Header> i = headergroup.headerIterator();
168         Assert.assertNotNull(i);
169         Assert.assertFalse(i.hasNext());
170     }
171 
172     @Test
173     public void testHeaderRemove() {
174         final HeaderGroup headergroup = new HeaderGroup();
175         final Header header1 = new BasicHeader("name", "value1");
176         final Header header2 = new BasicHeader("name", "value2");
177         final Header header3 = new BasicHeader("name", "value3");
178         headergroup.setHeaders(new Header[] { header1, header2, header3 });
179         final Iterator<Header> i = headergroup.headerIterator();
180         Assert.assertNotNull(i);
181         Assert.assertTrue(i.hasNext());
182         i.next();
183         Assert.assertTrue(i.hasNext());
184         i.next();
185         i.remove();
186         Assert.assertEquals(2, headergroup.getAllHeaders().length);
187         Assert.assertTrue(i.hasNext());
188         i.next();
189         i.remove();
190         Assert.assertEquals(1, headergroup.getAllHeaders().length);
191         Assert.assertFalse(i.hasNext());
192     }
193 
194     @Test
195     public void testSerialization() throws Exception {
196         final HeaderGroup orig = new HeaderGroup();
197         final Header header1 = new BasicHeader("name", "value1");
198         final Header header2 = new BasicHeader("name", "value2");
199         final Header header3 = new BasicHeader("name", "value3");
200         orig.setHeaders(new Header[] { header1, header2, header3 });
201         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
202         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
203         outStream.writeObject(orig);
204         outStream.close();
205         final byte[] raw = outbuffer.toByteArray();
206         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
207         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
208         final HeaderGroup clone = (HeaderGroup) inStream.readObject();
209         final Header[] headers1 = orig.getAllHeaders();
210         final Header[] headers2 = clone.getAllHeaders();
211         Assert.assertNotNull(headers1);
212         Assert.assertNotNull(headers2);
213         Assert.assertEquals(headers1.length, headers2.length);
214         for (int i = 0; i < headers1.length; i++) {
215             Assert.assertEquals(headers1[i].getName(), headers2[i].getName());
216             Assert.assertEquals(headers1[i].getValue(), headers2[i].getValue());
217         }
218     }
219 
220 }