| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Test Case for HTTP_Header_Cache
|
|
|
4 |
*
|
|
|
5 |
* Id$
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
require_once 'PHPUnit.php';
|
|
|
9 |
require_once 'HTTP/Header/Cache.php';
|
|
|
10 |
|
|
|
11 |
class HTTP_Header_CacheTest extends PHPUnit_TestCase
|
|
|
12 |
{
|
|
|
13 |
function HTTP_Header_CacheTest($name)
|
|
|
14 |
{
|
|
|
15 |
$this->PHPUnit_TestCase($name);
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
function setUp()
|
|
|
19 |
{
|
|
|
20 |
$this->testScript = 'http://local/www/mike/pear/HTTP_Header/tests/cacheresponse.php';
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
function tearDown()
|
|
|
24 |
{
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
function testHTTP_Header_Cache()
|
|
|
28 |
{
|
|
|
29 |
$this->assertTrue(is_a(new HTTP_Header_Cache, 'HTTP_Header_Cache'));
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
function testgetCacheStart()
|
|
|
33 |
{
|
|
|
34 |
$c = &new HTTP_Header_Cache;
|
|
|
35 |
$this->assertEquals(time(), $c->getCacheStart());
|
|
|
36 |
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = HTTP::Date(strtotime('yesterday'));
|
|
|
37 |
$this->assertEquals($_SERVER['HTTP_IF_MODIFIED_SINCE'], HTTP::Date($c->getCacheStart()));
|
|
|
38 |
unset($c, $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
function testisOlderThan()
|
|
|
42 |
{
|
|
|
43 |
$c = &new HTTP_Header_Cache;
|
|
|
44 |
$this->assertTrue($c->isOlderThan(1, 'second'));
|
|
|
45 |
$this->assertTrue($c->isOlderThan(1, 'hour'));
|
|
|
46 |
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = HTTP::Date(time() - 3);
|
|
|
47 |
$this->assertTrue($c->isOlderThan(1, 'second'));
|
|
|
48 |
unset($c, $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
function testisCached()
|
|
|
52 |
{
|
|
|
53 |
$c = &new HTTP_Header_Cache;
|
|
|
54 |
$this->assertFalse($c->isCached(), 'no last modified');
|
|
|
55 |
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = HTTP::Date(strtotime('yesterday'));
|
|
|
56 |
$this->assertTrue($c->isCached(), 'last modified header');
|
|
|
57 |
$this->assertFalse($c->isCached(time()), 'last modified header (yesterday) and param (now)');
|
|
|
58 |
$this->assertTrue($c->isCached(strtotime('last year')), 'last modified header (yesterday) and param (last year)');
|
|
|
59 |
unset($c, $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
function testexitIfCached()
|
|
|
63 |
{
|
|
|
64 |
require_once 'HTTP/Request.php';
|
|
|
65 |
$r = &new HTTP_Request($this->testScript);
|
|
|
66 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
67 |
$r->addHeader('If-Modified-Since', HTTP::Date());
|
|
|
68 |
$r->sendRequest();
|
|
|
69 |
$this->assertEquals(304, $r->getResponseCode(), 'HTTP 304 Not Modified');
|
|
|
70 |
$r->addHeader('If-Modified-Since', HTTP::Date(strtotime('yesterday')));
|
|
|
71 |
$r->sendRequest();
|
|
|
72 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
73 |
unset($r);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
function testget()
|
|
|
77 |
{
|
|
|
78 |
require_once 'HTTP/Request.php';
|
|
|
79 |
$r = &new HTTP_Request($this->testScript);
|
|
|
80 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
81 |
$r->sendRequest();
|
|
|
82 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (simple plain GET)');
|
|
|
83 |
$r->addHeader('If-Modified-Since', $r->getResponseHeader('last-modified'));
|
|
|
84 |
sleep(3);
|
|
|
85 |
$r->sendRequest();
|
|
|
86 |
$this->assertEquals(304, $r->getResponseCode(), 'HTTP 304 Not Modified (GET with If-Modified-Since set to Last-Modified of prior request');
|
|
|
87 |
unset($r);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
function testhead()
|
|
|
91 |
{
|
|
|
92 |
require_once 'HTTP/Request.php';
|
|
|
93 |
$r = &new HTTP_Request($this->testScript);
|
|
|
94 |
$r->setMethod(HTTP_REQUEST_METHOD_HEAD);
|
|
|
95 |
$r->sendRequest();
|
|
|
96 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (simple plain GET)');
|
|
|
97 |
$r->addHeader('If-Modified-Since', $r->getResponseHeader('last-modified'));
|
|
|
98 |
sleep(3);
|
|
|
99 |
$r->sendRequest();
|
|
|
100 |
$this->assertEquals(304, $r->getResponseCode(), 'HTTP 304 Not Modified (GET with If-Modified-Since set to Last-Modified of prior request');
|
|
|
101 |
unset($r);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
function testpost()
|
|
|
105 |
{
|
|
|
106 |
require_once 'HTTP/Request.php';
|
|
|
107 |
$r = &new HTTP_Request($this->testScript);
|
|
|
108 |
$r->setMethod(HTTP_REQUEST_METHOD_GET);
|
|
|
109 |
$r->sendRequest();
|
|
|
110 |
$lm = $r->getResponseHeader('last-modified');
|
|
|
111 |
$r->setMethod(HTTP_REQUEST_METHOD_POST);
|
|
|
112 |
$r->sendRequest();
|
|
|
113 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (POST without If-Modified-Since)');
|
|
|
114 |
$r->addHeader('If-Modified-Since', HTTP::Date(strtotime('yesterday')));
|
|
|
115 |
$r->sendRequest();
|
|
|
116 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (POST with If-Modified-Since == yesterday)');
|
|
|
117 |
$r->addHeader('If-Modified-Since', HTTP::Date(time() - 3));
|
|
|
118 |
$r->sendRequest();
|
|
|
119 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (POST with If-Modified-Since == now)');
|
|
|
120 |
$r->addHeader('If-Modified-Since', HTTP::Date($lm));
|
|
|
121 |
sleep(3);
|
|
|
122 |
$r->sendRequest();
|
|
|
123 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok (POST with If-Modified-Since == Last-Modified)');
|
|
|
124 |
$this->assertEquals(HTTP::Date(), $r->getResponseHeader('last-modified'), 'POST time() == Last-Modified');
|
|
|
125 |
unset($r);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$suite = &new PHPUnit_TestSuite('HTTP_Header_CacheTest');
|
|
|
130 |
$result = &PHPUnit::run($suite);
|
|
|
131 |
echo $result->toString();
|
|
|
132 |
|
|
|
133 |
?>
|