1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStatusLine.java,v 1.6.2.1 2003/10/19 18:49:56 olegk Exp $
3 * $Revision: 1.6.2.1 $
4 * $Date: 2003/10/19 18:49:56 $
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63 package org.apache.commons.httpclient;
64
65 import junit.framework.*;
66
67 /***
68 * Simple tests for {@link StatusLine}.
69 *
70 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
71 * @version $Id: TestStatusLine.java,v 1.6.2.1 2003/10/19 18:49:56 olegk Exp $
72 */
73 public class TestStatusLine extends TestCase {
74
75 private StatusLine statusLine = null;
76
77 // ------------------------------------------------------------ Constructor
78 public TestStatusLine(String testName) {
79 super(testName);
80 }
81
82 // ------------------------------------------------------------------- Main
83 public static void main(String args[]) {
84 String[] testCaseName = { TestStatusLine.class.getName() };
85 junit.textui.TestRunner.main(testCaseName);
86 }
87
88 // ------------------------------------------------------- TestCase Methods
89
90 public static Test suite() {
91 return new TestSuite(TestStatusLine.class);
92 }
93
94 // ------------------------------------------------------ Protected Methods
95
96
97 // ----------------------------------------------------------- Test Methods
98
99 public void testIfStatusLine() throws Exception {
100 assertTrue(StatusLine.startsWithHTTP("HTTP"));
101 assertTrue(StatusLine.startsWithHTTP(" HTTP"));
102 assertTrue(StatusLine.startsWithHTTP("\rHTTP"));
103 assertTrue(StatusLine.startsWithHTTP("\tHTTP"));
104 assertFalse(StatusLine.startsWithHTTP("crap"));
105 assertFalse(StatusLine.startsWithHTTP("HTT"));
106 assertFalse(StatusLine.startsWithHTTP("http"));
107 }
108
109 public void testSuccess() throws Exception {
110 //typical status line
111 statusLine = new StatusLine("HTTP/1.1 200 OK");
112 assertEquals("HTTP/1.1 200 OK", statusLine.toString());
113 assertEquals("HTTP/1.1", statusLine.getHttpVersion());
114 assertEquals(200, statusLine.getStatusCode());
115 assertEquals("OK", statusLine.getReasonPhrase());
116
117 //status line with multi word reason phrase
118 statusLine = new StatusLine("HTTP/1.1 404 Not Found");
119 assertEquals(404, statusLine.getStatusCode());
120 assertEquals("Not Found", statusLine.getReasonPhrase());
121
122 //reason phrase can be anyting
123 statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
124 assertEquals("Non Trouve", statusLine.getReasonPhrase());
125
126 //its ok to end with a \n\r
127 statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
128 assertEquals("Not Found", statusLine.getReasonPhrase());
129
130 //this is valid according to the Status-Line BNF
131 statusLine = new StatusLine("HTTP/1.1 200 ");
132 assertEquals(200, statusLine.getStatusCode());
133 assertEquals("", statusLine.getReasonPhrase());
134
135 //this is not strictly valid, but is lienent
136 statusLine = new StatusLine("HTTP/1.1 200");
137 assertEquals(200, statusLine.getStatusCode());
138 assertEquals("", statusLine.getReasonPhrase());
139
140 //this is not strictly valid, but is lienent
141 statusLine = new StatusLine("HTTP/1.1 200 OK");
142 assertEquals(200, statusLine.getStatusCode());
143 assertEquals("OK", statusLine.getReasonPhrase());
144
145 //this is not strictly valid, but is lienent
146 statusLine = new StatusLine("\rHTTP/1.1 200 OK");
147 assertEquals(200, statusLine.getStatusCode());
148 assertEquals("OK", statusLine.getReasonPhrase());
149 assertEquals("HTTP/1.1", statusLine.getHttpVersion());
150
151 //this is not strictly valid, but is lienent
152 statusLine = new StatusLine(" HTTP/1.1 200 OK");
153 assertEquals(200, statusLine.getStatusCode());
154 assertEquals("OK", statusLine.getReasonPhrase());
155 assertEquals("HTTP/1.1", statusLine.getHttpVersion());
156 }
157
158 public void testFailure() throws Exception {
159 try {
160 statusLine = new StatusLine(null);
161 fail();
162 } catch (NullPointerException e) { /* expected */ }
163
164 try {
165 statusLine = new StatusLine("xxx 200 OK");
166 fail();
167 } catch (HttpException e) { /* expected */ }
168
169 try {
170 statusLine = new StatusLine("HTTP/1.1 xxx OK");
171 fail();
172 } catch (HttpException e) { /* expected */ }
173
174 }
175
176 }
This page was automatically generated by Maven