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 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
46 public TestStatusLine(String testName) {
47 super(testName);
48 }
49
50
51 public static void main(String args[]) {
52 String[] testCaseName = { TestStatusLine.class.getName() };
53 junit.textui.TestRunner.main(testCaseName);
54 }
55
56
57
58 public static Test suite() {
59 return new TestSuite(TestStatusLine.class);
60 }
61
62
63
64
65
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
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
86 statusLine = new StatusLine("HTTP/1.1 404 Not Found");
87 assertEquals(404, statusLine.getStatusCode());
88 assertEquals("Not Found", statusLine.getReasonPhrase());
89
90
91 statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
92 assertEquals("Non Trouve", statusLine.getReasonPhrase());
93
94
95 statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
96 assertEquals("Not Found", statusLine.getReasonPhrase());
97
98
99 statusLine = new StatusLine("HTTP/1.1 200 ");
100 assertEquals(200, statusLine.getStatusCode());
101 assertEquals("", statusLine.getReasonPhrase());
102
103
104 statusLine = new StatusLine("HTTP/1.1 200");
105 assertEquals(200, statusLine.getStatusCode());
106 assertEquals("", statusLine.getReasonPhrase());
107
108
109 statusLine = new StatusLine("HTTP/1.1 200 OK");
110 assertEquals(200, statusLine.getStatusCode());
111 assertEquals("OK", statusLine.getReasonPhrase());
112
113
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
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) {
131
132 try {
133 statusLine = new StatusLine("xxx 200 OK");
134 fail();
135 } catch (HttpException e) {
136
137 try {
138 statusLine = new StatusLine("HTTP/1.1 xxx OK");
139 fail();
140 } catch (HttpException e) {
141
142 try {
143 statusLine = new StatusLine("HTTP/1.1 ");
144 fail();
145 } catch (HttpException e) {
146 }
147
148 }