From 1ae1d8688c1ab5bea21c34b7a7a77776e48c67f9 Mon Sep 17 00:00:00 2001 From: David Schoen Date: Fri, 16 Feb 2018 21:23:26 +1100 Subject: [PATCH] Fix bug parsing 0 epoch expiry times As per https://github.com/guzzle/guzzle/issues/1952 expiry times specified as "Thu, 01 Jan 1970 00:00:00 GMT" should be treated as expired, but they weren't because 0 was implicitly casting to a bool in SetCookie::isExpired(). --- src/Cookie/SetCookie.php | 2 +- tests/Cookie/SetCookieTest.php | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Cookie/SetCookie.php b/src/Cookie/SetCookie.php index ac9a89081..6fdee0dd3 100644 --- a/src/Cookie/SetCookie.php +++ b/src/Cookie/SetCookie.php @@ -333,7 +333,7 @@ public function matchesDomain($domain) */ public function isExpired() { - return $this->getExpires() && time() > $this->getExpires(); + return $this->getExpires() !== null && time() > $this->getExpires(); } /** diff --git a/tests/Cookie/SetCookieTest.php b/tests/Cookie/SetCookieTest.php index 3ddd08201..e1229212d 100644 --- a/tests/Cookie/SetCookieTest.php +++ b/tests/Cookie/SetCookieTest.php @@ -361,4 +361,45 @@ public function testParseCookie($cookie, $parsed) } } } + + /** + * Provides the data for testing isExpired + * + * @return array + */ + public function isExpiredProvider() { + return array( + array( + 'FOO=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT;', + true, + ), + array( + 'FOO=bar; expires=Thu, 01 Jan 1970 00:00:01 GMT;', + true, + ), + array( + 'FOO=bar; expires='.date(\DateTime::RFC1123, time()+10).';', + false, + ), + array( + 'FOO=bar; expires='.date(\DateTime::RFC1123, time()-10).';', + true, + ), + array( + 'FOO=bar;', + false, + ), + ); + } + + /** + * @dataProvider isExpiredProvider + */ + public function testIsExpired($cookie, $expired) + { + $this->assertEquals( + $expired, + SetCookie::fromString($cookie)->isExpired() + ); + } }