Skip to content

Commit

Permalink
[HttpKernel] fixed Cache, to respect the variable and trigger error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
avalanche123 authored and fabpot committed Jan 19, 2011
1 parent 8d6da86 commit 267a7e6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/Symfony/Component/HttpKernel/Cache/Cache.php
Expand Up @@ -145,11 +145,11 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
$this->traces[$request->getMethod().' '.$path] = array();

if (!$request->isMethodSafe($request)) {
$response = $this->invalidate($request);
$response = $this->invalidate($request, $catch);
} elseif ($request->headers->has('expect')) {
$response = $this->pass($request);
$response = $this->pass($request, $catch);
} else {
$response = $this->lookup($request);
$response = $this->lookup($request, $catch);
}

$response->isNotModified($request);
Expand All @@ -171,33 +171,35 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
* Forwards the Request to the backend without storing the Response in the cache.
*
* @param Request $request A Request instance
* @param Boolean $catch whether to process exceptions
*
* @return Response A Response instance
*/
protected function pass(Request $request)
protected function pass(Request $request, $catch = false)
{
$this->record($request, 'pass');

return $this->forward($request);
return $this->forward($request, $catch);
}

/**
* Invalidates non-safe methods (like POST, PUT, and DELETE).
*
* @param Request $request A Request instance
* @param Boolean $catch whether to process exceptions
*
* @return Response A Response instance
*
* @see RFC2616 13.10
*/
protected function invalidate(Request $request)
protected function invalidate(Request $request, $catch = false)
{
$response = $this->pass($request);
$response = $this->pass($request, $catch);

// invalidate only when the response is successful
if ($response->isSuccessful() || $response->isRedirect()) {
try {
$this->store->invalidate($request);
$this->store->invalidate($request, $catch);

$this->record($request, 'invalidate');
} catch (\Exception $e) {
Expand All @@ -222,10 +224,11 @@ protected function invalidate(Request $request)
* it triggers "miss" processing.
*
* @param Request $request A Request instance
* @param Boolean $catch whether to process exceptions
*
* @return Response A Response instance
*/
protected function lookup(Request $request)
protected function lookup(Request $request, $catch = false)
{
// if allow_reload and no-cache Cache-Control, allow a cache reload
if ($this->options['allow_reload'] && $request->isNoCache()) {
Expand All @@ -243,13 +246,13 @@ protected function lookup(Request $request)
throw $e;
}

return $this->pass($request);
return $this->pass($request, $catch);
}

if (null === $entry) {
$this->record($request, 'miss');

return $this->fetch($request);
return $this->fetch($request, $catch);
}

if (!$this->isFreshEnough($request, $entry)) {
Expand Down Expand Up @@ -332,10 +335,11 @@ protected function validate(Request $request, Response $entry)
* This methods is trigered when the cache missed or a reload is required.
*
* @param Request $request A Request instance
* @param Boolean $catch whether to process exceptions
*
* @return Response A Response instance
*/
protected function fetch(Request $request)
protected function fetch(Request $request, $catch = false)
{
$subRequest = clone $request;

Expand All @@ -346,7 +350,7 @@ protected function fetch(Request $request)
$subRequest->headers->remove('if_modified_since');
$subRequest->headers->remove('if_none_match');

$response = $this->forward($subRequest);
$response = $this->forward($subRequest, $catch);

if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
$response->setPrivate(true);
Expand Down
20 changes: 20 additions & 0 deletions tests/Symfony/Tests/Component/HttpKernel/Cache/CacheTest.php
Expand Up @@ -870,4 +870,24 @@ public function testStoresMultipleResponsesWhenHeadersDiffer()
$this->assertEquals('Bob/2.0', $this->response->getContent());
$this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
}

public function testShouldCatchExceptions()
{
$this->catchExceptions();

$this->setNextResponse();
$this->request('GET', '/');

$this->assertExceptionsAreCaught();
}

public function testShouldNotCatchExceptions()
{
$this->catchExceptions(false);

$this->setNextResponse();
$this->request('GET', '/');

$this->assertExceptionsAreNotCaught();
}
}
22 changes: 21 additions & 1 deletion tests/Symfony/Tests/Component/HttpKernel/Cache/CacheTestCase.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Cache\Cache;
use Symfony\Component\HttpKernel\Cache\Store;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CacheTestCase extends \PHPUnit_Framework_TestCase
{
Expand All @@ -26,6 +27,7 @@ class CacheTestCase extends \PHPUnit_Framework_TestCase
protected $request;
protected $response;
protected $responses;
protected $catch;

protected function setUp()
{
Expand All @@ -39,6 +41,8 @@ protected function setUp()
$this->response = null;
$this->responses = array();

$this->catch = false;

$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}

Expand All @@ -51,6 +55,7 @@ protected function tearDown()
$this->response = null;
$this->responses = null;
$this->cacheConfig = null;
$this->catch = null;

$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}
Expand Down Expand Up @@ -86,6 +91,16 @@ public function assertTraceNotContains($trace)
$this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
}

public function assertExceptionsAreCaught()
{
$this->assertTrue($this->kernel->isCatchingExceptions());
}

public function assertExceptionsAreNotCaught()
{
$this->assertFalse($this->kernel->isCatchingExceptions());
}

public function request($method, $uri = '/', $server = array(), $cookies = array())
{
if (null === $this->kernel) {
Expand All @@ -100,7 +115,7 @@ public function request($method, $uri = '/', $server = array(), $cookies = array
$this->cache = new Cache($this->kernel, $this->store, null, $this->cacheConfig);
$this->request = Request::create($uri, $method, array(), $cookies, array(), $server);

$this->response = $this->cache->handle($this->request);
$this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);

$this->responses[] = $this->response;
}
Expand All @@ -123,6 +138,11 @@ public function setNextResponse($statusCode = 200, array $headers = array(), $bo
$this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
}

public function catchExceptions($catch = true)
{
$this->catch = $catch;
}

static public function clearDirectory($directory)
{
if (!is_dir($directory)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Symfony/Tests/Component/HttpKernel/Cache/TestHttpKernel.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Tests\Component\HttpKernel\Cache;

use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -24,6 +25,7 @@ class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
protected $headers;
protected $called;
protected $customizer;
protected $catch;

public function __construct($body, $status, $headers, \Closure $customizer = null)
{
Expand All @@ -32,10 +34,22 @@ public function __construct($body, $status, $headers, \Closure $customizer = nul
$this->headers = $headers;
$this->customizer = $customizer;
$this->called = false;
$this->catch = false;

parent::__construct(new EventDispatcher(), $this);
}

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
{
$this->catch = $catch;
return parent::handle($request, $type, $catch);
}

public function isCatchingExceptions()
{
return $this->catch;
}

public function getController(Request $request)
{
return array($this, 'callController');
Expand Down

0 comments on commit 267a7e6

Please sign in to comment.