Skip to content

Commit

Permalink
Fix converting chrome-php cookie objects
Browse files Browse the repository at this point in the history
Fixes issue when converting cookie objects received from the chrome-php
library.
  • Loading branch information
otsch committed Oct 22, 2024
1 parent a92789f commit 208b29f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.1.2] - 2024-10-22
### Fixed
* Issue when converting cookie objects received from the chrome-php library.

## [2.1.1] - 2024-10-21
### Fixed
* Also add cookies, set during headless browser usage, to the cookie jar. When switching back to the (guzzle) HTTP client the cookies should also be sent.
Expand Down
7 changes: 4 additions & 3 deletions src/Loader/Http/Cookies/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Crwlr\Url\Url;
use DateTime;
use Exception;
use HeadlessChromium\Cookies\Cookie as BrowserCookie;
use HeadlessChromium\Cookies\CookiesCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -120,7 +121,7 @@ protected function getForDomainFromUrl(string|UriInterface|Url $url): ?string
return $forDomain;
}

protected function buildSetCookieHeaderFromBrowserCookie(\HeadlessChromium\Cookies\Cookie $cookie): string
protected function buildSetCookieHeaderFromBrowserCookie(BrowserCookie $cookie): string
{
$header = $cookie->getName() . '=' . $cookie->getValue();

Expand All @@ -140,8 +141,8 @@ protected function buildSetCookieHeaderFromBrowserCookie(\HeadlessChromium\Cooki
$header .= '; Path=' . $cookie->offsetGet('path');
}

if ($cookie->offsetExists('secure') && !empty($cookie->offsetGet('secure'))) {
$header .= '; Secure=' . $cookie->offsetGet('path');
if ($cookie->offsetExists('secure') && $cookie->offsetGet('secure') === true) {
$header .= '; Secure';
}

if ($cookie->offsetExists('httpOnly') && $cookie->offsetGet('httpOnly') === true) {
Expand Down
42 changes: 39 additions & 3 deletions tests/Loader/Http/Cookies/CookieJarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,52 @@
$jar = new CookieJar();

$jar->addFrom(Url::parse('https://www.crwl.io'), new CookiesCollection([
new Cookie(['name' => 'foo', 'value' => 'one', 'domain' => '.www.crwl.io', 'expires' => '1745068860']),
new Cookie(['name' => 'bar', 'value' => 'two', 'domain' => '.www.crwl.io', 'expires' => '1729603260.5272']),
new Cookie(['name' => 'baz', 'value' => 'three', 'domain' => '.www.crwl.io', 'expires' => '1764076860.878']),
new Cookie([
'name' => 'foo',
'value' => 'one',
'domain' => '.www.crwl.io',
'expires' => '1745068860',
'max-age' => '86400',
'secure' => true,
'httpOnly' => true,
'sameSite' => 'Strict',
]),
new Cookie([
'name' => 'bar',
'value' => 'two',
'domain' => '.www.crwl.io',
'expires' => '1729603260.5272',
'path' => '/bar',
]),
new Cookie([
'name' => 'baz',
'value' => 'three',
'domain' => '.www.crwl.io',
'expires' => '1764076860.878',
]),
]));

$allCookiesForDomain = $jar->allByDomain('crwl.io');

expect($allCookiesForDomain)->toHaveCount(3)
->and($allCookiesForDomain['foo']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2025-04-19 13:21')
->and($allCookiesForDomain['foo']->name())->toBe('foo')
->and($allCookiesForDomain['foo']->value())->toBe('one')
->and($allCookiesForDomain['foo']->domain())->toBe('www.crwl.io')
->and($allCookiesForDomain['foo']->maxAge())->toBe(86400)
->and($allCookiesForDomain['foo']->path())->toBeNull()
->and($allCookiesForDomain['foo']->secure())->toBeTrue()
->and($allCookiesForDomain['foo']->httpOnly())->toBeTrue()
->and($allCookiesForDomain['foo']->sameSite())->toBe('Strict')
->and($allCookiesForDomain['bar']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2024-10-22 13:21')
->and($allCookiesForDomain['bar']->name())->toBe('bar')
->and($allCookiesForDomain['bar']->value())->toBe('two')
->and($allCookiesForDomain['bar']->domain())->toBe('www.crwl.io')
->and($allCookiesForDomain['bar']->maxAge())->toBeNull()
->and($allCookiesForDomain['bar']->path())->toBe('/bar')
->and($allCookiesForDomain['bar']->secure())->toBeFalse()
->and($allCookiesForDomain['bar']->httpOnly())->toBeFalse()
->and($allCookiesForDomain['bar']->sameSite())->toBe('Lax')
->and($allCookiesForDomain['baz']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2025-11-25 13:21');
});

Expand Down

0 comments on commit 208b29f

Please sign in to comment.