001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.ByteArrayInputStream;
035: import java.io.InputStream;
036:
037: import junit.framework.*;
038:
039: /**
040: * Simple tests for {@link HttpParser}.
041: *
042: * @author Oleg Kalnichevski
043: * @version $Id: TestHttpParser.java 480424 2006-11-29 05:56:49Z bayard $
044: */
045: public class TestHttpParser extends TestCase {
046:
047: private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
048:
049: // ------------------------------------------------------------ Constructor
050: public TestHttpParser(String testName) {
051: super (testName);
052: }
053:
054: // ------------------------------------------------------------------- Main
055: public static void main(String args[]) {
056: String[] testCaseName = { TestHeader.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: // ------------------------------------------------------- TestCase Methods
061:
062: public static Test suite() {
063: return new TestSuite(TestHttpParser.class);
064: }
065:
066: public void testReadHttpLine() throws Exception {
067: InputStream instream = new ByteArrayInputStream(
068: "\r\r\nstuff\r\n".getBytes(HTTP_ELEMENT_CHARSET));
069: assertEquals("\r", HttpParser.readLine(instream,
070: HTTP_ELEMENT_CHARSET));
071: assertEquals("stuff", HttpParser.readLine(instream,
072: HTTP_ELEMENT_CHARSET));
073: assertEquals(null, HttpParser.readLine(instream,
074: HTTP_ELEMENT_CHARSET));
075:
076: instream = new ByteArrayInputStream("\n\r\nstuff\r\n"
077: .getBytes("US-ASCII"));
078: assertEquals("", HttpParser.readLine(instream,
079: HTTP_ELEMENT_CHARSET));
080: assertEquals("", HttpParser.readLine(instream,
081: HTTP_ELEMENT_CHARSET));
082: assertEquals("stuff", HttpParser.readLine(instream,
083: HTTP_ELEMENT_CHARSET));
084: assertEquals(null, HttpParser.readLine(instream,
085: HTTP_ELEMENT_CHARSET));
086: }
087:
088: public void testReadWellFormedHttpHeaders() throws Exception {
089: InputStream instream = new ByteArrayInputStream(
090: "a: a\r\nb: b\r\n\r\nwhatever"
091: .getBytes(HTTP_ELEMENT_CHARSET));
092: Header[] headers = HttpParser.parseHeaders(instream,
093: HTTP_ELEMENT_CHARSET);
094: assertNotNull(headers);
095: assertEquals(2, headers.length);
096: assertEquals("a", headers[0].getName());
097: assertEquals("a", headers[0].getValue());
098: assertEquals("b", headers[1].getName());
099: assertEquals("b", headers[1].getValue());
100: }
101:
102: public void testReadMalformedHttpHeaders() throws Exception {
103: InputStream instream = new ByteArrayInputStream(
104: "a: a\r\nb b\r\n\r\nwhatever"
105: .getBytes(HTTP_ELEMENT_CHARSET));
106: try {
107: Header[] headers = HttpParser.parseHeaders(instream,
108: HTTP_ELEMENT_CHARSET);
109: fail("HttpException should have been thrown");
110: } catch (HttpException expected) {
111: }
112: }
113:
114: public void testHeadersTerminatorLeniency1() throws Exception {
115: InputStream instream = new ByteArrayInputStream(
116: "a: a\r\nb: b\r\n\r\r\nwhatever"
117: .getBytes(HTTP_ELEMENT_CHARSET));
118: Header[] headers = HttpParser.parseHeaders(instream,
119: HTTP_ELEMENT_CHARSET);
120: assertNotNull(headers);
121: assertEquals(2, headers.length);
122: assertEquals("a", headers[0].getName());
123: assertEquals("a", headers[0].getValue());
124: assertEquals("b", headers[1].getName());
125: assertEquals("b", headers[1].getValue());
126: }
127:
128: public void testHeadersTerminatorLeniency2() throws Exception {
129: InputStream instream = new ByteArrayInputStream(
130: "a: a\r\nb: b\r\n \r\nwhatever"
131: .getBytes(HTTP_ELEMENT_CHARSET));
132: Header[] headers = HttpParser.parseHeaders(instream,
133: HTTP_ELEMENT_CHARSET);
134: assertNotNull(headers);
135: assertEquals(2, headers.length);
136: assertEquals("a", headers[0].getName());
137: assertEquals("a", headers[0].getValue());
138: assertEquals("b", headers[1].getName());
139: assertEquals("b", headers[1].getValue());
140: }
141: }
|