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 $
4
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
5
* ====================================================================
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
* ====================================================================
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/>.
28
* [Additional notices, if required by prior licensing conditions]
32
package org.apache.commons.httpclient;
34
import java.io.ByteArrayInputStream;
35
import java.io.InputStream;
37
import junit.framework.*;
40
* Simple tests for {@link HttpParser}.
42
* @author Oleg Kalnichevski
43
* @version $Id: TestHttpParser.java 480424 2006-11-29 05:56:49Z bayard $
45
public class TestHttpParser extends TestCase {
47
private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
49
// ------------------------------------------------------------ Constructor
50
public TestHttpParser(String testName) {
54
// ------------------------------------------------------------------- Main
55
public static void main(String args[]) {
56
String[] testCaseName = { TestHeader.class.getName() };
57
junit.textui.TestRunner.main(testCaseName);
60
// ------------------------------------------------------- TestCase Methods
62
public static Test suite() {
63
return new TestSuite(TestHttpParser.class);
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));
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));
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());
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));
97
Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET);
98
fail("HttpException should have been thrown");
99
} catch (HttpException expected) {
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());
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());