1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestHeaders.java,v 1.5.2.1 2004/02/22 18:21:16 olegk Exp $
3    * $Revision: 1.5.2.1 $
4    * $Date: 2004/02/22 18:21:16 $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  /***
38   * Tests for reading response headers.
39   *
40   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
41   * @version $Id: TestRequestHeaders.java,v 1.5.2.1 2004/02/22 18:21:16 olegk Exp $
42   */
43  public class TestRequestHeaders extends TestCase {
44  
45         HttpState state = null;
46         SimpleHttpMethod method = null;
47         HttpConnection conn = null;
48  
49      // ------------------------------------------------------------ Constructor
50      public TestRequestHeaders(String testName) {
51          super(testName);
52      }
53  
54      // ------------------------------------------------------------------- Main
55      public static void main(String args[]) {
56          String[] testCaseName = {TestRequestHeaders.class.getName()};
57          junit.textui.TestRunner.main(testCaseName);
58      }
59  
60      // ------------------------------------------------------- TestCase Methods
61      public static Test suite() {
62          return new TestSuite(TestRequestHeaders.class);
63      }
64  
65      public void setUp() {
66         state = new HttpState();
67         method = new SimpleHttpMethod("/some/absolute/path/");
68         //assign conn in test case
69      }
70  
71      public void tearDown() {
72         state = null;
73         method = null;
74         conn = null;
75      }
76  
77      public void testNullHeader() throws Exception {
78         conn = new SimpleHttpConnection("some.host.name", 80, "http");
79         assertEquals(null, method.getRequestHeader(null));
80         assertEquals(null, method.getRequestHeader("bogus"));
81      }
82  
83      public void testHostHeaderPortHTTP80() throws Exception {
84         conn = new SimpleHttpConnection("some.host.name", 80, "http");
85         method.addRequestHeaders(state, conn);
86         assertEquals("Host: some.host.name", method.getRequestHeader("Host").toString().trim());
87      }
88  
89      public void testHostHeaderPortHTTP81() throws Exception {
90         conn = new SimpleHttpConnection("some.host.name", 81, "http");
91         method.addRequestHeaders(state, conn);
92         assertEquals("Host: some.host.name:81", method.getRequestHeader("Host").toString().trim());
93      }
94  
95      public void testHostHeaderPortHTTPS443() throws Exception {
96         conn = new SimpleHttpConnection("some.host.name", 443, "https");
97         method.addRequestHeaders(state, conn);
98         assertEquals("Host: some.host.name", method.getRequestHeader("Host").toString().trim());
99      }
100 
101     public void testHostHeaderPortHTTPS444() throws Exception {
102        conn = new SimpleHttpConnection("some.host.name", 444, "https");
103        method.addRequestHeaders(state, conn);
104        assertEquals("Host: some.host.name:444", method.getRequestHeader("Host").toString().trim());
105     }
106 
107     public void testHeadersPreserveCaseKeyIgnoresCase() throws Exception {
108         method.addRequestHeader(new Header("NAME", "VALUE"));
109         Header upHeader =  method.getRequestHeader("NAME");
110         Header loHeader =  method.getRequestHeader("name");
111         Header mixHeader =  method.getRequestHeader("nAmE");
112         assertEquals("NAME", upHeader.getName());
113         assertEquals("VALUE", upHeader.getValue());
114         assertEquals("NAME", loHeader.getName());
115         assertEquals("VALUE", loHeader.getValue());
116         assertEquals("NAME", mixHeader.getName());
117         assertEquals("VALUE", mixHeader.getValue());
118     }
119 }