001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestStatusLine.java,v 1.9 2004/07/19 20:24:21 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 junit.framework.*;
035:
036: /**
037: * Simple tests for {@link StatusLine}.
038: *
039: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
040: * @version $Id: TestStatusLine.java 480424 2006-11-29 05:56:49Z bayard $
041: */
042: public class TestStatusLine extends TestCase {
043:
044: private StatusLine statusLine = null;
045:
046: // ------------------------------------------------------------ Constructor
047: public TestStatusLine(String testName) {
048: super (testName);
049: }
050:
051: // ------------------------------------------------------------------- Main
052: public static void main(String args[]) {
053: String[] testCaseName = { TestStatusLine.class.getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: // ------------------------------------------------------- TestCase Methods
058:
059: public static Test suite() {
060: return new TestSuite(TestStatusLine.class);
061: }
062:
063: // ------------------------------------------------------ Protected Methods
064:
065: // ----------------------------------------------------------- Test Methods
066:
067: public void testIfStatusLine() throws Exception {
068: assertTrue(StatusLine.startsWithHTTP("HTTP"));
069: assertTrue(StatusLine.startsWithHTTP(" HTTP"));
070: assertTrue(StatusLine.startsWithHTTP("\rHTTP"));
071: assertTrue(StatusLine.startsWithHTTP("\tHTTP"));
072: assertFalse(StatusLine.startsWithHTTP("crap"));
073: assertFalse(StatusLine.startsWithHTTP("HTT"));
074: assertFalse(StatusLine.startsWithHTTP("http"));
075: }
076:
077: public void testSuccess() throws Exception {
078: //typical status line
079: statusLine = new StatusLine("HTTP/1.1 200 OK");
080: assertEquals("HTTP/1.1 200 OK", statusLine.toString());
081: assertEquals("HTTP/1.1", statusLine.getHttpVersion());
082: assertEquals(200, statusLine.getStatusCode());
083: assertEquals("OK", statusLine.getReasonPhrase());
084:
085: //status line with multi word reason phrase
086: statusLine = new StatusLine("HTTP/1.1 404 Not Found");
087: assertEquals(404, statusLine.getStatusCode());
088: assertEquals("Not Found", statusLine.getReasonPhrase());
089:
090: //reason phrase can be anyting
091: statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
092: assertEquals("Non Trouve", statusLine.getReasonPhrase());
093:
094: //its ok to end with a \n\r
095: statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
096: assertEquals("Not Found", statusLine.getReasonPhrase());
097:
098: //this is valid according to the Status-Line BNF
099: statusLine = new StatusLine("HTTP/1.1 200 ");
100: assertEquals(200, statusLine.getStatusCode());
101: assertEquals("", statusLine.getReasonPhrase());
102:
103: //this is not strictly valid, but is lienent
104: statusLine = new StatusLine("HTTP/1.1 200");
105: assertEquals(200, statusLine.getStatusCode());
106: assertEquals("", statusLine.getReasonPhrase());
107:
108: //this is not strictly valid, but is lienent
109: statusLine = new StatusLine("HTTP/1.1 200 OK");
110: assertEquals(200, statusLine.getStatusCode());
111: assertEquals("OK", statusLine.getReasonPhrase());
112:
113: //this is not strictly valid, but is lienent
114: statusLine = new StatusLine("\rHTTP/1.1 200 OK");
115: assertEquals(200, statusLine.getStatusCode());
116: assertEquals("OK", statusLine.getReasonPhrase());
117: assertEquals("HTTP/1.1", statusLine.getHttpVersion());
118:
119: //this is not strictly valid, but is lienent
120: statusLine = new StatusLine(" HTTP/1.1 200 OK");
121: assertEquals(200, statusLine.getStatusCode());
122: assertEquals("OK", statusLine.getReasonPhrase());
123: assertEquals("HTTP/1.1", statusLine.getHttpVersion());
124: }
125:
126: public void testFailure() throws Exception {
127: try {
128: statusLine = new StatusLine(null);
129: fail();
130: } catch (NullPointerException e) { /* expected */
131: }
132:
133: try {
134: statusLine = new StatusLine("xxx 200 OK");
135: fail();
136: } catch (HttpException e) { /* expected */
137: }
138:
139: try {
140: statusLine = new StatusLine("HTTP/1.1 xxx OK");
141: fail();
142: } catch (HttpException e) { /* expected */
143: }
144:
145: try {
146: statusLine = new StatusLine("HTTP/1.1 ");
147: fail();
148: } catch (HttpException e) { /* expected */
149: }
150: }
151:
152: }
|