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