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.http.message;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.apache.http.Header;
36  import org.apache.http.HeaderElement;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Unit tests for {@link Header}.
42   */
43  public class TestHeader {
44  
45      @Test
46      public void testBasicConstructor() {
47          final Header header = new BasicHeader("name", "value");
48          Assert.assertEquals("name", header.getName());
49          Assert.assertEquals("value", header.getValue());
50      }
51  
52      @Test
53      public void testBasicConstructorNullValue() {
54          final Header header = new BasicHeader("name", null);
55          Assert.assertEquals("name", header.getName());
56          Assert.assertEquals(null, header.getValue());
57      }
58  
59      @Test
60      public void testInvalidName() {
61          try {
62              new BasicHeader(null, null);
63              Assert.fail("IllegalArgumentException should have been thrown");
64          } catch (final IllegalArgumentException ex) {
65              //expected
66          }
67      }
68  
69      @Test
70      public void testToString() {
71          final Header header1 = new BasicHeader("name1", "value1");
72          Assert.assertEquals("name1: value1", header1.toString());
73          final Header header2 = new BasicHeader("name2", null);
74          Assert.assertEquals("name2: ", header2.toString());
75      }
76  
77      @Test
78      public void testHeaderElements() {
79          Header header = new BasicHeader("name", "element1 = value1, element2; param1 = value1, element3");
80          HeaderElement[] elements = header.getElements();
81          Assert.assertNotNull(elements);
82          Assert.assertEquals(3, elements.length);
83          Assert.assertEquals("element1", elements[0].getName());
84          Assert.assertEquals("value1", elements[0].getValue());
85          Assert.assertEquals("element2", elements[1].getName());
86          Assert.assertEquals(null, elements[1].getValue());
87          Assert.assertEquals("element3", elements[2].getName());
88          Assert.assertEquals(null, elements[2].getValue());
89          Assert.assertEquals(1, elements[1].getParameters().length);
90  
91          header = new BasicHeader("name", null);
92          elements = header.getElements();
93          Assert.assertNotNull(elements);
94          Assert.assertEquals(0, elements.length);
95      }
96  
97      @Test
98      public void testCloning() throws Exception {
99          final BasicHeader orig = new BasicHeader("name1", "value1");
100         final BasicHeader clone = (BasicHeader) orig.clone();
101         Assert.assertEquals(orig.getName(), clone.getName());
102         Assert.assertEquals(orig.getValue(), clone.getValue());
103     }
104 
105     @Test
106     public void testSerialization() throws Exception {
107         final BasicHeader orig = new BasicHeader("name1", "value1");
108         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
109         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
110         outstream.writeObject(orig);
111         outstream.close();
112         final byte[] raw = outbuffer.toByteArray();
113         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
114         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
115         final BasicHeader clone = (BasicHeader) instream.readObject();
116         Assert.assertEquals(orig.getName(), clone.getName());
117         Assert.assertEquals(orig.getValue(), clone.getValue());
118     }
119 
120 }
121