From b93718daeaf1198348e2996a69cddd2bd4ed1cf8 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 28 Sep 2021 15:33:36 -0700 Subject: [PATCH] feat(fetch): support ignoreHTTPSErrors option (#9206) --- docs/src/api/class-fetchrequest.md | 6 ++++++ src/client/fetch.ts | 4 ++++ src/dispatchers/networkDispatchers.ts | 1 + src/protocol/channels.ts | 2 ++ src/protocol/protocol.yml | 1 + src/protocol/validator.ts | 1 + src/server/fetch.ts | 2 +- src/server/types.ts | 1 + tests/browsercontext-fetch.spec.ts | 7 ++++++- types/types.d.ts | 15 +++++++++++++++ 10 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/src/api/class-fetchrequest.md b/docs/src/api/class-fetchrequest.md index 3bef765ffd..07596a6bb9 100644 --- a/docs/src/api/class-fetchrequest.md +++ b/docs/src/api/class-fetchrequest.md @@ -57,6 +57,8 @@ Request timeout in milliseconds. Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. +### option: FetchRequest.fetch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + ## async method: FetchRequest.get - returns: <[FetchResponse]> @@ -89,6 +91,8 @@ Request timeout in milliseconds. Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. +### option: FetchRequest.get.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + ## async method: FetchRequest.post - returns: <[FetchResponse]> @@ -128,3 +132,5 @@ Request timeout in milliseconds. Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + +### option: FetchRequest.post.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% diff --git a/src/client/fetch.ts b/src/client/fetch.ts index 09da722c10..0ed45ee8fd 100644 --- a/src/client/fetch.ts +++ b/src/client/fetch.ts @@ -35,6 +35,7 @@ export type FetchOptions = { data?: string | Buffer | Serializable, timeout?: number, failOnStatusCode?: boolean, + ignoreHTTPSErrors?: boolean, }; export class FetchRequest extends ChannelOwner implements api.FetchRequest { @@ -59,6 +60,7 @@ export class FetchRequest extends ChannelOwner { return this.fetch(urlOrRequest, { ...options, @@ -74,6 +76,7 @@ export class FetchRequest extends ChannelOwner { return this.fetch(urlOrRequest, { ...options, @@ -129,6 +132,7 @@ export class FetchRequest extends ChannelOwner Validator): Scheme { formData: tOptional(tAny), timeout: tOptional(tNumber), failOnStatusCode: tOptional(tBoolean), + ignoreHTTPSErrors: tOptional(tBoolean), }); scheme.FetchRequestFetchResponseBodyParams = tObject({ fetchUid: tString, diff --git a/src/server/fetch.ts b/src/server/fetch.ts index 471358a7db..f7afbafbde 100644 --- a/src/server/fetch.ts +++ b/src/server/fetch.ts @@ -123,7 +123,7 @@ export abstract class FetchRequest extends SdkObject { deadline }; // rejectUnauthorized = undefined is treated as true in node 12. - if (defaults.ignoreHTTPSErrors) + if (params.ignoreHTTPSErrors || defaults.ignoreHTTPSErrors) options.rejectUnauthorized = false; const requestUrl = new URL(params.url, defaults.baseURL); diff --git a/src/server/types.ts b/src/server/types.ts index 8789898b5b..08b7ed80fb 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -393,6 +393,7 @@ export type FetchOptions = { formData?: FormField[], timeout?: number, failOnStatusCode?: boolean, + ignoreHTTPSErrors?: boolean, }; export type FetchResponse = { diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.ts index beb6f32d54..f44931d03c 100644 --- a/tests/browsercontext-fetch.spec.ts +++ b/tests/browsercontext-fetch.spec.ts @@ -157,6 +157,11 @@ for (const method of ['get', 'post', 'fetch']) { }).catch(e => e); expect(error.message).toContain('404 Not Found'); }); + + it(`${method}should support ignoreHTTPSErrors option`, async ({ context, httpsServer }) => { + const response = await context._request[method](httpsServer.EMPTY_PAGE, { ignoreHTTPSErrors: true }); + expect(response.status()).toBe(200); + }); } it('should not add context cookie if cookie header passed as a parameter', async ({ context, server }) => { @@ -517,7 +522,7 @@ it('should support https', async ({ context, httpsServer }) => { } }); -it('should support ignoreHTTPSErrors', async ({ contextFactory, contextOptions, httpsServer }) => { +it('should inherit ignoreHTTPSErrors from context', async ({ contextFactory, contextOptions, httpsServer }) => { const context = await contextFactory({ ...contextOptions, ignoreHTTPSErrors: true }); const response = await context._request.get(httpsServer.EMPTY_PAGE); expect(response.status()).toBe(200); diff --git a/types/types.d.ts b/types/types.d.ts index c0c5d6211c..ee36b18743 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -12701,6 +12701,11 @@ export interface FetchRequest { */ headers?: { [key: string]: string; }; + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + /** * If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. */ @@ -12734,6 +12739,11 @@ export interface FetchRequest { */ headers?: { [key: string]: string; }; + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + /** * Query parameters to be send with the URL. */ @@ -12772,6 +12782,11 @@ export interface FetchRequest { */ headers?: { [key: string]: string; }; + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + /** * Query parameters to be send with the URL. */