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 org.apache.http.Header;
30  import org.apache.http.HttpMessage;
31  import org.apache.http.HttpVersion;
32  import org.apache.http.ProtocolVersion;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Unit tests for {@link AbstractHttpMessage}.
38   *
39   */
40  public class TestAbstractMessage {
41  
42      static class TestHttpMessage extends AbstractHttpMessage {
43  
44          private final ProtocolVersion ver;
45  
46          public TestHttpMessage(final ProtocolVersion ver) {
47              super();
48              this.ver = ver != null ? ver : HttpVersion.HTTP_1_1;
49          }
50  
51          public TestHttpMessage() {
52              this(HttpVersion.HTTP_1_1);
53          }
54  
55          @Override
56          public ProtocolVersion getProtocolVersion() {
57              return ver;
58          }
59  
60      }
61  
62      @Test
63      public void testBasicProperties() {
64          final HttpMessage message = new TestHttpMessage();
65          Assert.assertNotNull(message.headerIterator());
66          final Header[] headers = message.getAllHeaders();
67          Assert.assertNotNull(headers);
68          Assert.assertEquals(0, headers.length);
69      }
70  
71      @Test
72      public void testBasicHeaderOps() {
73          final HttpMessage message = new TestHttpMessage();
74          Assert.assertFalse(message.containsHeader("whatever"));
75  
76          message.addHeader("name", "1");
77          message.addHeader("name", "2");
78  
79          Header[] headers = message.getAllHeaders();
80          Assert.assertNotNull(headers);
81          Assert.assertEquals(2, headers.length);
82  
83          Header h = message.getFirstHeader("name");
84          Assert.assertNotNull(h);
85          Assert.assertEquals("1", h.getValue());
86  
87          message.setHeader("name", "3");
88          h = message.getFirstHeader("name");
89          Assert.assertNotNull(h);
90          Assert.assertEquals("3", h.getValue());
91          h = message.getLastHeader("name");
92          Assert.assertNotNull(h);
93          Assert.assertEquals("2", h.getValue());
94  
95          // Should have no effect
96          message.addHeader(null);
97          message.setHeader(null);
98  
99          headers = message.getHeaders("name");
100         Assert.assertNotNull(headers);
101         Assert.assertEquals(2, headers.length);
102         Assert.assertEquals("3", headers[0].getValue());
103         Assert.assertEquals("2", headers[1].getValue());
104 
105         message.addHeader("name", "4");
106 
107         headers[1] = new BasicHeader("name", "5");
108         message.setHeaders(headers);
109 
110         headers = message.getHeaders("name");
111         Assert.assertNotNull(headers);
112         Assert.assertEquals(2, headers.length);
113         Assert.assertEquals("3", headers[0].getValue());
114         Assert.assertEquals("5", headers[1].getValue());
115 
116         message.setHeader("whatever", null);
117         message.removeHeaders("name");
118         message.removeHeaders(null);
119         headers = message.getAllHeaders();
120         Assert.assertNotNull(headers);
121         Assert.assertEquals(1, headers.length);
122         Assert.assertEquals(null, headers[0].getValue());
123 
124         message.removeHeader(message.getFirstHeader("whatever"));
125         headers = message.getAllHeaders();
126         Assert.assertNotNull(headers);
127         Assert.assertEquals(0, headers.length);
128     }
129 
130     @Test
131     public void testInvalidInput() {
132         final HttpMessage message = new TestHttpMessage();
133         try {
134             message.addHeader(null, null);
135             Assert.fail("IllegalArgumentException should have been thrown");
136         } catch (final IllegalArgumentException ex) {
137             // expected
138         }
139         try {
140             message.setHeader(null, null);
141             Assert.fail("IllegalArgumentException should have been thrown");
142         } catch (final IllegalArgumentException ex) {
143             // expected
144         }
145     }
146 
147 }
148