Skip to content

Commit

Permalink
Fix Http\Client\CookieCollection according to RFC6265.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka committed Mar 14, 2017
1 parent 9a4c7d1 commit 6b1a8ff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/Http/Client/CookieCollection.php
Expand Up @@ -92,15 +92,15 @@ public function get($url)
continue;
}
$leadingDot = $cookie['domain'][0] === '.';
if (!$leadingDot && $host !== $cookie['domain']) {
continue;
}
if ($leadingDot) {
$pattern = '/' . preg_quote(substr($cookie['domain'], 1), '/') . '$/';
if (!preg_match($pattern, $host)) {
continue;
}
$cookie['domain'] = ltrim($cookie['domain'], '.');
}

$pattern = '/' . preg_quote(substr($cookie['domain'], 1), '/') . '$/';
if (!preg_match($pattern, $host)) {
continue;
}

$out[$cookie['name']] = $cookie['value'];
}

Expand Down
38 changes: 37 additions & 1 deletion tests/TestCase/Http/Client/CookieCollectionTest.php
Expand Up @@ -226,7 +226,7 @@ public function testGetMatchingDomain()
{
$headers = [
'HTTP/1.0 200 Ok',
'Set-Cookie: first=1; Domain=.example.com',
'Set-Cookie: first=1; Domain=example.com',
'Set-Cookie: second=2;',
];
$response = new Response($headers, '');
Expand All @@ -241,9 +241,45 @@ public function testGetMatchingDomain()
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://bar.foo.example.com');
$expected = ['first' => 1, 'second' => '2'];
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://api.example.com');
$expected = ['first' => 1];
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://google.com');
$expected = [];
$this->assertEquals($expected, $result);
}

/**
* Test getting cookies matching on paths exactly
*
* @return void
*/
public function testGetMatchingDomainWithDot()
{
$headers = [
'HTTP/1.0 200 Ok',
'Set-Cookie: first=1; Domain=.example.com',
'Set-Cookie: second=2;',
];
$response = new Response($headers, '');
$this->cookies->store($response, 'http://foo.example.com/');

$result = $this->cookies->get('http://example.com');
$expected = ['first' => 1];
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://foo.example.com');
$expected = ['first' => 1, 'second' => '2'];
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://bar.foo.example.com');
$expected = ['first' => 1, 'second' => '2'];
$this->assertEquals($expected, $result);

$result = $this->cookies->get('http://api.example.com');
$expected = ['first' => 1];
$this->assertEquals($expected, $result);
Expand Down

0 comments on commit 6b1a8ff

Please sign in to comment.