1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
47 public TestStatusLine(String testName) {
48 super(testName);
49 }
50
51
52 public static void main(String args[]) {
53 String[] testCaseName = { TestStatusLine.class.getName() };
54 junit.textui.TestRunner.main(testCaseName);
55 }
56
57
58
59 public static Test suite() {
60 return new TestSuite(TestStatusLine.class);
61 }
62
63
64
65
66
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
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
87 statusLine = new StatusLine("HTTP/1.1 404 Not Found");
88 assertEquals(404, statusLine.getStatusCode());
89 assertEquals("Not Found", statusLine.getReasonPhrase());
90
91
92 statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
93 assertEquals("Non Trouve", statusLine.getReasonPhrase());
94
95
96 statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
97 assertEquals("Not Found", statusLine.getReasonPhrase());
98
99
100 statusLine = new StatusLine("HTTP/1.1 200 ");
101 assertEquals(200, statusLine.getStatusCode());
102 assertEquals("", statusLine.getReasonPhrase());
103
104
105 statusLine = new StatusLine("HTTP/1.1 200");
106 assertEquals(200, statusLine.getStatusCode());
107 assertEquals("", statusLine.getReasonPhrase());
108
109
110 statusLine = new StatusLine("HTTP/1.1 200 OK");
111 assertEquals(200, statusLine.getStatusCode());
112 assertEquals("OK", statusLine.getReasonPhrase());
113
114
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
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) {
132
133 try {
134 statusLine = new StatusLine("xxx 200 OK");
135 fail();
136 } catch (HttpException e) {
137
138 try {
139 statusLine = new StatusLine("HTTP/1.1 xxx OK");
140 fail();
141 } catch (HttpException e) {
142
143 try {
144 statusLine = new StatusLine("HTTP/1.1 ");
145 fail();
146 } catch (HttpException e) {
147 }
148
149 }