Skip to content

Commit

Permalink
feat(chromium): add userAgent method
Browse files Browse the repository at this point in the history
  • Loading branch information
meetic-julienneuhart committed Jun 13, 2024
1 parent a7e7489 commit da5b899
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/coverage.xml
/.phpcs-cache
/.phpunit.result.cache
/composer.lock
/composer.lock
/.phpunit.cache/
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ $request = Gotenberg::chromium($apiUrl)
->url('https://my.url');
```

#### Extra HTTP headers
#### User Agent

You may add HTTP headers that Chromium will send when loading the HTML document:
You may overridethe default *User-Agent* HTTP header with:

```php
use Gotenberg\Gotenberg;
Expand All @@ -583,6 +583,19 @@ $request = Gotenberg::chromium($apiUrl)
->url('https://my.url');
```

#### Extra HTTP headers

You may add HTTP headers that Chromium will send when loading the HTML document:

```php
use Gotenberg\Gotenberg;

$request = Gotenberg::chromium($apiUrl)
->pdf()
->userAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)')
->url('https://my.url');
```

#### Invalid HTTP status codes

You may force Gotenberg to return a `409 Conflict` response if the HTTP status code from the main page is not acceptable:
Expand Down Expand Up @@ -659,7 +672,7 @@ $request = Gotenberg::chromium($apiUrl)

#### Screenshots

You can capture full-page screenshots using the following three routes, which function similarly to their PDF equivalents:
You can capture screenshots using the following three routes, which function similarly to their PDF equivalents:

```php
use Gotenberg\Gotenberg;
Expand Down
10 changes: 10 additions & 0 deletions src/Modules/ChromiumMultipartFormDataModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public function cookies(array $cookies): self
return $this;
}

/**
* Overrides the default 'User-Agent' HTTP header.
*/
public function userAgent(string $userAgent): self
{
$this->formValue('userAgent', $userAgent);

return $this;
}

/**
* Sets extra HTTP headers that Chromium will send when loading the HTML
* document.
Expand Down
20 changes: 20 additions & 0 deletions tests/Modules/ChromiumPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -69,6 +70,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -106,6 +108,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -143,6 +146,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'HTTP Header content',
'My-Second-Http-Header' => 'Second HTTP Header content',
Expand Down Expand Up @@ -189,6 +193,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -220,6 +225,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -259,6 +265,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -295,6 +302,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand Down Expand Up @@ -343,6 +351,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -374,6 +383,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -418,6 +428,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -463,6 +474,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand Down Expand Up @@ -507,6 +519,7 @@ function hydrateChromiumPdfFormData(
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -580,6 +593,10 @@ function hydrateChromiumPdfFormData(
$chromium->cookies($cookies);
}

if ($userAgent !== null) {
$chromium->userAgent($userAgent);
}

if (count($extraHttpHeaders) > 0) {
$chromium->extraHttpHeaders($extraHttpHeaders);
}
Expand Down Expand Up @@ -643,6 +660,7 @@ function expectChromiumPdfOptions(
string|null $waitForExpression,
string|null $emulatedMediaType,
array $cookies,
string|null $userAgent,
array $extraHttpHeaders,
array $failOnHttpStatusCodes,
bool $failOnConsoleExceptions,
Expand Down Expand Up @@ -698,6 +716,8 @@ function expectChromiumPdfOptions(
expect($body)->toContainFormValue('cookies', $json);
}

expect($body)->unless($userAgent === null, fn ($body) => $body->toContainFormValue('userAgent', $userAgent));

if (count($extraHttpHeaders) > 0) {
$json = json_encode($extraHttpHeaders);
if ($json === false) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Modules/ChromiumScreenshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand All @@ -49,6 +50,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand All @@ -75,6 +77,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand All @@ -100,6 +103,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'HTTP Header content',
'My-Second-Http-Header' => 'Second HTTP Header content',
Expand Down Expand Up @@ -134,6 +138,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand All @@ -154,6 +159,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -182,6 +188,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand All @@ -207,6 +214,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand Down Expand Up @@ -243,6 +251,7 @@ function (
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand All @@ -263,6 +272,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -296,6 +306,7 @@ function (
$waitForExpression,
$emulatedMediaType,
$cookies,
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnConsoleExceptions,
Expand Down Expand Up @@ -330,6 +341,7 @@ function (
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
],
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
[
'My-Http-Header' => 'Http Header content',
'My-Second-Http-Header' => 'Second Http Header content',
Expand Down Expand Up @@ -362,6 +374,7 @@ function hydrateChromiumScreenshotFormData(
string|null $waitForExpression = null,
string|null $emulatedMediaType = null,
array $cookies = [],
string|null $userAgent = null,
array $extraHttpHeaders = [],
array $failOnHttpStatusCodes = [],
bool $failOnConsoleExceptions = false,
Expand Down Expand Up @@ -424,6 +437,10 @@ function hydrateChromiumScreenshotFormData(
$chromium->cookies($cookies);
}

if ($userAgent !== null) {
$chromium->userAgent($userAgent);
}

if (count($extraHttpHeaders) > 0) {
$chromium->extraHttpHeaders($extraHttpHeaders);
}
Expand Down Expand Up @@ -466,6 +483,7 @@ function expectChromiumScreenshotOptions(
string|null $waitForExpression,
string|null $emulatedMediaType,
array $cookies,
string|null $userAgent,
array $extraHttpHeaders,
array $failOnHttpStatusCodes,
bool $failOnConsoleExceptions,
Expand All @@ -492,6 +510,8 @@ function expectChromiumScreenshotOptions(
expect($body)->toContainFormValue('cookies', $json);
}

expect($body)->unless($userAgent === null, fn ($body) => $body->toContainFormValue('userAgent', $userAgent));

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

0 comments on commit da5b899

Please sign in to comment.