001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/io/TestMessageParser.java $
003: * $Revision: 591798 $
004: * $Date: 2007-11-04 17:19:29 +0100 (Sun, 04 Nov 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.io;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.Header;
037: import org.apache.http.HeaderElement;
038: import org.apache.http.NameValuePair;
039: import org.apache.http.ProtocolException;
040: import org.apache.http.io.SessionInputBuffer;
041: import org.apache.http.message.BufferedHeader;
042: import org.apache.http.mockup.SessionInputBufferMockup;
043:
044: import junit.framework.Test;
045: import junit.framework.TestCase;
046: import junit.framework.TestSuite;
047:
048: /**
049: * Unit tests for {@link Header}.
050: *
051: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
052: */
053: public class TestMessageParser extends TestCase {
054:
055: public TestMessageParser(String testName) {
056: super (testName);
057: }
058:
059: public static void main(String args[]) {
060: String[] testCaseName = { TestMessageParser.class.getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: public static Test suite() {
065: return new TestSuite(TestMessageParser.class);
066: }
067:
068: public void testInvalidInput() throws Exception {
069: try {
070: // the first argument must not be null
071: AbstractMessageParser.parseHeaders(null, -1, -1, null);
072: fail("IllegalArgumentException should have been thrown");
073: } catch (IllegalArgumentException ex) {
074: // expected
075: }
076: try {
077: new BufferedHeader(null);
078: fail("IllegalArgumentException should have been thrown");
079: } catch (IllegalArgumentException ex) {
080: // expected
081: }
082: }
083:
084: public void testBasicHeaderParsing() throws Exception {
085: String s = "header1: stuff\r\n" + "header2 : stuff \r\n"
086: + "header3: stuff\r\n" + " and more stuff\r\n"
087: + "\t and even more stuff\r\n" + " \r\n" + "\r\n";
088: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
089: "US-ASCII");
090: Header[] headers = AbstractMessageParser.parseHeaders(receiver,
091: -1, -1, null);
092: assertNotNull(headers);
093: assertEquals(3, headers.length);
094: assertEquals("header1", headers[0].getName());
095: assertEquals("stuff", headers[0].getValue());
096: assertEquals("header2", headers[1].getName());
097: assertEquals("stuff", headers[1].getValue());
098: assertEquals("header3", headers[2].getName());
099: assertEquals("stuff and more stuff and even more stuff",
100: headers[2].getValue());
101:
102: Header h = headers[0];
103:
104: assertTrue(h instanceof BufferedHeader);
105: assertNotNull(((BufferedHeader) h).getBuffer());
106: assertEquals("header1: stuff", ((BufferedHeader) h).toString());
107: assertEquals(8, ((BufferedHeader) h).getValuePos());
108: }
109:
110: public void testBufferedHeader() throws Exception {
111: String s = "header1 : stuff; param1 = value1; param2 = \"value 2\" \r\n"
112: + "\r\n";
113: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
114: "US-ASCII");
115: Header[] headers = AbstractMessageParser.parseHeaders(receiver,
116: -1, -1, null);
117: assertNotNull(headers);
118: assertEquals(1, headers.length);
119: assertEquals(
120: "header1 : stuff; param1 = value1; param2 = \"value 2\" ",
121: headers[0].toString());
122: HeaderElement[] elements = headers[0].getElements();
123: assertNotNull(elements);
124: assertEquals(1, elements.length);
125: assertEquals("stuff", elements[0].getName());
126: assertEquals(null, elements[0].getValue());
127: NameValuePair[] params = elements[0].getParameters();
128: assertNotNull(params);
129: assertEquals(2, params.length);
130: assertEquals("param1", params[0].getName());
131: assertEquals("value1", params[0].getValue());
132: assertEquals("param2", params[1].getName());
133: assertEquals("value 2", params[1].getValue());
134: }
135:
136: public void testParsingInvalidHeaders() throws Exception {
137: String s = " stuff\r\n" + "header1: stuff\r\n" + "\r\n";
138: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
139: "US-ASCII");
140: try {
141: AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
142: fail("ProtocolException should have been thrown");
143: } catch (ProtocolException ex) {
144: // expected
145: }
146: s = " : stuff\r\n" + "header1: stuff\r\n" + "\r\n";
147: receiver = new SessionInputBufferMockup(s, "US-ASCII");
148: try {
149: AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
150: fail("ProtocolException should have been thrown");
151: } catch (ProtocolException ex) {
152: // expected
153: }
154: }
155:
156: public void testParsingMalformedFirstHeader() throws Exception {
157: String s = " header1: stuff\r\n" + "header2 : stuff \r\n";
158: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
159: "US-ASCII");
160: Header[] headers = AbstractMessageParser.parseHeaders(receiver,
161: -1, -1, null);
162: assertNotNull(headers);
163: assertEquals(2, headers.length);
164: assertEquals("header1", headers[0].getName());
165: assertEquals("stuff", headers[0].getValue());
166: assertEquals("header2", headers[1].getName());
167: assertEquals("stuff", headers[1].getValue());
168: }
169:
170: public void testEmptyDataStream() throws Exception {
171: String s = "";
172: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
173: "US-ASCII");
174: Header[] headers = AbstractMessageParser.parseHeaders(receiver,
175: -1, -1, null);
176: assertNotNull(headers);
177: assertEquals(0, headers.length);
178: }
179:
180: public void testMaxHeaderCount() throws Exception {
181: String s = "header1: stuff\r\n" + "header2: stuff \r\n"
182: + "header3: stuff\r\n" + "\r\n";
183: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
184: "US-ASCII");
185: try {
186: AbstractMessageParser.parseHeaders(receiver, 2, -1, null);
187: fail("IOException should have been thrown");
188: } catch (IOException ex) {
189: // expected
190: }
191: }
192:
193: public void testMaxHeaderCountForFoldedHeader() throws Exception {
194: String s = "header1: stuff\r\n" + " stuff \r\n" + " stuff\r\n"
195: + "\r\n";
196: SessionInputBuffer receiver = new SessionInputBufferMockup(s,
197: "US-ASCII");
198: try {
199: AbstractMessageParser.parseHeaders(receiver, 2, 15, null);
200: fail("IOException should have been thrown");
201: } catch (IOException ex) {
202: // expected
203: }
204: }
205:
206: }
|