| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TestCase for HTTP_Header
|
|
|
4 |
*
|
|
|
5 |
* $Id: header.php 163375 2004-07-15 13:11:13Z mike $
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
require_once 'PHPUnit.php';
|
|
|
9 |
require_once 'HTTP/Header.php';
|
|
|
10 |
|
|
|
11 |
class HTTP_HeaderTest extends PHPUnit_TestCase
|
|
|
12 |
{
|
|
|
13 |
function HTTP_HeaderTest($name)
|
|
|
14 |
{
|
|
|
15 |
$this->PHPUnit_TestCase($name);
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
function setUp()
|
|
|
19 |
{
|
|
|
20 |
$this->testScript = 'http://local/www/mike/pear/HTTP_Header/tests/response.php';
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
function tearDown()
|
|
|
24 |
{
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
function testHTTP_Header()
|
|
|
28 |
{
|
|
|
29 |
$h = &new HTTP_Header;
|
|
|
30 |
$this->assertTrue(is_a($h, 'HTTP_Header'));
|
|
|
31 |
$this->assertTrue($h->_httpVersion == 1.1 || $h->_httpVersion == 1.0);
|
|
|
32 |
unset($h);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
function testsetHttpVersion()
|
|
|
36 |
{
|
|
|
37 |
$h = &new HTTP_Header;
|
|
|
38 |
$this->assertFalse($h->setHttpVersion('foo'));
|
|
|
39 |
$this->assertTrue($h->setHttpVersion(1.0));
|
|
|
40 |
$this->assertTrue($h->setHttpVersion(1.1));
|
|
|
41 |
$this->assertTrue($h->setHttpVersion(1));
|
|
|
42 |
$this->assertTrue($h->setHttpVersion(1.111111111));
|
|
|
43 |
$this->assertTrue($h->setHttpVersion('1'));
|
|
|
44 |
$this->assertTrue($h->setHttpVersion('1.1'));
|
|
|
45 |
$this->assertTrue($h->setHttpVersion('1.0000000000000'));
|
|
|
46 |
$this->assertFalse($h->setHttpVersion(2));
|
|
|
47 |
unset($h);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
function testgetHttpVersion()
|
|
|
51 |
{
|
|
|
52 |
$h = new HTTP_Header;
|
|
|
53 |
$this->assertTrue($h->getHttpVersion() == 1.0 || $h->getHttpVersion() == 1.1, ' http version is 1.0 or 1.1');
|
|
|
54 |
$h->setHttpVersion(1);
|
|
|
55 |
$this->assertEquals(1, $h->getHttpVersion());
|
|
|
56 |
$h->setHttpVersion(1.1);
|
|
|
57 |
$this->assertEquals(1.1, $h->getHttpVersion());
|
|
|
58 |
$h->setHttpVersion(2);
|
|
|
59 |
$this->assertEquals(1.1, $h->getHttpVersion());
|
|
|
60 |
unset($h);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
function testsetHeader()
|
|
|
64 |
{
|
|
|
65 |
$h = &new HTTP_Header;
|
|
|
66 |
$this->assertFalse($h->setHeader(null), 'set null');
|
|
|
67 |
$this->assertFalse($h->setHeader(''), ' set empty string');
|
|
|
68 |
$this->assertFalse($h->setHeader(0), 'set 0');
|
|
|
69 |
$this->assertTrue($h->setHeader('X-Foo', 'bla'), 'set X-Foo = bla');
|
|
|
70 |
$this->assertFalse($h->setHeader('X-Array', array('foo')), 'set array');
|
|
|
71 |
$this->assertFalse($h->setHeader('X-Object', new StdClass), 'set object');
|
|
|
72 |
unset($h);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
function testgetHeader()
|
|
|
76 |
{
|
|
|
77 |
$h = &new HTTP_Header;
|
|
|
78 |
$this->assertEquals('no-cache', $h->getHeader('Pragma'));
|
|
|
79 |
$this->assertEquals('no-cache', $h->getHeader('pRaGmA'));
|
|
|
80 |
$h->setHeader('X-Foo', 'foo');
|
|
|
81 |
$this->assertEquals('foo', $h->getHeader('X-Foo'));
|
|
|
82 |
$this->assertEquals('foo', $h->getHeader('x-FoO'));
|
|
|
83 |
$this->assertTrue(is_array($h->getHeader()), 'test for array');
|
|
|
84 |
$this->assertFalse($h->getHeader('Non-Existant'), 'test unset header');
|
|
|
85 |
unset($h);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
function testsendHeaders()
|
|
|
89 |
{
|
|
|
90 |
require_once 'HTTP/Request.php';
|
|
|
91 |
$r = &new HTTP_Request($this->testScript);
|
|
|
92 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
93 |
$r->addQueryString('X-Foo', 'blablubb');
|
|
|
94 |
$r->sendRequest();
|
|
|
95 |
$this->assertEquals('blablubb', $r->getResponseHeader('x-foo'));
|
|
|
96 |
unset($h, $r);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
function testsendStatusCode()
|
|
|
100 |
{
|
|
|
101 |
require_once 'HTTP/Request.php';
|
|
|
102 |
$r = &new HTTP_Request($this->testScript);
|
|
|
103 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
104 |
$r->sendRequest();
|
|
|
105 |
$this->assertEquals(200, $r->getResponseCode(), 'test for response code 200');
|
|
|
106 |
$r->addQueryString('status', 500);
|
|
|
107 |
$r->sendRequest();
|
|
|
108 |
$this->assertEquals(500, $r->getResponseCode(), 'test for response code 500');
|
|
|
109 |
unset($h, $r);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
function testdateToTimestamp()
|
|
|
113 |
{
|
|
|
114 |
$h = &new HTTP_Header;
|
|
|
115 |
$this->assertEquals(strtotime($d = HTTP::Date()), $h->dateToTimestamp($d));
|
|
|
116 |
unset($h);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
function testredirect()
|
|
|
120 |
{
|
|
|
121 |
require_once 'HTTP/Request.php';
|
|
|
122 |
$r = &new HTTP_Request($this->testScript, array('allowRedirects' => false));
|
|
|
123 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
124 |
$r->addQueryString('redirect', 'response.php?abc=123');
|
|
|
125 |
$r->sendRequest();
|
|
|
126 |
$this->assertEquals(302, $r->getResponseCode(), 'test for response code 302');
|
|
|
127 |
$this->assertTrue(strstr($r->getResponseHeader('location'), 'response.php'));
|
|
|
128 |
unset($h, $r);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
function testgetStatusType()
|
|
|
132 |
{
|
|
|
133 |
$h = &new HTTP_Header;
|
|
|
134 |
$this->assertEquals(HTTP_HEADER_STATUS_INFORMATIONAL, $h->getStatusType(101));
|
|
|
135 |
$this->assertEquals(HTTP_HEADER_STATUS_SUCCESSFUL, $h->getStatusType(206));
|
|
|
136 |
$this->assertEquals(HTTP_HEADER_STATUS_REDIRECT, $h->getStatusType(301));
|
|
|
137 |
$this->assertEquals(HTTP_HEADER_STATUS_CLIENT_ERROR, $h->getStatusType(404));
|
|
|
138 |
$this->assertEquals(HTTP_HEADER_STATUS_SERVER_ERROR, $h->getStatusType(500));
|
|
|
139 |
$this->assertFalse($h->getStatusType(8));
|
|
|
140 |
unset($h);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
function testgetStatusText()
|
|
|
144 |
{
|
|
|
145 |
$h = &new HTTP_Header;
|
|
|
146 |
$this->assertEquals(HTTP_HEADER_STATUS_100, '100 '. $h->getStatusText(100));
|
|
|
147 |
$this->assertEquals(HTTP_HEADER_STATUS_200, '200 '. $h->getStatusText(200));
|
|
|
148 |
$this->assertEquals(HTTP_HEADER_STATUS_300, '300 '. $h->getStatusText(300));
|
|
|
149 |
$this->assertEquals(HTTP_HEADER_STATUS_302, '302 '. $h->getStatusText(302));
|
|
|
150 |
$this->assertEquals(HTTP_HEADER_STATUS_401, '401 '. $h->getStatusText(401));
|
|
|
151 |
$this->assertEquals(HTTP_HEADER_STATUS_400, '400 '. $h->getStatusText(400));
|
|
|
152 |
$this->assertEquals(HTTP_HEADER_STATUS_500, '500 '. $h->getStatusText(500));
|
|
|
153 |
$this->assertEquals(HTTP_HEADER_STATUS_102, '102 '. $h->getStatusText(102));
|
|
|
154 |
$this->assertEquals(HTTP_HEADER_STATUS_404, '404 '. $h->getStatusText(404));
|
|
|
155 |
$this->assertFalse($h->getStatusText(0));
|
|
|
156 |
$this->assertFalse($h->getStatusText(800));
|
|
|
157 |
unset($h);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
function testisInformational()
|
|
|
161 |
{
|
|
|
162 |
$h = &new HTTP_Header;
|
|
|
163 |
$this->assertTrue($h->isInformational(100));
|
|
|
164 |
$this->assertTrue($h->isInformational(101));
|
|
|
165 |
$this->assertTrue($h->isInformational(102));
|
|
|
166 |
$this->assertFalse($h->isInformational(404));
|
|
|
167 |
unset($h);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
function testisSuccessful()
|
|
|
171 |
{
|
|
|
172 |
$h = &new HTTP_Header;
|
|
|
173 |
$this->assertTrue($h->isSuccessful(200));
|
|
|
174 |
$this->assertTrue($h->isSuccessful(201));
|
|
|
175 |
$this->assertTrue($h->isSuccessful(202));
|
|
|
176 |
$this->assertFalse($h->isSuccessful(404));
|
|
|
177 |
unset($h);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
function testisRedirect()
|
|
|
181 |
{
|
|
|
182 |
$h = &new HTTP_Header;
|
|
|
183 |
$this->assertTrue($h->isRedirect(300));
|
|
|
184 |
$this->assertTrue($h->isRedirect(301));
|
|
|
185 |
$this->assertTrue($h->isRedirect(302));
|
|
|
186 |
$this->assertFalse($h->isRedirect(404));
|
|
|
187 |
unset($h);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
function testisClientError()
|
|
|
191 |
{
|
|
|
192 |
$h = &new HTTP_Header;
|
|
|
193 |
$this->assertTrue($h->isClientError(400));
|
|
|
194 |
$this->assertTrue($h->isClientError(401));
|
|
|
195 |
$this->assertTrue($h->isClientError(404));
|
|
|
196 |
$this->assertFalse($h->isClientError(500));
|
|
|
197 |
unset($h);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
function testisServerError()
|
|
|
201 |
{
|
|
|
202 |
$h = &new HTTP_Header;
|
|
|
203 |
$this->assertTrue($h->isServerError(500));
|
|
|
204 |
$this->assertTrue($h->isServerError(501));
|
|
|
205 |
$this->assertTrue($h->isServerError(502));
|
|
|
206 |
$this->assertFalse($h->isServerError(404));
|
|
|
207 |
unset($h);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
function testisError()
|
|
|
211 |
{
|
|
|
212 |
$h = &new HTTP_Header;
|
|
|
213 |
$this->assertTrue($h->isError(500));
|
|
|
214 |
$this->assertTrue($h->isError(501));
|
|
|
215 |
$this->assertTrue($h->isError(502));
|
|
|
216 |
$this->assertFalse($h->isError(206));
|
|
|
217 |
$this->assertTrue($h->isError(400));
|
|
|
218 |
$this->assertTrue($h->isError(401));
|
|
|
219 |
$this->assertTrue($h->isError(404));
|
|
|
220 |
$this->assertFalse($h->isError(100));
|
|
|
221 |
unset($h);
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
$suite = &new PHPUnit_TestSuite('HTTP_HeaderTest');
|
|
|
226 |
$result = &PHPUnit::run($suite);
|
|
|
227 |
echo $result->toString();
|
|
|
228 |
|
|
|
229 |
?>
|