Skip to content

Commit

Permalink
feat(chromium): add failOnResourceLoadingFailed and failOnResourceHtt…
Browse files Browse the repository at this point in the history
…pStatusCodes form fields
  • Loading branch information
gulien committed Nov 5, 2024
1 parent 76bc9d4 commit 1f97497
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Modules/ChromiumMultipartFormDataModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions tests/Modules/ChromiumPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param int[] $failOnResourceHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
* @param Stream[] $assets
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -73,6 +76,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -111,6 +116,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -152,6 +159,8 @@ function (
'My-Second-Http-Header' => 'Second HTTP Header content',
],
[ 499, 599 ],
[ 499, 599 ],
true,
true,
true,
'PDF/A-1a',
Expand All @@ -169,6 +178,7 @@ function (
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param int[] $failOnResourceHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
* @param Stream[] $assets
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -228,6 +240,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -268,6 +282,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -308,6 +324,8 @@ function (
'My-Second-Http-Header' => 'Second Http Header content',
],
[ 499, 599 ],
[ 499, 599 ],
true,
true,
true,
'PDF/A-1a',
Expand All @@ -325,6 +343,7 @@ function (
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param int[] $failOnResourceHttpStatusCodes
* @param Stream[] $markdowns
* @param array<string,string|bool|float|int|array<string>> $metadata
* @param Stream[] $assets
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -386,6 +407,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -431,6 +454,8 @@ function (
$userAgent,
$extraHttpHeaders,
$failOnHttpStatusCodes,
$failOnResourceHttpStatusCodes,
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$pdfa,
Expand Down Expand Up @@ -480,6 +505,8 @@ function (
'My-Second-Http-Header' => 'Second Http Header content',
],
[ 499, 599 ],
[ 499, 599 ],
true,
true,
true,
'PDF/A-1a',
Expand All @@ -495,6 +522,7 @@ function (
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param int[] $failOnResourceHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
* @param Stream[] $assets
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -605,6 +635,14 @@ function hydrateChromiumPdfFormData(
$chromium->failOnHttpStatusCodes($failOnHttpStatusCodes);
}

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

if ($failOnResourceLoadingFailed) {
$chromium->failOnResourceLoadingFailed();
}

if ($failOnConsoleExceptions) {
$chromium->failOnConsoleExceptions();
}
Expand Down Expand Up @@ -636,6 +674,7 @@ function hydrateChromiumPdfFormData(
* @param ChromiumCookie[] $cookies
* @param array<string,string> $extraHttpHeaders
* @param int[] $failOnHttpStatusCodes
* @param int[] $failOnResourceHttpStatusCodes
* @param array<string,string|bool|float|int|array<string>> $metadata
* @param Stream[] $assets
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down
Loading

0 comments on commit 1f97497

Please sign in to comment.