Skip to content

Commit

Permalink
feat(chormium): add support to failOnResourceHttpStatusCodes and fail…
Browse files Browse the repository at this point in the history
…OnResourceLoadingFailed
  • Loading branch information
cherfia committed Nov 18, 2024
1 parent 662ea16 commit 20bfe4c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/chromium/converters/html.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class HtmlConverter extends Converter {
extraHttpHeaders,
failOnHttpStatusCodes,
failOnConsoleExceptions,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed,
skipNetworkIdleEvent,
metadata,
cookies,
Expand Down Expand Up @@ -89,6 +91,8 @@ export class HtmlConverter extends Converter {
extraHttpHeaders,
failOnHttpStatusCodes,
failOnConsoleExceptions,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed,
skipNetworkIdleEvent,
metadata,
cookies,
Expand Down
6 changes: 5 additions & 1 deletion src/chromium/converters/markdown.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class MarkdownConverter extends Converter {
extraHttpHeaders,
failOnHttpStatusCodes,
failOnConsoleExceptions,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed,
skipNetworkIdleEvent,
metadata,
cookies,
Expand Down Expand Up @@ -87,7 +89,9 @@ export class MarkdownConverter extends Converter {
skipNetworkIdleEvent,
metadata,
cookies,
downloadFrom
downloadFrom,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed
});

return GotenbergUtils.fetch(
Expand Down
6 changes: 5 additions & 1 deletion src/chromium/converters/url.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class UrlConverter extends Converter {
userAgent,
extraHttpHeaders,
failOnHttpStatusCodes,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed,
failOnConsoleExceptions,
skipNetworkIdleEvent,
metadata,
Expand Down Expand Up @@ -84,7 +86,9 @@ export class UrlConverter extends Converter {
skipNetworkIdleEvent,
metadata,
cookies,
downloadFrom
downloadFrom,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed
});

return GotenbergUtils.fetch(
Expand Down
2 changes: 2 additions & 0 deletions src/chromium/interfaces/common.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type ChromiumOptions = {
extraHttpHeaders?: Record<string, string>;
failOnHttpStatusCodes?: number[]; // Return a 409 Conflict response if the HTTP status code is in the list (default [499,599])
failOnConsoleExceptions?: boolean; // Return a 409 Conflict response if there are exceptions in the Chromium console (default false)
failOnResourceHttpStatusCodes?: number[]; // Return a 409 Conflict response if the HTTP status code is in the list (default [499,599])
failOnResourceLoadingFailed?: boolean; // Return a 409 Conflict response if the resource loading failed (default false)
skipNetworkIdleEvent?: boolean; // Do not wait for Chromium network to be idle (default false)
cookies?: Cookie[]; // Cookies to be written.
downloadFrom?: DownloadFrom; // Download a file from a URL. It must return a Content-Disposition header with a filename parameter.
Expand Down
14 changes: 14 additions & 0 deletions src/chromium/utils/converter.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ export class ConverterUtils {
);
}

if (options.failOnResourceHttpStatusCodes) {
data.append(
'failOnResourceHttpStatusCodes',
JSON.stringify(options.failOnResourceHttpStatusCodes)
);
}

if (options.failOnResourceLoadingFailed) {
data.append(
'failOnResourceLoadingFailed',
String(options.failOnResourceLoadingFailed)
);
}

if (options.failOnConsoleExceptions) {
data.append(
'failOnConsoleExceptions',
Expand Down
14 changes: 14 additions & 0 deletions src/chromium/utils/screenshot.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ export class ScreenshotUtils {
);
}

if (options.failOnResourceHttpStatusCodes) {
data.append(
'failOnResourceHttpStatusCodes',
JSON.stringify(options.failOnResourceHttpStatusCodes)
);
}

if (options.failOnResourceLoadingFailed) {
data.append(
'failOnResourceLoadingFailed',
String(options.failOnResourceLoadingFailed)
);
}

if (options.failOnConsoleExceptions) {
data.append(
'failOnConsoleExceptions',
Expand Down
20 changes: 16 additions & 4 deletions src/chromium/utils/tests/converter.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ describe('ConverterUtils', () => {
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
extraHttpHeaders: { 'X-Custom-Header': 'value' },
failOnHttpStatusCodes: [499, 599],
failOnResourceHttpStatusCodes: [499, 599],
failOnResourceLoadingFailed: true,
skipNetworkIdleEvent: false,
failOnConsoleExceptions: true,
metadata: { Author: 'John Doe' },
Expand All @@ -439,7 +441,7 @@ describe('ConverterUtils', () => {
extraHttpHeaders: { 'Content-Type': 'application/json' }
}
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(16);
expect(mockFormDataAppend).toHaveBeenCalledTimes(18);
expect(data.append).toHaveBeenNthCalledWith(
1,
'pdfa',
Expand Down Expand Up @@ -500,21 +502,31 @@ describe('ConverterUtils', () => {
);
expect(data.append).toHaveBeenNthCalledWith(
13,
'failOnResourceHttpStatusCodes',
JSON.stringify([499, 599])
);
expect(data.append).toHaveBeenNthCalledWith(
14,
'failOnResourceLoadingFailed',
'true'
);
expect(data.append).toHaveBeenNthCalledWith(
15,
'failOnConsoleExceptions',
'true'
);
expect(data.append).toHaveBeenNthCalledWith(
14,
16,
'skipNetworkIdleEvent',
'false'
);
expect(data.append).toHaveBeenNthCalledWith(
15,
17,
'metadata',
JSON.stringify({ Author: 'John Doe' })
);
expect(data.append).toHaveBeenNthCalledWith(
16,
18,
'downloadFrom',
JSON.stringify({
url: 'http://example.com',
Expand Down
21 changes: 17 additions & 4 deletions src/chromium/utils/tests/screenshot.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ describe('ScreenshotUtils', () => {
await ScreenshotUtils.customize(data, {
emulatedMediaType: 'screen',
failOnHttpStatusCodes: [499, 599],
failOnResourceHttpStatusCodes: [499, 599],
failOnResourceLoadingFailed: true,
skipNetworkIdleEvent: false,
failOnConsoleExceptions: true,
properties: {
Expand All @@ -266,7 +268,7 @@ describe('ScreenshotUtils', () => {
extraHttpHeaders: { 'Content-Type': 'application/json' }
}
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(11);
expect(mockFormDataAppend).toHaveBeenCalledTimes(13);
expect(data.append).toHaveBeenNthCalledWith(
1,
'emulatedMediaType',
Expand Down Expand Up @@ -300,21 +302,32 @@ describe('ScreenshotUtils', () => {
);
expect(data.append).toHaveBeenNthCalledWith(
8,
'failOnResourceHttpStatusCodes',
JSON.stringify([499, 599])
);
expect(data.append).toHaveBeenNthCalledWith(
9,
'failOnResourceLoadingFailed',
'true'
);

expect(data.append).toHaveBeenNthCalledWith(
10,
'failOnConsoleExceptions',
'true'
);
expect(data.append).toHaveBeenNthCalledWith(
9,
11,
'skipNetworkIdleEvent',
'false'
);
expect(data.append).toHaveBeenNthCalledWith(
10,
12,
'optimizeForSpeed',
'true'
);
expect(data.append).toHaveBeenNthCalledWith(
11,
13,
'downloadFrom',
JSON.stringify({
url: 'http://example.com',
Expand Down

0 comments on commit 20bfe4c

Please sign in to comment.