1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import junit.framework.*;
34  
35  /***
36   * Simple tests for {@link StatusLine}.
37   *
38   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
39   * @version $Id: TestStatusLine.java 155418 2005-02-26 13:01:52Z dirkv $
40   */
41  public class TestStatusLine extends TestCase {
42  
43      private StatusLine statusLine = null;
44  
45      // ------------------------------------------------------------ Constructor
46      public TestStatusLine(String testName) {
47          super(testName);
48      }
49  
50      // ------------------------------------------------------------------- Main
51      public static void main(String args[]) {
52          String[] testCaseName = { TestStatusLine.class.getName() };
53          junit.textui.TestRunner.main(testCaseName);
54      }
55  
56      // ------------------------------------------------------- TestCase Methods
57  
58      public static Test suite() {
59          return new TestSuite(TestStatusLine.class);
60      }
61  
62      // ------------------------------------------------------ Protected Methods
63  
64  
65      // ----------------------------------------------------------- Test Methods
66  
67      public void testIfStatusLine() throws Exception {
68          assertTrue(StatusLine.startsWithHTTP("HTTP"));
69          assertTrue(StatusLine.startsWithHTTP("         HTTP"));
70          assertTrue(StatusLine.startsWithHTTP("\rHTTP"));
71          assertTrue(StatusLine.startsWithHTTP("\tHTTP"));
72          assertFalse(StatusLine.startsWithHTTP("crap"));
73          assertFalse(StatusLine.startsWithHTTP("HTT"));
74          assertFalse(StatusLine.startsWithHTTP("http"));
75      }
76  
77      public void testSuccess() throws Exception {
78          //typical status line
79          statusLine = new StatusLine("HTTP/1.1 200 OK");
80          assertEquals("HTTP/1.1 200 OK", statusLine.toString());
81          assertEquals("HTTP/1.1", statusLine.getHttpVersion());
82          assertEquals(200, statusLine.getStatusCode());
83          assertEquals("OK", statusLine.getReasonPhrase());
84  
85          //status line with multi word reason phrase
86          statusLine = new StatusLine("HTTP/1.1 404 Not Found");
87          assertEquals(404, statusLine.getStatusCode());
88          assertEquals("Not Found", statusLine.getReasonPhrase());
89  
90          //reason phrase can be anyting
91          statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
92          assertEquals("Non Trouve", statusLine.getReasonPhrase());
93  
94          //its ok to end with a \n\r
95          statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
96          assertEquals("Not Found", statusLine.getReasonPhrase());
97  
98          //this is valid according to the Status-Line BNF
99          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         try {
133             statusLine = new StatusLine("xxx 200 OK");
134             fail();
135         } catch (HttpException e) { /* expected */ }
136 
137         try {
138             statusLine = new StatusLine("HTTP/1.1 xxx OK");
139             fail();
140         } catch (HttpException e) { /* expected */ }
141 
142         try {
143             statusLine = new StatusLine("HTTP/1.1    ");
144             fail();
145         } catch (HttpException e) { /* expected */ }
146     }
147 
148 }