/usr/share/php/kohana3.1/system/tests/kohana/request/client/CacheTest.php is in libkohana3.1-core-php 3.1.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for request client cache logic
*
* @group kohana
* @group kohana.request
* @group kohana.request.client
* @group kohana.request.client.cache
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_CacheTest extends Unittest_TestCase {
/**
* Sets up a test route for caching
*
* @return void
*/
public function setUp()
{
Route::set('welcome', 'welcome/index')
->defaults(array(
'controller' => 'welcome',
'action' => 'index'
));
parent::setUp();
}
/**
* Tests the Client does not attempt to load cache if no Cache library
* is present
*
* @return void
*/
public function test_cache_not_called_with_no_cache()
{
$request = new Request('welcome/index');
$response = new Response;
$client_mock = $this->getMock('Request_Client_Internal');
$request->client($client_mock);
$client_mock->expects($this->exactly(0))
->method('execute_request');
$client_mock->expects($this->once())
->method('execute')
->will($this->returnValue($response));
$this->assertSame($response, $request->execute());
}
/**
* Tests that the client attempts to load a cached response from the
* cache library, but fails.
*
* @return void
*/
public function test_cache_miss()
{
$request = new Request('welcome/index');
$cache_mock = $this->_get_cache_mock();
$request->client()->cache(HTTP_Cache::factory($cache_mock));
$cache_mock->expects($this->once())
->method('get')
->with($request->client()->cache()->create_cache_key($request))
->will($this->returnValue(FALSE));
$response = $request->client()->execute($request);
$this->assertSame(HTTP_Cache::CACHE_STATUS_MISS,
$response->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Tests the client saves a response if the correct headers are set
*
* @return void
*/
public function test_cache_save()
{
$lifetime = 800;
$request = new Request('welcome/index');
$cache_mock = $this->_get_cache_mock();
$response = $request->create_response();
$request->client()->cache(new HTTP_Cache(array(
'cache' => $cache_mock
)
));
$response->headers('cache-control', 'max-age='.$lifetime);
$key = $request->client()->cache()->create_cache_key($request);
$cache_mock->expects($this->at(0))
->method('set')
->with($this->stringEndsWith($key), $this->identicalTo(0));
$cache_mock->expects($this->at(1))
->method('set')
->with($this->identicalTo($key), $this->anything(), $this->identicalTo($lifetime))
->will($this->returnValue(TRUE));
$this->assertTrue(
$request->client()->cache()
->cache_response($key, $request, $response)
);
$this->assertSame(HTTP_Cache::CACHE_STATUS_SAVED,
$request->response()->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Tests the client handles a cache HIT event correctly
*
* @return void
*/
public function test_cache_hit()
{
$lifetime = 800;
$request = new Request('welcome/index');
$cache_mock = $this->_get_cache_mock();
$request->client()->cache(new HTTP_Cache(array(
'cache' => $cache_mock
)
));
$response = $request->create_response();
$response->headers(array(
'cache-control' => 'max-age='.$lifetime,
HTTP_Cache::CACHE_STATUS_KEY =>
HTTP_Cache::CACHE_STATUS_HIT
));
$key = $request->client()->cache()->create_cache_key($request);
$cache_mock->expects($this->exactly(2))
->method('get')
->with($this->stringContains($key))
->will($this->returnValue($response));
$request->client()->cache()->cache_response($key, $request);
$this->assertSame(HTTP_Cache::CACHE_STATUS_HIT,
$response->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Data provider for test_set_cache
*
* @return array
*/
public function provider_set_cache()
{
return array(
array(
new HTTP_Header(array('cache-control' => 'no-cache')),
array('no-cache' => NULL),
FALSE,
),
array(
new HTTP_Header(array('cache-control' => 'no-store')),
array('no-store' => NULL),
FALSE,
),
array(
new HTTP_Header(array('cache-control' => 'max-age=100')),
array('max-age' => '100'),
TRUE
),
array(
new HTTP_Header(array('cache-control' => 'private')),
array('private' => NULL),
FALSE
),
array(
new HTTP_Header(array('cache-control' => 'private, max-age=100')),
array('private' => NULL, 'max-age' => '100'),
FALSE
),
array(
new HTTP_Header(array('cache-control' => 'private, s-maxage=100')),
array('private' => NULL, 's-maxage' => '100'),
TRUE
),
array(
new HTTP_Header(array(
'expires' => date('m/d/Y', strtotime('-1 day')),
)),
array(),
FALSE
),
array(
new HTTP_Header(array(
'expires' => date('m/d/Y', strtotime('+1 day')),
)),
array(),
TRUE
),
array(
new HTTP_Header(array()),
array(),
TRUE
),
);
}
/**
* Tests the set_cache() method
*
* @test
* @dataProvider provider_set_cache
*
* @return null
*/
public function test_set_cache($headers, $cache_control, $expected)
{
/**
* Set up a mock response object to test with
*/
$response = $this->getMock('Response');
$response->expects($this->any())
->method('headers')
->will($this->returnValue($headers));
$request = new Request_Client_Internal;
$request->cache(new HTTP_Cache);
$this->assertEquals($request->cache()->set_cache($response), $expected);
}
/**
* Returns a mock object for Cache
*
* @return Cache
*/
protected function _get_cache_mock()
{
return $this->getMock('Cache_File', array(), array(), '', FALSE);
}
} // End Kohana_Request_Client_CacheTest
|