diff --git a/src/Modules/ChromiumMultipartFormDataModule.php b/src/Modules/ChromiumMultipartFormDataModule.php index 05c5082..d549837 100644 --- a/src/Modules/ChromiumMultipartFormDataModule.php +++ b/src/Modules/ChromiumMultipartFormDataModule.php @@ -138,6 +138,37 @@ public function failOnHttpStatusCodes(array $statusCodes): self return $this; } + /** + * Forces Gotenberg to return a 409 Conflict response if the HTTP status + * code at least one resource is not acceptable. + * + * @param int[] $statusCodes + * + * @throws NativeFunctionErrored + */ + public function failOnResourceHttpStatusCodes(array $statusCodes): self + { + $json = json_encode($statusCodes); + if ($json === false) { + throw NativeFunctionErrored::createFromLastPhpError(); + } + + $this->formValue('failOnResourceHttpStatusCodes', $json); + + return $this; + } + + /** + * Forces Gotenberg to return a 409 Conflict if Chromium fails to load at + * least one resource. + */ + public function failOnResourceLoadingFailed(): self + { + $this->formValue('failOnResourceLoadingFailed', true); + + return $this; + } + /** * Forces Gotenberg to return a 409 Conflict response if there are * exceptions in the Chromium console. diff --git a/tests/Modules/ChromiumPdfTest.php b/tests/Modules/ChromiumPdfTest.php index 46b8975..6f97aee 100644 --- a/tests/Modules/ChromiumPdfTest.php +++ b/tests/Modules/ChromiumPdfTest.php @@ -14,6 +14,7 @@ * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param array> $metadata * @param Stream[] $assets */ @@ -41,6 +42,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, string|null $pdfa = null, @@ -73,6 +76,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -111,6 +116,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -152,6 +159,8 @@ function ( 'My-Second-Http-Header' => 'Second HTTP Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, 'PDF/A-1a', @@ -169,6 +178,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param array> $metadata * @param Stream[] $assets */ @@ -196,6 +206,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, string|null $pdfa = null, @@ -228,6 +240,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -268,6 +282,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -308,6 +324,8 @@ function ( 'My-Second-Http-Header' => 'Second Http Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, 'PDF/A-1a', @@ -325,6 +343,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $markdowns * @param array> $metadata * @param Stream[] $assets @@ -354,6 +373,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, string|null $pdfa = null, @@ -386,6 +407,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -431,6 +454,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $pdfa, @@ -480,6 +505,8 @@ function ( 'My-Second-Http-Header' => 'Second Http Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, 'PDF/A-1a', @@ -495,6 +522,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param array> $metadata * @param Stream[] $assets */ @@ -522,6 +550,8 @@ function hydrateChromiumPdfFormData( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, string|null $pdfa = null, @@ -605,6 +635,14 @@ function hydrateChromiumPdfFormData( $chromium->failOnHttpStatusCodes($failOnHttpStatusCodes); } + if (count($failOnResourceHttpStatusCodes) > 0) { + $chromium->failOnResourceHttpStatusCodes($failOnResourceHttpStatusCodes); + } + + if ($failOnResourceLoadingFailed) { + $chromium->failOnResourceLoadingFailed(); + } + if ($failOnConsoleExceptions) { $chromium->failOnConsoleExceptions(); } @@ -636,6 +674,7 @@ function hydrateChromiumPdfFormData( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param array> $metadata * @param Stream[] $assets */ @@ -663,6 +702,8 @@ function expectChromiumPdfOptions( string|null $userAgent, array $extraHttpHeaders, array $failOnHttpStatusCodes, + array $failOnResourceHttpStatusCodes, + bool $failOnResourceLoadingFailed, bool $failOnConsoleExceptions, bool|null $skipNetworkIdleEvent, string|null $pdfa, @@ -736,6 +777,16 @@ function expectChromiumPdfOptions( expect($body)->toContainFormValue('failOnHttpStatusCodes', $json); } + if (count($failOnResourceHttpStatusCodes) > 0) { + $json = json_encode($failOnResourceHttpStatusCodes); + if ($json === false) { + throw NativeFunctionErrored::createFromLastPhpError(); + } + + expect($body)->toContainFormValue('failOnResourceHttpStatusCodes', $json); + } + + expect($body)->unless($failOnResourceLoadingFailed === false, fn ($body) => $body->toContainFormValue('failOnResourceLoadingFailed', '1')); expect($body)->unless($failOnConsoleExceptions === false, fn ($body) => $body->toContainFormValue('failOnConsoleExceptions', '1')); expect($body)->unless($skipNetworkIdleEvent === null, fn ($body) => $body->toContainFormValue('skipNetworkIdleEvent', $skipNetworkIdleEvent === true ? '1' : '0')); expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa)); diff --git a/tests/Modules/ChromiumScreenshotTest.php b/tests/Modules/ChromiumScreenshotTest.php index 46c55f2..bc063d2 100644 --- a/tests/Modules/ChromiumScreenshotTest.php +++ b/tests/Modules/ChromiumScreenshotTest.php @@ -14,6 +14,7 @@ * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $assets */ function ( @@ -32,6 +33,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, array $assets = [], @@ -53,6 +56,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -80,6 +85,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -109,6 +116,8 @@ function ( 'My-Second-Http-Header' => 'Second HTTP Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, [ @@ -123,6 +132,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $assets */ function ( @@ -141,6 +151,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, array $assets = [], @@ -162,6 +174,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -191,6 +205,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -220,6 +236,8 @@ function ( 'My-Second-Http-Header' => 'Second Http Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, [ @@ -234,6 +252,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $markdowns * @param Stream[] $assets */ @@ -254,6 +273,8 @@ function ( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, array $assets = [], @@ -275,6 +296,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -309,6 +332,8 @@ function ( $userAgent, $extraHttpHeaders, $failOnHttpStatusCodes, + $failOnResourceHttpStatusCodes, + $failOnResourceLoadingFailed, $failOnConsoleExceptions, $skipNetworkIdleEvent, $assets, @@ -347,6 +372,8 @@ function ( 'My-Second-Http-Header' => 'Second Http Header content', ], [ 499, 599 ], + [ 499, 599 ], + true, true, true, [ @@ -359,6 +386,7 @@ function ( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $assets */ function hydrateChromiumScreenshotFormData( @@ -377,6 +405,8 @@ function hydrateChromiumScreenshotFormData( string|null $userAgent = null, array $extraHttpHeaders = [], array $failOnHttpStatusCodes = [], + array $failOnResourceHttpStatusCodes = [], + bool $failOnResourceLoadingFailed = false, bool $failOnConsoleExceptions = false, bool|null $skipNetworkIdleEvent = null, array $assets = [], @@ -449,6 +479,14 @@ function hydrateChromiumScreenshotFormData( $chromium->failOnHttpStatusCodes($failOnHttpStatusCodes); } + if (count($failOnResourceHttpStatusCodes) > 0) { + $chromium->failOnResourceHttpStatusCodes($failOnResourceHttpStatusCodes); + } + + if ($failOnResourceLoadingFailed) { + $chromium->failOnResourceLoadingFailed(); + } + if ($failOnConsoleExceptions) { $chromium->failOnConsoleExceptions(); } @@ -468,6 +506,7 @@ function hydrateChromiumScreenshotFormData( * @param ChromiumCookie[] $cookies * @param array $extraHttpHeaders * @param int[] $failOnHttpStatusCodes + * @param int[] $failOnResourceHttpStatusCodes * @param Stream[] $assets */ function expectChromiumScreenshotOptions( @@ -486,6 +525,8 @@ function expectChromiumScreenshotOptions( string|null $userAgent, array $extraHttpHeaders, array $failOnHttpStatusCodes, + array $failOnResourceHttpStatusCodes, + bool $failOnResourceLoadingFailed, bool $failOnConsoleExceptions, bool|null $skipNetworkIdleEvent, array $assets, @@ -530,6 +571,16 @@ function expectChromiumScreenshotOptions( expect($body)->toContainFormValue('failOnHttpStatusCodes', $json); } + if (count($failOnResourceHttpStatusCodes) > 0) { + $json = json_encode($failOnResourceHttpStatusCodes); + if ($json === false) { + throw NativeFunctionErrored::createFromLastPhpError(); + } + + expect($body)->toContainFormValue('failOnResourceHttpStatusCodes', $json); + } + + expect($body)->unless($failOnResourceLoadingFailed === false, fn ($body) => $body->toContainFormValue('failOnResourceLoadingFailed', '1')); expect($body)->unless($failOnConsoleExceptions === false, fn ($body) => $body->toContainFormValue('failOnConsoleExceptions', '1')); expect($body)->unless($skipNetworkIdleEvent === null, fn ($body) => $body->toContainFormValue('skipNetworkIdleEvent', $skipNetworkIdleEvent === true ? '1' : '0'));