Skip to content

Commit 5496bf2

Browse files
authored
Merge pull request #104 from gnat42/patch-1
Fix Cookie handling as per RFC 6265 Section 5.4
2 parents 0b9ce65 + 87aab1d commit 5496bf2

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 1.8.2 (unreleased)
4+
5+
### Changed
6+
7+
- When multiple cookies exist, a single header with all cookies is sent as per RFC 6265 Section 5.4
8+
39
## 1.8.1 - 2018-10-09
410

511
### Fixed

spec/Plugin/CookiePluginSpec.php

+21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ function it_loads_cookie(RequestInterface $request, UriInterface $uri, Promise $
5151
}, function () {});
5252
}
5353

54+
function it_combines_multiple_cookies_into_one_header(RequestInterface $request, UriInterface $uri, Promise $promise)
55+
{
56+
$cookie = new Cookie('name', 'value', 86400, 'test.com');
57+
$cookie2 = new Cookie('name2', 'value2', 86400, 'test.com');
58+
59+
$this->cookieJar->addCookie($cookie);
60+
$this->cookieJar->addCookie($cookie2);
61+
62+
$request->getUri()->willReturn($uri);
63+
$uri->getHost()->willReturn('test.com');
64+
$uri->getPath()->willReturn('/');
65+
66+
$request->withAddedHeader('Cookie', 'name=value; name2=value2')->willReturn($request);
67+
68+
$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
69+
if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) {
70+
return $promise->getWrappedObject();
71+
}
72+
}, function () {});
73+
}
74+
5475
function it_does_not_load_cookie_if_expired(RequestInterface $request, UriInterface $uri, Promise $promise)
5576
{
5677
$cookie = new Cookie('name', 'value', null, 'test.com', false, false, null, (new \DateTime())->modify('-1 day'));

src/Plugin/CookiePlugin.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(CookieJar $cookieJar)
3838
*/
3939
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4040
{
41+
$cookies = [];
4142
foreach ($this->cookieJar->getCookies() as $cookie) {
4243
if ($cookie->isExpired()) {
4344
continue;
@@ -55,7 +56,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
5556
continue;
5657
}
5758

58-
$request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue()));
59+
$cookies[] = sprintf('%s=%s', $cookie->getName(), $cookie->getValue());
60+
}
61+
62+
if (!empty($cookies)) {
63+
$request = $request->withAddedHeader('Cookie', implode('; ', array_unique($cookies)));
5964
}
6065

6166
return $next($request)->then(function (ResponseInterface $response) use ($request) {

0 commit comments

Comments
 (0)