Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(fetch): support ignoreHTTPSErrors option (#9206)

Yury Semikhatskyb93718daeaf1
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<channels.FetchRequestChannel, channels.FetchRequestInitializer> implements api.FetchRequest {
@@ -59,6 +60,7 @@ export class FetchRequest extends ChannelOwner<channels.FetchRequestChannel, cha
headers?: { [key: string]: string; };
timeout?: number;
failOnStatusCode?: boolean;
+ ignoreHTTPSErrors?: boolean,
}): Promise<FetchResponse> {
return this.fetch(urlOrRequest, {
...options,
@@ -74,6 +76,7 @@ export class FetchRequest extends ChannelOwner<channels.FetchRequestChannel, cha
data?: string | Buffer | Serializable;
timeout?: number;
failOnStatusCode?: boolean;
+ ignoreHTTPSErrors?: boolean,
}): Promise<FetchResponse> {
return this.fetch(urlOrRequest, {
...options,
@@ -129,6 +132,7 @@ export class FetchRequest extends ChannelOwner<channels.FetchRequestChannel, cha
formData,
timeout: options.timeout,
failOnStatusCode: options.failOnStatusCode,
+ ignoreHTTPSErrors: options.ignoreHTTPSErrors,
});
if (result.error)
throw new Error(result.error);
diff --git a/src/dispatchers/networkDispatchers.ts b/src/dispatchers/networkDispatchers.ts
index ff2e6a0825..e60400b9b9 100644
--- a/src/dispatchers/networkDispatchers.ts
+++ b/src/dispatchers/networkDispatchers.ts
@@ -191,6 +191,7 @@ export class FetchRequestDispatcher extends Dispatcher<FetchRequest, channels.Fe
formData: params.formData,
timeout: params.timeout,
failOnStatusCode: params.failOnStatusCode,
+ ignoreHTTPSErrors: params.ignoreHTTPSErrors,
});
let response;
if (fetchResponse) {
diff --git a/src/protocol/channels.ts b/src/protocol/channels.ts
index ab707a18bb..c4f4cf66a8 100644
--- a/src/protocol/channels.ts
+++ b/src/protocol/channels.ts
@@ -174,6 +174,7 @@ export type FetchRequestFetchParams = {
formData?: any,
timeout?: number,
failOnStatusCode?: boolean,
+ ignoreHTTPSErrors?: boolean,
};
export type FetchRequestFetchOptions = {
params?: NameValue[],
@@ -183,6 +184,7 @@ export type FetchRequestFetchOptions = {
formData?: any,
timeout?: number,
failOnStatusCode?: boolean,
+ ignoreHTTPSErrors?: boolean,
};
export type FetchRequestFetchResult = {
response?: FetchResponse,
diff --git a/src/protocol/protocol.yml b/src/protocol/protocol.yml
index cde1894811..cacb263cb3 100644
--- a/src/protocol/protocol.yml
+++ b/src/protocol/protocol.yml
@@ -245,6 +245,7 @@ FetchRequest:
formData: json?
timeout: number?
failOnStatusCode: boolean?
+ ignoreHTTPSErrors: boolean?
returns:
response: FetchResponse?
error: string?
diff --git a/src/protocol/validator.ts b/src/protocol/validator.ts
index 4de5ccd2b2..35d05098ba 100644
--- a/src/protocol/validator.ts
+++ b/src/protocol/validator.ts
@@ -162,6 +162,7 @@ export function createScheme(tChannel: (name: string) => 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.
*/