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