diff --git a/lib/Cake/Network/Http/Client.php b/lib/Cake/Network/Http/Client.php index e82d56dc109..b6836f82c75 100644 --- a/lib/Cake/Network/Http/Client.php +++ b/lib/Cake/Network/Http/Client.php @@ -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]); + } } } diff --git a/lib/Cake/Test/TestCase/Network/Http/ClientTest.php b/lib/Cake/Test/TestCase/Network/Http/ClientTest.php index 4844ade9162..1f1162095ec 100644 --- a/lib/Cake/Test/TestCase/Network/Http/ClientTest.php +++ b/lib/Cake/Test/TestCase/Network/Http/ClientTest.php @@ -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', @@ -494,6 +495,14 @@ 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 @@ -501,6 +510,7 @@ public function testCookiesWithDomain() { $http->get('/projects'); $http->get('http://cakephp.org/versions'); + $http->get('/projects'); $result = $http->cookies(); $expected = [