Skip to content

Commit 08b4ad9

Browse files
committed
Manually apply php-http/plugins#68
1 parent 6404570 commit 08b4ad9

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

spec/Plugin/CookiePluginSpec.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,27 @@ 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(
162+
RequestInterface $request,
163+
ResponseInterface $response,
164+
UriInterface $uri
165+
) {
166+
$next = function () use ($response) {
167+
return new FulfilledPromise($response->getWrappedObject());
168+
};
169+
170+
$response->hasHeader('Set-Cookie')->willReturn(true);
171+
$response->getHeader('Set-Cookie')->willReturn([
172+
'cookie=value; expires=i-am-an-invalid-date;'
173+
]);
174+
175+
$request->getUri()->willReturn($uri);
176+
$uri->getHost()->willReturn('test.com');
177+
$uri->getPath()->willReturn('/');
178+
179+
$promise = $this->handleRequest($request, $next, function () {});
180+
$promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
181+
$promise->shouldThrow('Http\Client\Exception\TransferException')->duringWait();
182+
}
160183
}

src/Plugin/CookiePlugin.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Client\Common\Plugin;
44

55
use Http\Client\Common\Plugin;
6+
use Http\Client\Exception\TransferException;
67
use Http\Message\Cookie;
78
use Http\Message\CookieJar;
89
use Psr\Http\Message\RequestInterface;
@@ -87,6 +88,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
8788
* @param $setCookie
8889
*
8990
* @return Cookie|null
91+
*
92+
* @throws TransferException
9093
*/
9194
private function createCookie(RequestInterface $request, $setCookie)
9295
{
@@ -98,7 +101,8 @@ private function createCookie(RequestInterface $request, $setCookie)
98101

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

101-
$expires = 0;
104+
$maxAge = null;
105+
$expires = null;
102106
$domain = $request->getUri()->getHost();
103107
$path = $request->getUri()->getPath();
104108
$secure = false;
@@ -110,11 +114,21 @@ private function createCookie(RequestInterface $request, $setCookie)
110114

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

116130
case 'max-age':
117-
$expires = (new \DateTime())->add(new \DateInterval('PT'.(int) $value.'S'));
131+
$maxAge = (int) $value;
118132
break;
119133

120134
case 'domain':
@@ -135,7 +149,7 @@ private function createCookie(RequestInterface $request, $setCookie)
135149
}
136150
}
137151

138-
return new Cookie($name, $cookieValue, $expires, $domain, $path, $secure, $httpOnly);
152+
return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires);
139153
}
140154

141155
/**

0 commit comments

Comments
 (0)