From 10b01c915909fbd5f9607c468f6b78ea463aa27a Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 2 Jan 2013 22:06:26 +0100 Subject: [PATCH] [HttpFoundation] fix return types and handling of zero in Response --- .../Component/HttpFoundation/Response.php | 16 ++++++++-------- .../HttpFoundation/Tests/ResponseTest.php | 9 +++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 6a04363a54..de84fc20c9 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -614,8 +614,8 @@ class Response */ public function getAge() { - if ($age = $this->headers->get('Age')) { - return $age; + if (null !== $age = $this->headers->get('Age')) { + return (int) $age; } return max(time() - $this->getDate()->format('U'), 0); @@ -691,12 +691,12 @@ class Response */ public function getMaxAge() { - if ($age = $this->headers->getCacheControlDirective('s-maxage')) { - return $age; + if ($this->headers->hasCacheControlDirective('s-maxage')) { + return (int) $this->headers->getCacheControlDirective('s-maxage'); } - if ($age = $this->headers->getCacheControlDirective('max-age')) { - return $age; + if ($this->headers->hasCacheControlDirective('max-age')) { + return (int) $this->headers->getCacheControlDirective('max-age'); } if (null !== $this->getExpires()) { @@ -757,7 +757,7 @@ class Response */ public function getTtl() { - if ($maxAge = $this->getMaxAge()) { + if (null !== $maxAge = $this->getMaxAge()) { return $maxAge - $this->getAge(); } @@ -961,7 +961,7 @@ class Response */ public function hasVary() { - return (Boolean) $this->headers->get('Vary'); + return null !== $this->headers->get('Vary'); } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 38853ecc8c..6ca569c3ea 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -223,7 +223,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $response = new Response(); $response->headers->set('Expires', -1); $response->expire(); - $this->assertEquals(0, $response->headers->get('Age'), '->expire() does not set the Age to 0'); + $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired'); } public function testGetTtl() @@ -237,7 +237,12 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $response = new Response(); $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)); - $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part'); + $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past'); + + $response = new Response(); + $response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822)); + $response->headers->set('Age', 0); + $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero'); $response = new Response(); $response->headers->set('Cache-Control', 'max-age=60');