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

Skip to content

Commit 8c28317

Browse files
committed
[HttpFoundation] Compute cookie max-age attribute
1 parent e98c068 commit 8c28317

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public function __toString()
9494
$str = urlencode($this->getName()).'=';
9595

9696
if ('' === (string) $this->getValue()) {
97-
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
97+
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001';
9898
} else {
9999
$str .= urlencode($this->getValue());
100100

101101
if ($this->getExpiresTime() !== 0) {
102-
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
102+
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; max-age='.$this->getMaxAge();
103103
}
104104
}
105105

@@ -166,6 +166,16 @@ public function getExpiresTime()
166166
return $this->expire;
167167
}
168168

169+
/**
170+
* Gets the max-age attribute.
171+
*
172+
* @return int
173+
*/
174+
public function getMaxAge()
175+
{
176+
return 0 !== $this->expire ? $this->expire - time() : 0;
177+
}
178+
169179
/**
170180
* Gets the path on the server in which the cookie will be available on.
171181
*

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ public function testGetPath()
7272

7373
public function testGetExpiresTime()
7474
{
75-
$cookie = new Cookie('foo', 'bar', 3600);
75+
$cookie = new Cookie('foo', 'bar');
76+
77+
$this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
78+
79+
$cookie = new Cookie('foo', 'bar', $expire = time() + 3600);
7680

77-
$this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
81+
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
7882
}
7983

8084
public function testConstructorWithDateTime()
@@ -107,21 +111,21 @@ public function testGetExpiresTimeWithStringValue()
107111

108112
public function testGetDomain()
109113
{
110-
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
114+
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com');
111115

112116
$this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
113117
}
114118

115119
public function testIsSecure()
116120
{
117-
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
121+
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', true);
118122

119123
$this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
120124
}
121125

122126
public function testIsHttpOnly()
123127
{
124-
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
128+
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
125129

126130
$this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
127131
}
@@ -142,14 +146,14 @@ public function testCookieIsCleared()
142146

143147
public function testToString()
144148
{
145-
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
146-
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
149+
$cookie = new Cookie('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
150+
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
147151

148152
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
149-
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
153+
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; max-age='.($expire - time()).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
150154

151155
$cookie = new Cookie('foo', 'bar', 0, '/', '');
152-
$this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
156+
$this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
153157
}
154158

155159
public function testRawCookie()
@@ -160,4 +164,16 @@ public function testRawCookie()
160164
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true);
161165
$this->assertTrue($cookie->isRaw());
162166
}
167+
168+
public function testGetMaxAge()
169+
{
170+
$cookie = new Cookie('foo', 'bar');
171+
$this->assertEquals(0, $cookie->getMaxAge());
172+
173+
$cookie = new Cookie('foo', 'bar', $expire = time() + 100);
174+
$this->assertEquals($expire - time(), $cookie->getMaxAge());
175+
176+
$cookie = new Cookie('foo', 'bar', $expire = time() - 100);
177+
$this->assertEquals($expire - time(), $cookie->getMaxAge());
178+
}
163179
}

src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function testToStringIncludesCookieHeaders()
128128

129129
$bag->clearCookie('foo');
130130

131-
$this->assertRegExp('#^Set-Cookie: foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly#m', $bag->__toString());
131+
$this->assertRegExp('#^Set-Cookie: foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; httponly#m', $bag->__toString());
132132
}
133133

134134
public function testClearCookieSecureNotHttpOnly()
@@ -137,7 +137,7 @@ public function testClearCookieSecureNotHttpOnly()
137137

138138
$bag->clearCookie('foo', '/', null, true, false);
139139

140-
$this->assertRegExp('#^Set-Cookie: foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure#m', $bag->__toString());
140+
$this->assertRegExp('#^Set-Cookie: foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; secure#m', $bag->__toString());
141141
}
142142

143143
public function testReplace()

0 commit comments

Comments
 (0)