Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add new isExpirable() method and consider s-maxage=0 cacheable #22035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public function isCacheable()
return false;
}

return $this->isValidateable() || $this->isFresh();
return $this->isValidateable() || $this->isExpirable();
}

/**
Expand All @@ -547,6 +547,19 @@ public function isFresh()
return $this->getTtl() > 0;
}

/**
* Returns true if the response includes headers that indicate time-based
* content expiration.
*
* @return bool true if the response expires when it reaches a particular age, false otherwise.
*/
public function isExpirable()
{
return $this->headers->hasCacheControlDirective('s-maxage')
|| $this->headers->hasCacheControlDirective('max-age')
|| $this->headers->has('expires');
}

/**
* Returns true if the response includes headers that can be used to validate
* the response with the origin server using a conditional GET request.
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ public function testIsValidateable()
$this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
}

public function testIsExpirable()
{
$response = new Response('', 200);
$response->setSharedMaxAge(100);
$this->assertTrue($response->isExpirable(), '->isExpirable() returns true if a shared-maxage is set');

$response = new Response('', 200);
$response->setMaxAge(100);
$this->assertTrue($response->isExpirable(), '->isExpirable() returns true if a maxage is set');

$response = new Response('', 200);
$response->setExpires($this->createDateTimeOneHourLater());
$this->assertTrue($response->isExpirable(), '->isExpirable() returns true if an Expires date present');

$response = new Response();
$this->assertFalse($response->isExpirable(), '->isExpirable() returns false when no max-age or expire date is present');
}

public function testGetDate()
{
$oneHourAgo = $this->createDateTimeOneHourAgo();
Expand Down Expand Up @@ -304,6 +322,14 @@ public function testGetMaxAge()
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
}

public function testMaxAge0IsStillCacheable()
{
$response = new Response();
$response->setSharedMaxAge(0);

$this->assertTrue($response->isCacheable(), '->isCacheable() returns true even if the Response must always be revalidated');
}

public function testSetSharedMaxAge()
{
$response = new Response();
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,31 @@ public function testHitsCachedResponseWithSMaxAgeDirective()
$this->assertEquals('Hello World', $this->response->getContent());
}

public function testCachesResponseWithSMaxAge0ButRevalidatesIt()
{
$this->setNextResponse(200, array('Cache-Control' => 's-maxage=0'), 'Hello World');

$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('miss');
$this->assertTraceContains('store');
$this->assertEquals(0, $this->response->getAge());
$this->assertEquals('Hello World', $this->response->getContent());

sleep(2);

$this->setNextResponse(304, array('Cache-Control' => 's-maxage=0'));

$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('stale');
$this->assertTraceContains('valid');
$this->assertEquals(0, $this->response->getAge());
$this->assertEquals('Hello World', $this->response->getContent());
}

public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
{
$this->setNextResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
Expand All @@ -25,6 +26,8 @@ class HttpCacheTestCase extends TestCase
protected $caches;
protected $cacheConfig;
protected $request;

/** @var Response */
protected $response;
protected $responses;
protected $catch;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.9",
"symfony/event-dispatcher": "^2.6.7|~3.0.0",
"symfony/http-foundation": "~2.7.20|~2.8.13|~3.1.6",
"symfony/http-foundation": "^2.8.19|~3.1.6",
"symfony/debug": "^2.6.2",
"psr/log": "~1.0"
},
Expand Down