Skip to content

Commit 9395b4f

Browse files
committed
Merge pull request #68 from Bouwdie/master
2 parents 741f0c4 + 7f038d4 commit 9395b4f

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

spec/CookiePluginSpec.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace spec\Http\Client\Plugin;
44

5-
use Http\Promise\FulfilledPromise;
65
use Http\Message\Cookie;
76
use Http\Message\CookieJar;
7+
use Http\Promise\FulfilledPromise;
88
use Http\Promise\Promise;
9+
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
911
use Psr\Http\Message\RequestInterface;
1012
use Psr\Http\Message\ResponseInterface;
1113
use Psr\Http\Message\UriInterface;
12-
use PhpSpec\ObjectBehavior;
13-
use Prophecy\Argument;
1414

1515
class CookiePluginSpec extends ObjectBehavior
1616
{
@@ -146,7 +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-
'cookie=value',
149+
'cookie=value; expires=Tuesday, 31-Mar-99 07:42:12 GMT; Max-Age=60; path=/; domain=test.com; secure; HttpOnly'
150150
]);
151151

152152
$request->getUri()->willReturn($uri);
@@ -157,4 +157,24 @@ function it_saves_cookie(RequestInterface $request, ResponseInterface $response,
157157
$promise->shouldHaveType('Http\Promise\Promise');
158158
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
159159
}
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+
}
160180
}

src/CookiePlugin.php

Lines changed: 19 additions & 4 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,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
8687
* @param $setCookie
8788
*
8889
* @return Cookie|null
90+
*
91+
* @throws \Http\Client\Exception\TransferException
8992
*/
9093
private function createCookie(RequestInterface $request, $setCookie)
9194
{
@@ -97,7 +100,8 @@ private function createCookie(RequestInterface $request, $setCookie)
97100

98101
list($name, $cookieValue) = $this->createValueKey(array_shift($parts));
99102

100-
$expires = 0;
103+
$maxAge = null;
104+
$expires = null;
101105
$domain = $request->getUri()->getHost();
102106
$path = $request->getUri()->getPath();
103107
$secure = false;
@@ -109,11 +113,22 @@ private function createCookie(RequestInterface $request, $setCookie)
109113

110114
switch (strtolower($key)) {
111115
case 'expires':
112-
$expires = \DateTime::createFromFormat(DATE_COOKIE, $value);
116+
$expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value);
117+
118+
if (true !== ($expires instanceof \DateTime)) {
119+
throw new TransferException(
120+
sprintf(
121+
'Cookie header `%s` expires value `%s` could not be converted to date',
122+
$name,
123+
$value
124+
)
125+
);
126+
}
127+
113128
break;
114129

115130
case 'max-age':
116-
$expires = (new \DateTime())->add(new \DateInterval('PT'.(int) $value.'S'));
131+
$maxAge = (int) $value;
117132
break;
118133

119134
case 'domain':
@@ -134,7 +149,7 @@ private function createCookie(RequestInterface $request, $setCookie)
134149
}
135150
}
136151

137-
return new Cookie($name, $cookieValue, $expires, $domain, $path, $secure, $httpOnly);
152+
return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires);
138153
}
139154

140155
/**

0 commit comments

Comments
 (0)