Skip to content

Commit

Permalink
Fix cookies on subdomains.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 4, 2013
1 parent 29ef4f3 commit bc9940a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/Cake/Network/Http/Client.php
Expand Up @@ -342,14 +342,19 @@ protected function _storeCookies(Response $response, $host) {
* Adds cookies stored in the client to the request.
*
* Uses the request's host to find matching cookies.
* Walks backwards through subdomains to find cookies
* defined on parent domains.
*
* @param Request $request
* @return void
*/
protected function _addCookies(Request $request) {
$host = parse_url($request->url(), PHP_URL_HOST);
if (isset($this->_cookies[$host])) {
$request->cookie($this->_cookies[$host]);
$parts = explode('.', parse_url($request->url(), PHP_URL_HOST));
for ($i = 0, $len = count($parts); $i < $len - 1; $i++) {
$host = implode('.', array_slice($parts, $i));
if (isset($this->_cookies[$host])) {
$request->cookie($this->_cookies[$host]);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/ClientTest.php
Expand Up @@ -476,6 +476,7 @@ public function testCookiesWithDomain() {
'Set-Cookie: third=3',
];
$secondResponse = new Response($secondHeaders, '');
$thirdResponse = new Response([], '');

$mock = $this->getMock(
'Cake\Network\Http\Adapter\Stream',
Expand All @@ -494,13 +495,22 @@ public function testCookiesWithDomain() {
))
->will($this->returnValue([$secondResponse]));

$mock->expects($this->at(2))
->method('send')
->with($this->attributeEqualTo(
'_cookies',
['first' => '1', 'second' => '2', 'third' => '3']
))
->will($this->returnValue([$thirdResponse]));

$http = new Client([
'host' => 'test.cakephp.org',
'adapter' => $mock
]);

$http->get('/projects');
$http->get('http://cakephp.org/versions');
$http->get('/projects');

$result = $http->cookies();
$expected = [
Expand Down

0 comments on commit bc9940a

Please sign in to comment.