Skip to content

Commit

Permalink
fixing cookie expiration math
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Sep 30, 2024
1 parent 57892ad commit 76528ea
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/behaviors/TestableResponseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace markhuot\craftpest\behaviors;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use markhuot\craftpest\dom\Form;
use markhuot\craftpest\dom\NodeList;
Expand Down Expand Up @@ -267,7 +268,13 @@ public function assertCookieExpired(string $name)

// Then check the expiration of it
$cookie = $this->response->cookies->get($name);
if ($cookie->expire === 0 || $cookie->expire >= time()) {

if ($cookie->expire === null) {
Assert::fail('Cookie `'.$name.'` does not have an expiration date.');
}

$expiration = Carbon::parse($cookie->expire);
if ($expiration->isAfter(Carbon::now())) {
Assert::fail('Cookie `'.$name.'` does not have an expiration in the past.');
}

Expand All @@ -288,8 +295,14 @@ public function assertCookieNotExpired(string $name)

// Then check the expiration of it
$cookie = $this->response->cookies->get($name);
if ($cookie->expire !== 0 && $cookie->expire < time()) {
Assert::fail('Cookie `'.$name.'` does not have an expiration in the future.');

if ($cookie->expire === null) {
Assert::fail('Cookie `'.$name.'` does not have an expiration date.');
}

$expiration = Carbon::parse($cookie->expire);
if ($expiration->isAfter(Carbon::now())) {
Assert::fail('Cookie `'.$name.'` does not have an expiration in the past.');
}

return $this->response;
Expand Down
3 changes: 2 additions & 1 deletion src/http/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public function addHeader(string $name, $value): self
return $this;
}

public function addCookie(string $key, $value): self
public function addCookie(string $key, $value, $expire=0): self
{
$this->request->cookies->add(new Cookie([
'name' => $key,
'value' => $value,
'expire' => $expire,
]));

return $this;
Expand Down
5 changes: 4 additions & 1 deletion src/test/CookieState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace markhuot\craftpest\test;

use Carbon\Carbon;
use yii\web\Cookie;
use yii\web\CookieCollection;

Expand Down Expand Up @@ -94,7 +95,9 @@ public function storeCookieCollection(?CookieCollection $cookies)
// We have to manually clear our expired cookies because this is normally handled
// by the browser for us
foreach ($this->cookies as $cookie) {
if ($cookie->expire !== 0 && $cookie->expire < time()) {
$expiration = Carbon::parse($cookie->expire);

if ($cookie->expire !== 0 && $expiration->isBefore(Carbon::now())) {
$this->cookies->remove($cookie, false);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
expect(trim($content))->toBe(json_encode(['theName' => 'theValue']));
});

it('tests expired cookies', function () {
$this->get('responses/expired-cookie')
->assertCookieExpired('cookieName');
});

it('retains cookies', function () {
$this->get('response-test')
->assertOk()
Expand Down
6 changes: 6 additions & 0 deletions tests/templates/responses/expired-cookie.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% set cookie = create('yii\\web\\Cookie', {config: {
name: 'cookieName',
value: 'cookieValue',
expire: 0,
}}) %}
{% do craft.app.response.cookies.add(cookie) %}

0 comments on commit 76528ea

Please sign in to comment.