Skip to content

Commit 52eb427

Browse files
author
Peter Bouwdewijn
committed
Fix #67 Throw transfer exception when the cookie expires header can not be parsed.
Add test with invalid exprires date content. Remove obsolete sprintf statement in test.
1 parent 008ff68 commit 52eb427

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

spec/CookiePluginSpec.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ function it_saves_cookie(RequestInterface $request, ResponseInterface $response,
146146

147147
$response->hasHeader('Set-Cookie')->willReturn(true);
148148
$response->getHeader('Set-Cookie')->willReturn([
149-
sprintf(
150-
'cookie=value; expires=Tuesday, 31-Mar-99 07:42:12 GMT; Max-Age=60; path=/; domain=test.com; secure; HttpOnly'
151-
)
149+
'cookie=value; expires=Tuesday, 31-Mar-99 07:42:12 GMT; Max-Age=60; path=/; domain=test.com; secure; HttpOnly'
152150
]);
153151

154152
$request->getUri()->willReturn($uri);
@@ -159,4 +157,24 @@ function it_saves_cookie(RequestInterface $request, ResponseInterface $response,
159157
$promise->shouldHaveType('Http\Promise\Promise');
160158
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
161159
}
160+
161+
function it_throws_exception_on_invalid_expires_date(RequestInterface $request, ResponseInterface $response, UriInterface $uri)
162+
{
163+
$next = function () use ($response) {
164+
return new FulfilledPromise($response->getWrappedObject());
165+
};
166+
167+
$response->hasHeader('Set-Cookie')->willReturn(true);
168+
$response->getHeader('Set-Cookie')->willReturn([
169+
'cookie=value; expires=i-am-an-invalid-date;'
170+
]);
171+
172+
$request->getUri()->willReturn($uri);
173+
$uri->getHost()->willReturn('test.com');
174+
$uri->getPath()->willReturn('/');
175+
176+
$promise = $this->handleRequest($request, $next, function () {});
177+
$promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
178+
$promise->shouldThrow('Http\Client\Exception\TransferException')->duringWait();
179+
}
162180
}

src/CookiePlugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Client\Plugin;
44

5+
use Http\Client\Exception\TransferException;
56
use Http\Message\Cookie;
67
use Http\Message\CookieJar;
78
use Psr\Http\Message\RequestInterface;
@@ -86,6 +87,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
8687
* @param $setCookie
8788
*
8889
* @return Cookie|null
90+
* @throws \Http\Client\Exception\TransferException
8991
*/
9092
private function createCookie(RequestInterface $request, $setCookie)
9193
{
@@ -111,6 +113,17 @@ private function createCookie(RequestInterface $request, $setCookie)
111113
switch (strtolower($key)) {
112114
case 'expires':
113115
$expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value);
116+
117+
if (true !== ($expires instanceof \DateTime)) {
118+
throw new TransferException(
119+
sprintf(
120+
'Cookie header `%s` expires value `%s` could not be converted to date',
121+
$name,
122+
$value
123+
)
124+
);
125+
}
126+
114127
break;
115128

116129
case 'max-age':

0 commit comments

Comments
 (0)