TestHttpParser xref

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpParser.java,v 1.3 2004/02/22 18:08:49 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 java.io.ByteArrayInputStream;
    35  import java.io.InputStream;
    36
    37  import junit.framework.*;
    38
    39  /**
    40   * Simple tests for {@link HttpParser}.
    41   *
    42   * @author Oleg Kalnichevski
    43   * @version $Id: TestHttpParser.java 480424 2006-11-29 05:56:49Z bayard $
    44   */
    45  public class TestHttpParser extends TestCase {
    46
    47      private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
    48
    49      // ------------------------------------------------------------ Constructor
    50      public TestHttpParser(String testName) {
    51          super(testName);
    52      }
    53
    54      // ------------------------------------------------------------------- Main
    55      public static void main(String args[]) {
    56          String[] testCaseName = { TestHeader.class.getName() };
    57          junit.textui.TestRunner.main(testCaseName);
    58      }
    59
    60      // ------------------------------------------------------- TestCase Methods
    61
    62      public static Test suite() {
    63          return new TestSuite(TestHttpParser.class);
    64      }
    65
    66      public void testReadHttpLine() throws Exception {
    67          InputStream instream = new ByteArrayInputStream(
    68              "\r\r\nstuff\r\n".getBytes(HTTP_ELEMENT_CHARSET));
    69          assertEquals("\r", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    70          assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    71          assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    72
    73          instream = new ByteArrayInputStream(
    74              "\n\r\nstuff\r\n".getBytes("US-ASCII"));
    75          assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    76          assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    77          assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    78          assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
    79      }
    80
    81      public void testReadWellFormedHttpHeaders() throws Exception {
    82          InputStream instream = new ByteArrayInputStream(
    83              "a: a\r\nb: b\r\n\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET));
    84          Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET);
    85          assertNotNull(headers);
    86          assertEquals(2, headers.length);
    87          assertEquals("a", headers[0].getName());
    88          assertEquals("a", headers[0].getValue());
    89          assertEquals("b", headers[1].getName());
    90          assertEquals("b", headers[1].getValue());
    91      }
    92
    93      public void testReadMalformedHttpHeaders() throws Exception {
    94          InputStream instream = new ByteArrayInputStream(
    95              "a: a\r\nb b\r\n\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET));
    96          try {
    97              Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET);
    98              fail("HttpException should have been thrown");
    99          } catch (HttpException expected) {
    100         }
    101     }
    102
    103     public void testHeadersTerminatorLeniency1() throws Exception {
    104         InputStream instream = new ByteArrayInputStream(
    105             "a: a\r\nb: b\r\n\r\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET));
    106         Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET);
    107         assertNotNull(headers);
    108         assertEquals(2, headers.length);
    109         assertEquals("a", headers[0].getName());
    110         assertEquals("a", headers[0].getValue());
    111         assertEquals("b", headers[1].getName());
    112         assertEquals("b", headers[1].getValue());
    113     }
    114
    115     public void testHeadersTerminatorLeniency2() throws Exception {
    116         InputStream instream = new ByteArrayInputStream(
    117             "a: a\r\nb: b\r\n    \r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET));
    118         Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET);
    119         assertNotNull(headers);
    120         assertEquals(2, headers.length);
    121         assertEquals("a", headers[0].getName());
    122         assertEquals("a", headers[0].getValue());
    123         assertEquals("b", headers[1].getName());
    124         assertEquals("b", headers[1].getValue());
    125     }
    126 }