001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java $
003: * $Revision: 589374 $
004: * $Date: 2007-10-28 17:25:07 +0100 (Sun, 28 Oct 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with 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,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.message;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: import org.apache.http.HttpVersion;
038: import org.apache.http.ParseException;
039: import org.apache.http.RequestLine;
040: import org.apache.http.StatusLine;
041: import org.apache.http.util.CharArrayBuffer;
042:
043: /**
044: * Tests for {@link BasicLineParser}.
045: *
046: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
047: *
048: * @version $Revision: 589374 $
049: */
050: public class TestBasicLineParser extends TestCase {
051:
052: // ------------------------------------------------------------ Constructor
053: public TestBasicLineParser(String testName) {
054: super (testName);
055: }
056:
057: // ------------------------------------------------------------------- Main
058: public static void main(String args[]) {
059: String[] testCaseName = { TestBasicLineParser.class.getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: // ------------------------------------------------------- TestCase Methods
064:
065: public static Test suite() {
066: return new TestSuite(TestBasicLineParser.class);
067: }
068:
069: public void testRLParseSuccess() throws Exception {
070: //typical request line
071: RequestLine requestline = BasicLineParser.parseRequestLine(
072: "GET /stuff HTTP/1.1", null);
073: assertEquals("GET /stuff HTTP/1.1", requestline.toString());
074: assertEquals("GET", requestline.getMethod());
075: assertEquals("/stuff", requestline.getUri());
076: assertEquals(HttpVersion.HTTP_1_1, requestline
077: .getProtocolVersion());
078:
079: //Lots of blanks
080: requestline = BasicLineParser.parseRequestLine(
081: " GET /stuff HTTP/1.1 ", null);
082: assertEquals("GET /stuff HTTP/1.1", requestline.toString());
083: assertEquals("GET", requestline.getMethod());
084: assertEquals("/stuff", requestline.getUri());
085: assertEquals(HttpVersion.HTTP_1_1, requestline
086: .getProtocolVersion());
087:
088: //this is not strictly valid, but is lenient
089: requestline = BasicLineParser.parseRequestLine(
090: "\rGET /stuff HTTP/1.1", null);
091: assertEquals("GET", requestline.getMethod());
092: assertEquals("/stuff", requestline.getUri());
093: assertEquals(HttpVersion.HTTP_1_1, requestline
094: .getProtocolVersion());
095: }
096:
097: public void testRLParseFailure() throws Exception {
098: try {
099: BasicLineParser.parseRequestLine(" ", null);
100: fail();
101: } catch (ParseException e) {
102: // expected
103: }
104:
105: try {
106: BasicLineParser.parseRequestLine(" GET", null);
107: fail();
108: } catch (ParseException e) {
109: // expected
110: }
111:
112: try {
113: BasicLineParser.parseRequestLine("GET /stuff", null);
114: fail();
115: } catch (ParseException e) {
116: // expected
117: }
118:
119: try {
120: BasicLineParser
121: .parseRequestLine("GET/stuff HTTP/1.1", null);
122: fail();
123: } catch (ParseException e) {
124: // expected
125: }
126:
127: try {
128: BasicLineParser.parseRequestLine(
129: "GET /stuff HTTP/1.1 Oooooooooooppsie", null);
130: fail();
131: } catch (ParseException e) {
132: // expected
133: }
134: }
135:
136: public void testSLParseSuccess() throws Exception {
137: //typical status line
138: StatusLine statusLine = BasicLineParser.parseStatusLine(
139: "HTTP/1.1 200 OK", null);
140: assertEquals("HTTP/1.1 200 OK", statusLine.toString());
141: assertEquals(HttpVersion.HTTP_1_1, statusLine
142: .getProtocolVersion());
143: assertEquals(200, statusLine.getStatusCode());
144: assertEquals("OK", statusLine.getReasonPhrase());
145:
146: //status line with multi word reason phrase
147: statusLine = BasicLineParser.parseStatusLine(
148: "HTTP/1.1 404 Not Found", null);
149: assertEquals(404, statusLine.getStatusCode());
150: assertEquals("Not Found", statusLine.getReasonPhrase());
151:
152: //reason phrase can be anyting
153: statusLine = BasicLineParser.parseStatusLine(
154: "HTTP/1.1 404 Non Trouve", null);
155: assertEquals("Non Trouve", statusLine.getReasonPhrase());
156:
157: //its ok to end with a \n\r
158: statusLine = BasicLineParser.parseStatusLine(
159: "HTTP/1.1 404 Not Found\r\n", null);
160: assertEquals("Not Found", statusLine.getReasonPhrase());
161:
162: //this is valid according to the Status-Line BNF
163: statusLine = BasicLineParser.parseStatusLine("HTTP/1.1 200 ",
164: null);
165: assertEquals(200, statusLine.getStatusCode());
166: assertEquals("", statusLine.getReasonPhrase());
167:
168: //this is not strictly valid, but is lenient
169: statusLine = BasicLineParser.parseStatusLine("HTTP/1.1 200",
170: null);
171: assertEquals(200, statusLine.getStatusCode());
172: assertEquals("", statusLine.getReasonPhrase());
173:
174: //this is not strictly valid, but is lenient
175: statusLine = BasicLineParser.parseStatusLine(
176: "HTTP/1.1 200 OK", null);
177: assertEquals(200, statusLine.getStatusCode());
178: assertEquals("OK", statusLine.getReasonPhrase());
179:
180: //this is not strictly valid, but is lenient
181: statusLine = BasicLineParser.parseStatusLine(
182: "\rHTTP/1.1 200 OK", null);
183: assertEquals(200, statusLine.getStatusCode());
184: assertEquals("OK", statusLine.getReasonPhrase());
185: assertEquals(HttpVersion.HTTP_1_1, statusLine
186: .getProtocolVersion());
187:
188: //this is not strictly valid, but is lenient
189: statusLine = BasicLineParser.parseStatusLine(
190: " HTTP/1.1 200 OK", null);
191: assertEquals(200, statusLine.getStatusCode());
192: assertEquals("OK", statusLine.getReasonPhrase());
193: assertEquals(HttpVersion.HTTP_1_1, statusLine
194: .getProtocolVersion());
195: }
196:
197: public void testSLParseFailure() throws Exception {
198: try {
199: BasicLineParser.parseStatusLine("xxx 200 OK", null);
200: fail();
201: } catch (ParseException e) {
202: // expected
203: }
204:
205: try {
206: BasicLineParser.parseStatusLine("HTTP/1.1 xxx OK", null);
207: fail();
208: } catch (ParseException e) {
209: // expected
210: }
211:
212: try {
213: BasicLineParser.parseStatusLine("HTTP/1.1 ", null);
214: fail();
215: } catch (ParseException e) {
216: // expected
217: }
218: try {
219: BasicLineParser.parseStatusLine("HTTP/1.1", null);
220: fail();
221: } catch (ParseException e) {
222: // expected
223: }
224: }
225:
226: public void testHttpVersionParsing() throws Exception {
227:
228: String s = "HTTP/1.1";
229: HttpVersion version = (HttpVersion) BasicLineParser
230: .parseProtocolVersion(s, null);
231: assertEquals("HTTP protocol name", "HTTP", version
232: .getProtocol());
233: assertEquals("HTTP major version number", 1, version.getMajor());
234: assertEquals("HTTP minor version number", 1, version.getMinor());
235: assertEquals("HTTP version number", s, version.toString());
236:
237: s = "HTTP/123.4567";
238: version = (HttpVersion) BasicLineParser.parseProtocolVersion(s,
239: null);
240: assertEquals("HTTP protocol name", "HTTP", version
241: .getProtocol());
242: assertEquals("HTTP major version number", 123, version
243: .getMajor());
244: assertEquals("HTTP minor version number", 4567, version
245: .getMinor());
246: assertEquals("HTTP version number", s, version.toString());
247: }
248:
249: public void testHttpVersionParsingUsingCursor() throws Exception {
250:
251: String s = "HTTP/1.1";
252: CharArrayBuffer buffer = new CharArrayBuffer(16);
253: buffer.append(s);
254: ParserCursor cursor = new ParserCursor(0, s.length());
255:
256: LineParser parser = BasicLineParser.DEFAULT;
257:
258: HttpVersion version = (HttpVersion) parser
259: .parseProtocolVersion(buffer, cursor);
260: assertEquals("HTTP protocol name", "HTTP", version
261: .getProtocol());
262: assertEquals("HTTP major version number", 1, version.getMajor());
263: assertEquals("HTTP minor version number", 1, version.getMinor());
264: assertEquals("HTTP version number", "HTTP/1.1", version
265: .toString());
266: assertEquals(s.length(), cursor.getPos());
267: assertTrue(cursor.atEnd());
268:
269: s = "HTTP/1.123 123";
270: buffer = new CharArrayBuffer(16);
271: buffer.append(s);
272: cursor = new ParserCursor(0, s.length());
273:
274: version = (HttpVersion) parser.parseProtocolVersion(buffer,
275: cursor);
276: assertEquals("HTTP protocol name", "HTTP", version
277: .getProtocol());
278: assertEquals("HTTP major version number", 1, version.getMajor());
279: assertEquals("HTTP minor version number", 123, version
280: .getMinor());
281: assertEquals("HTTP version number", "HTTP/1.123", version
282: .toString());
283: assertEquals(' ', buffer.charAt(cursor.getPos()));
284: assertEquals(s.length() - 4, cursor.getPos());
285: assertFalse(cursor.atEnd());
286: }
287:
288: public void testInvalidHttpVersionParsing() throws Exception {
289: try {
290: BasicLineParser.parseProtocolVersion((String) null, null);
291: fail("IllegalArgumentException should have been thrown");
292: } catch (IllegalArgumentException e) {
293: //expected
294: }
295: try {
296: BasicLineParser.parseProtocolVersion(" ", null);
297: fail("ParseException should have been thrown");
298: } catch (ParseException e) {
299: //expected
300: }
301: try {
302: BasicLineParser.parseProtocolVersion("HTT", null);
303: fail("ParseException should have been thrown");
304: } catch (ParseException e) {
305: //expected
306: }
307: try {
308: BasicLineParser.parseProtocolVersion("crap", null);
309: fail("ParseException should have been thrown");
310: } catch (ParseException e) {
311: //expected
312: }
313: try {
314: BasicLineParser.parseProtocolVersion("HTTP/crap", null);
315: fail("ParseException should have been thrown");
316: } catch (ParseException e) {
317: //expected
318: }
319: try {
320: BasicLineParser.parseProtocolVersion("HTTP/1", null);
321: fail("ParseException should have been thrown");
322: } catch (ParseException e) {
323: //expected
324: }
325: try {
326: BasicLineParser.parseProtocolVersion("HTTP/1234 ", null);
327: fail("ParseException should have been thrown");
328: } catch (ParseException e) {
329: //expected
330: }
331: try {
332: BasicLineParser.parseProtocolVersion("HTTP/1.", null);
333: fail("ParseException should have been thrown");
334: } catch (ParseException e) {
335: //expected
336: }
337: try {
338: BasicLineParser.parseProtocolVersion(
339: "HTTP/whatever.whatever whatever", null);
340: fail("ParseException should have been thrown");
341: } catch (ParseException e) {
342: //expected
343: }
344: try {
345: BasicLineParser.parseProtocolVersion(
346: "HTTP/1.whatever whatever", null);
347: fail("ParseException should have been thrown");
348: } catch (ParseException e) {
349: //expected
350: }
351: }
352:
353: }
|