Skip to content

Commit

Permalink
[HttpFoundation] standardized cookie paths (an empty path is equivale…
Browse files Browse the repository at this point in the history
…nt to /)
  • Loading branch information
fabpot committed Sep 28, 2011
1 parent 1284681 commit b402835
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
$this->value = $value;
$this->domain = $domain;
$this->expire = $expire;
$this->path = $path;
$this->path = empty($path) ? '/' : $path;

This comment has been minimized.

Copy link
@staabm

staabm May 15, 2018

Contributor

The php-src native setcookie defaults to the „current directory“ instead of /

Was this intentionally made a different way and if so, why?

$this->secure = (Boolean) $secure;
$this->httpOnly = (Boolean) $httpOnly;
}
Expand All @@ -90,8 +90,8 @@ public function __toString()
}
}

if ('/' !== $this->getPath()) {
$str .= '; path='.$this->getPath();
if ('/' !== $this->path) {
$str .= '; path='.$this->path;
}

if (null !== $this->getDomain()) {
Expand Down Expand Up @@ -166,7 +166,7 @@ public function getExpiresTime()
*/
public function getPath()
{
return null === $this->path ? '/' : $this->path;
return $this->path;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public function testToString()
{
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);

$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');
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');

$cookie = new Cookie('foo', null, 1, '/', '.myfoodomain.com');
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');

$this->assertEquals('foo=deleted; expires=' . gmdate("D, d-M-Y H:i:s T", time()-31536001) . '; path=/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
$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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testToStringIncludesCookieHeaders()
$bag = new ResponseHeaderBag(array());
$bag->setCookie(new Cookie('foo', 'bar'));

$this->assertContains("Set-Cookie: foo=bar; path=/; httponly", explode("\r\n", $bag->__toString()));
$this->assertContains("Set-Cookie: foo=bar; httponly", explode("\r\n", $bag->__toString()));

$bag->clearCookie('foo');

Expand All @@ -90,7 +90,7 @@ public function testCookiesWithSameNames()
$this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/path/bar; domain=bar.foo; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; httponly", $headers);

$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function testLogout()
$this->assertEquals('foo.foo', $cookie->getDomain());
$this->assertTrue($cookie->isCleared());

$cookie = $cookies['']['']['foo2'];
$cookie = $cookies['']['/']['foo2'];
$this->assertStringStartsWith('foo2', $cookie->getName());
$this->assertNull($cookie->getPath());
$this->assertEquals('/', $cookie->getPath());
$this->assertNull($cookie->getDomain());
$this->assertTrue($cookie->isCleared());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function testLogoutSimplyIgnoresNonSetRequestCookie()

$cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
$this->assertTrue($cookie->isCleared());
$this->assertNull($cookie->getPath());
$this->assertEquals('/', $cookie->getPath());
$this->assertNull($cookie->getDomain());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function testLogout()

$cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
$this->assertTrue($cookie->isCleared());
$this->assertNull($cookie->getPath());
$this->assertEquals('/', $cookie->getPath());
$this->assertNull($cookie->getDomain());
}

Expand Down

0 comments on commit b402835

Please sign in to comment.