1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestStatusLine.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient;
33  
34  import junit.framework.*;
35  
36  /***
37   * Simple tests for {@link StatusLine}.
38   *
39   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
40   * @version $Id: TestStatusLine.java 608014 2008-01-02 05:48:53Z rolandw $
41   */
42  public class TestStatusLine extends TestCase {
43  
44      private StatusLine statusLine = null;
45  
46      // ------------------------------------------------------------ Constructor
47      public TestStatusLine(String testName) {
48          super(testName);
49      }
50  
51      // ------------------------------------------------------------------- Main
52      public static void main(String args[]) {
53          String[] testCaseName = { TestStatusLine.class.getName() };
54          junit.textui.TestRunner.main(testCaseName);
55      }
56  
57      // ------------------------------------------------------- TestCase Methods
58  
59      public static Test suite() {
60          return new TestSuite(TestStatusLine.class);
61      }
62  
63      // ------------------------------------------------------ Protected Methods
64  
65  
66      // ----------------------------------------------------------- Test Methods
67  
68      public void testIfStatusLine() throws Exception {
69          assertTrue(StatusLine.startsWithHTTP("HTTP"));
70          assertTrue(StatusLine.startsWithHTTP("         HTTP"));
71          assertTrue(StatusLine.startsWithHTTP("\rHTTP"));
72          assertTrue(StatusLine.startsWithHTTP("\tHTTP"));
73          assertFalse(StatusLine.startsWithHTTP("crap"));
74          assertFalse(StatusLine.startsWithHTTP("HTT"));
75          assertFalse(StatusLine.startsWithHTTP("http"));
76      }
77  
78      public void testSuccess() throws Exception {
79          //typical status line
80          statusLine = new StatusLine("HTTP/1.1 200 OK");
81          assertEquals("HTTP/1.1 200 OK", statusLine.toString());
82          assertEquals("HTTP/1.1", statusLine.getHttpVersion());
83          assertEquals(200, statusLine.getStatusCode());
84          assertEquals("OK", statusLine.getReasonPhrase());
85  
86          //status line with multi word reason phrase
87          statusLine = new StatusLine("HTTP/1.1 404 Not Found");
88          assertEquals(404, statusLine.getStatusCode());
89          assertEquals("Not Found", statusLine.getReasonPhrase());
90  
91          //reason phrase can be anyting
92          statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
93          assertEquals("Non Trouve", statusLine.getReasonPhrase());
94  
95          //its ok to end with a \n\r
96          statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
97          assertEquals("Not Found", statusLine.getReasonPhrase());
98  
99          //this is valid according to the Status-Line BNF
100         statusLine = new StatusLine("HTTP/1.1 200 ");
101         assertEquals(200, statusLine.getStatusCode());
102         assertEquals("", statusLine.getReasonPhrase());
103 
104         //this is not strictly valid, but is lienent
105         statusLine = new StatusLine("HTTP/1.1 200");
106         assertEquals(200, statusLine.getStatusCode());
107         assertEquals("", statusLine.getReasonPhrase());
108 
109         //this is not strictly valid, but is lienent
110         statusLine = new StatusLine("HTTP/1.1     200 OK");
111         assertEquals(200, statusLine.getStatusCode());
112         assertEquals("OK", statusLine.getReasonPhrase());
113 
114         //this is not strictly valid, but is lienent
115         statusLine = new StatusLine("\rHTTP/1.1 200 OK");
116         assertEquals(200, statusLine.getStatusCode());
117         assertEquals("OK", statusLine.getReasonPhrase());
118         assertEquals("HTTP/1.1", statusLine.getHttpVersion());
119 
120         //this is not strictly valid, but is lienent
121         statusLine = new StatusLine("  HTTP/1.1 200 OK");
122         assertEquals(200, statusLine.getStatusCode());
123         assertEquals("OK", statusLine.getReasonPhrase());
124         assertEquals("HTTP/1.1", statusLine.getHttpVersion());
125     }
126 
127     public void testFailure() throws Exception {
128         try {
129             statusLine = new StatusLine(null);
130             fail();
131         } catch (NullPointerException e) { /* expected */ }
132 
133         try {
134             statusLine = new StatusLine("xxx 200 OK");
135             fail();
136         } catch (HttpException e) { /* expected */ }
137 
138         try {
139             statusLine = new StatusLine("HTTP/1.1 xxx OK");
140             fail();
141         } catch (HttpException e) { /* expected */ }
142 
143         try {
144             statusLine = new StatusLine("HTTP/1.1    ");
145             fail();
146         } catch (HttpException e) { /* expected */ }
147     }
148 
149 }