1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestRequestHeaders.java,v 1.8 2004/10/31 14:04:13 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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 org.apache.commons.httpclient.protocol.Protocol;
34  
35  import junit.framework.Test;
36  import junit.framework.TestCase;
37  import junit.framework.TestSuite;
38  
39  /***
40   * Tests for reading response headers.
41   *
42   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
43   * @version $Id: TestRequestHeaders.java 155418 2005-02-26 13:01:52Z dirkv $
44   */
45  public class TestRequestHeaders extends TestCase {
46  
47      // ------------------------------------------------------------ Constructor
48      public TestRequestHeaders(String testName) {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------------------- Main
53      public static void main(String args[]) {
54          String[] testCaseName = {TestRequestHeaders.class.getName()};
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------- TestCase Methods
59      public static Test suite() {
60          return new TestSuite(TestRequestHeaders.class);
61      }
62  
63      public void testNullHeader() throws Exception {
64          FakeHttpMethod method = new FakeHttpMethod();
65          assertEquals(null, method.getRequestHeader(null));
66          assertEquals(null, method.getRequestHeader("bogus"));
67      }
68  
69      public void testHostHeaderPortHTTP80() throws Exception {
70          HttpConnection conn = new HttpConnection("some.host.name", 80);
71          HttpState state = new HttpState();
72          FakeHttpMethod method = new FakeHttpMethod();
73          method.addRequestHeaders(state, conn);
74          assertEquals("Host: some.host.name", method.getRequestHeader("Host").toString().trim());
75      }
76  
77      public void testHostHeaderPortHTTP81() throws Exception {
78          HttpConnection conn = new HttpConnection("some.host.name", 81);
79          HttpState state = new HttpState();
80          FakeHttpMethod method = new FakeHttpMethod();
81          method.addRequestHeaders(state, conn);
82          assertEquals("Host: some.host.name:81", method.getRequestHeader("Host").toString().trim());
83      }
84  
85      public void testHostHeaderPortHTTPS443() throws Exception {
86          HttpConnection conn = new HttpConnection("some.host.name", 443, 
87                  Protocol.getProtocol("https"));
88          HttpState state = new HttpState();
89          FakeHttpMethod method = new FakeHttpMethod();
90          method.addRequestHeaders(state, conn);
91          assertEquals("Host: some.host.name", method.getRequestHeader("Host").toString().trim());
92      }
93  
94      public void testHostHeaderPortHTTPS444() throws Exception {
95          HttpConnection conn = new HttpConnection("some.host.name", 444, 
96                  Protocol.getProtocol("https"));
97          HttpState state = new HttpState();
98          FakeHttpMethod method = new FakeHttpMethod();
99          method.addRequestHeaders(state, conn);
100         assertEquals("Host: some.host.name:444", method.getRequestHeader("Host").toString().trim());
101     }
102 
103     public void testHeadersPreserveCaseKeyIgnoresCase() throws Exception {
104         FakeHttpMethod method = new FakeHttpMethod();
105         method.addRequestHeader(new Header("NAME", "VALUE"));
106         Header upHeader =  method.getRequestHeader("NAME");
107         Header loHeader =  method.getRequestHeader("name");
108         Header mixHeader =  method.getRequestHeader("nAmE");
109         assertEquals("NAME", upHeader.getName());
110         assertEquals("VALUE", upHeader.getValue());
111         assertEquals("NAME", loHeader.getName());
112         assertEquals("VALUE", loHeader.getValue());
113         assertEquals("NAME", mixHeader.getName());
114         assertEquals("VALUE", mixHeader.getValue());
115     }
116 }