Skip to content

Commit

Permalink
Merge pull request #230 from FriendsOfSymfony/fix-invalidation-status…
Browse files Browse the repository at this point in the history
…-codes

Fix invalidation not being triggered for some status codes
  • Loading branch information
dbu committed Jul 13, 2015
2 parents a716f6c + 3f6cda3 commit ae7aca7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.3.2
-----

* Fixed some status codes (such as 204 and 302) not triggering invalidation.

1.3.1
-----

Expand Down
7 changes: 5 additions & 2 deletions EventListener/InvalidationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public function onKernelTerminate(PostResponseEvent $event)
$response = $event->getResponse();

// Don't invalidate any caches if the request was unsuccessful
if ($response->isSuccessful()) {
if ($response->getStatusCode() >= 200
&& $response->getStatusCode() < 400
) {
$this->handleInvalidation($request, $response);
}

Expand Down Expand Up @@ -144,7 +146,8 @@ public static function getSubscribedEvents()
/**
* Handle the invalidation annotations and configured invalidators.
*
* @param Request $request
* @param Request $request
* @param Response $response
*/
private function handleInvalidation(Request $request, Response $response)
{
Expand Down
2 changes: 1 addition & 1 deletion Http/RuleMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function matches(Request $request, Response $response)
* headers are already set. As we are about to set them, that would
* always return false.
*/
$status = array(200, 203, 300, 301, 302, 404, 410);
$status = array(200, 203, 204, 300, 301, 302, 404, 410);
if (!empty($this->criteria['additional_cacheable_status'])) {
$status = array_merge($status, $this->criteria['additional_cacheable_status']);
}
Expand Down
15 changes: 13 additions & 2 deletions Tests/Functional/EventListener/InvalidationSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function testInvalidateRoute()
$client->request('POST', '/invalidate/route/42');
}

public function testInvalidatePath()
/**
* @dataProvider getStatusCodesThatTriggerInvalidation
*/
public function testInvalidatePath($statusCode)
{
$client = static::createClient();

Expand All @@ -43,10 +46,13 @@ public function testInvalidatePath()
)
->shouldReceive('supports')->andReturn(true)
->shouldReceive('invalidatePath')->once()->with('/cached')
->shouldReceive('invalidatePath')->once()->with(
sprintf('/invalidate/path/%s', $statusCode)
)
->shouldReceive('flush')->once()
;

$client->request('POST', '/invalidate/path');
$client->request('POST', sprintf('/invalidate/path/%s', $statusCode));
}

public function testErrorIsNotInvalidated()
Expand All @@ -65,6 +71,11 @@ public function testErrorIsNotInvalidated()
$client->request('POST', '/invalidate/error');
}

public function getStatusCodesThatTriggerInvalidation()
{
return array(array(200), array(204), array(302));
}

protected function tearDown()
{
static::createClient()->getContainer()->unmock('fos_http_cache.cache_manager');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function itemAction($id)
/**
* @InvalidatePath("/cached")
*/
public function otherAction()
public function otherAction($statusCode)
{
return new Response('Done.');
return new Response('Done.', $statusCode);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions Tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ fos_http_cache:
varnish:
servers: 127.0.0.1
base_url: localhost:8080
invalidation:
rules:
-
match:
path: ^/invalidate/path.*
routes:
invalidation_path: ~
tags:
rules:
-
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Fixtures/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ invalidation_route:
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\InvalidationController::itemAction }

invalidation_path:
path: /invalidate/path
path: /invalidate/path/{statusCode}
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\InvalidationController::otherAction }

invalidation_error:
Expand Down

0 comments on commit ae7aca7

Please sign in to comment.