diff --git a/.gitignore b/.gitignore index 0352a0c..7b1d51e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /coverage.xml /.phpcs-cache /.phpunit.result.cache -/composer.lock \ No newline at end of file +/composer.lock +/.phpunit.cache/ diff --git a/README.md b/README.md index f955d0c..b84f846 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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: @@ -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; diff --git a/src/Modules/ChromiumMultipartFormDataModule.php b/src/Modules/ChromiumMultipartFormDataModule.php index b66435f..6aa6872 100644 --- a/src/Modules/ChromiumMultipartFormDataModule.php +++ b/src/Modules/ChromiumMultipartFormDataModule.php @@ -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. diff --git a/tests/Modules/ChromiumPdfTest.php b/tests/Modules/ChromiumPdfTest.php index 917dd71..6f380ae 100644 --- a/tests/Modules/ChromiumPdfTest.php +++ b/tests/Modules/ChromiumPdfTest.php @@ -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, @@ -69,6 +70,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -106,6 +108,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -220,6 +225,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -259,6 +265,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -374,6 +383,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -418,6 +428,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -580,6 +593,10 @@ function hydrateChromiumPdfFormData( $chromium->cookies($cookies); } + if ($userAgent !== null) { + $chromium->userAgent($userAgent); + } + if (count($extraHttpHeaders) > 0) { $chromium->extraHttpHeaders($extraHttpHeaders); } @@ -643,6 +660,7 @@ function expectChromiumPdfOptions( string|null $waitForExpression, string|null $emulatedMediaType, array $cookies, + string|null $userAgent, array $extraHttpHeaders, array $failOnHttpStatusCodes, bool $failOnConsoleExceptions, @@ -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) { diff --git a/tests/Modules/ChromiumScreenshotTest.php b/tests/Modules/ChromiumScreenshotTest.php index 563919a..40138ee 100644 --- a/tests/Modules/ChromiumScreenshotTest.php +++ b/tests/Modules/ChromiumScreenshotTest.php @@ -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, @@ -49,6 +50,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -75,6 +77,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -154,6 +159,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -182,6 +188,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -263,6 +272,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -296,6 +306,7 @@ function ( $waitForExpression, $emulatedMediaType, $cookies, + $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, $failOnConsoleExceptions, @@ -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', @@ -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, @@ -424,6 +437,10 @@ function hydrateChromiumScreenshotFormData( $chromium->cookies($cookies); } + if ($userAgent !== null) { + $chromium->userAgent($userAgent); + } + if (count($extraHttpHeaders) > 0) { $chromium->extraHttpHeaders($extraHttpHeaders); } @@ -466,6 +483,7 @@ function expectChromiumScreenshotOptions( string|null $waitForExpression, string|null $emulatedMediaType, array $cookies, + string|null $userAgent, array $extraHttpHeaders, array $failOnHttpStatusCodes, bool $failOnConsoleExceptions, @@ -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) {