Skip to content

Commit

Permalink
feat(chromium): set cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Apr 11, 2024
1 parent 9a103a7 commit 7c6726e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,22 @@ $request = Gotenberg::chromium($apiUrl)
->url('https://my.url');
```

#### Cookies

You may add ccookies to store in the Chromium cookie jar:

```php
use Gotenberg\Gotenberg;

$request = Gotenberg::chromium($apiUrl)
->pdf()
->cookies([
new ChromiumCookie(name: 'yummy_cookie', value: 'choco', domain: 'theyummycookie.com'),
new ChromiumCookie(name: 'vanilla_cookie', value: 'vanilla', domain: 'theyummycookie.com', path: '/', secure: true, httpOnly: true, sameSite: 'Lax'),
])
->url('https://my.url');
```

#### Extra HTTP headers

You may add HTTP headers that Chromium will send when loading the HTML document:
Expand Down
19 changes: 19 additions & 0 deletions src/Modules/ChromiumCookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Gotenberg\Modules;

class ChromiumCookie
{
public function __construct(
public readonly string $name,
public readonly string $value,
public readonly string $domain,
public readonly string|null $path = null,
public readonly bool|null $secure = null,
public readonly bool|null $httpOnly = null,
public readonly string|null $sameSite = null,
) {
}
}
19 changes: 19 additions & 0 deletions src/Modules/ChromiumMultipartFormDataModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ public function emulateScreenMediaType(): self
return $this;
}

/**
* Cookies to store in the Chromium cookie jar.
*
* @param ChromiumCookie[] $cookies
*
* @throws NativeFunctionErrored
*/
public function cookies(array $cookies): self
{
$json = json_encode($cookies);
if ($json === false) {
throw NativeFunctionErrored::createFromLastPhpError();
}

$this->formValue('cookies', $json);

return $this;
}

/**
* Sets extra HTTP headers that Chromium will send when loading the HTML
* document.
Expand Down
42 changes: 42 additions & 0 deletions tests/Modules/ChromiumPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\Gotenberg;
use Gotenberg\Modules\ChromiumCookie;
use Gotenberg\Modules\ChromiumPdf;
use Gotenberg\Stream;

it(
'creates a valid request for the "/forms/chromium/convert/url" endpoint',
/**
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
Expand All @@ -35,6 +37,7 @@ function (
string|null $waitDelay = null,
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -65,6 +68,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -101,6 +105,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -134,6 +139,10 @@ function (
'1s',
"window.status === 'ready'",
'print',
[
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
[
'My-Http-Header' => 'HTTP Header content',
'My-Second-Http-Header' => 'Second HTTP Header content',
Expand All @@ -153,6 +162,7 @@ function (
it(
'creates a valid request for the "/forms/chromium/convert/html" endpoint',
/**
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
Expand All @@ -178,6 +188,7 @@ function (
string|null $waitDelay = null,
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -208,6 +219,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -246,6 +258,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -278,6 +291,10 @@ function (
'1s',
"window.status === 'ready'",
'screen',
[
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand All @@ -297,6 +314,7 @@ function (
it(
'creates a valid request for the "/forms/chromium/convert/markdown" endpoint',
/**
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param Stream[] $markdowns
Expand Down Expand Up @@ -324,6 +342,7 @@ function (
string|null $waitDelay = null,
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -354,6 +373,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -397,6 +417,7 @@ function (
$waitDelay,
$waitForExpression,
$emulatedMediaType,
$cookies,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -438,6 +459,10 @@ function (
'1s',
"window.status === 'ready'",
'screen',
[
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand All @@ -455,6 +480,7 @@ function (
]);

/**
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
Expand All @@ -480,6 +506,7 @@ function hydrateChromiumPdfFormData(
string|null $waitDelay = null,
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -549,6 +576,10 @@ function hydrateChromiumPdfFormData(
$chromium->emulateScreenMediaType();
}

if (count($cookies) > 0) {
$chromium->cookies($cookies);
}

if (count($extraHttpHeaders) > 0) {
$chromium->extraHttpHeaders($extraHttpHeaders);
}
Expand Down Expand Up @@ -585,6 +616,7 @@ function hydrateChromiumPdfFormData(
}

/**
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
Expand All @@ -610,6 +642,7 @@ function expectChromiumPdfOptions(
string|null $waitDelay,
string|null $waitForExpression,
string|null $emulatedMediaType,
array $cookies,
array $extraHttpHeaders,
array $failOnHttpStatusCodes,
bool $failOnConsoleExceptions,
Expand Down Expand Up @@ -656,6 +689,15 @@ function expectChromiumPdfOptions(
expect($body)->unless($waitForExpression === null, fn ($body) => $body->toContainFormValue('waitForExpression', $waitForExpression));
expect($body)->unless($emulatedMediaType === null, fn ($body) => $body->toContainFormValue('emulatedMediaType', $emulatedMediaType));

if (count($cookies) > 0) {
$json = json_encode($cookies);
if ($json === false) {
throw NativeFunctionErrored::createFromLastPhpError();
}

expect($body)->toContainFormValue('cookies', $json);
}

if (count($extraHttpHeaders) > 0) {
$json = json_encode($extraHttpHeaders);
if ($json === false) {
Expand Down
Loading

0 comments on commit 7c6726e

Please sign in to comment.