Skip to content

Commit

Permalink
feat: update flag to be named apiRequestFailsOnErrorStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonLei123 committed Feb 11, 2025
1 parent ee59e0f commit 592829c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion docs/src/api/class-apirequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Creates new instances of [APIRequestContext].
### option: APIRequest.newContext.extraHTTPHeaders = %%-context-option-extrahttpheaders-%%
* since: v1.16

### option: APIRequest.newContext.fetchFailOnStatusCode = %%-context-option-fetchFailOnStatusCode-%%
### option: APIRequest.newContext.apiRequestFailsOnErrorStatus = %%-context-option-apiRequestFailsOnErrorStatus-%%
* since: v1.51

### option: APIRequest.newContext.httpCredentials = %%-context-option-httpcredentials-%%
Expand Down
6 changes: 3 additions & 3 deletions docs/src/api/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ A list of permissions to grant to all pages in this context. See

An object containing additional HTTP headers to be sent with every request. Defaults to none.

## context-option-fetchFailOnStatusCode
- `fetchFailOnStatusCode` <[boolean]>
## context-option-apiRequestFailsOnErrorStatus
- `apiRequestFailsOnErrorStatus` <[boolean]>

An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By default, response object is returned for all status codes.

Expand Down Expand Up @@ -970,7 +970,7 @@ between the same pixel in compared images, between zero (strict) and one (lax),
- %%-context-option-locale-%%
- %%-context-option-permissions-%%
- %%-context-option-extrahttpheaders-%%
- %%-context-option-fetchFailOnStatusCode-%%
- %%-context-option-apiRequestFailsOnErrorStatus-%%
- %%-context-option-offline-%%
- %%-context-option-httpcredentials-%%
- %%-context-option-colorscheme-%%
Expand Down
10 changes: 5 additions & 5 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ scheme.PlaywrightNewRequestParams = tObject({
userAgent: tOptional(tString),
ignoreHTTPSErrors: tOptional(tBoolean),
extraHTTPHeaders: tOptional(tArray(tType('NameValue'))),
fetchFailOnStatusCode: tOptional(tBoolean),
apiRequestFailsOnErrorStatus: tOptional(tBoolean),
clientCertificates: tOptional(tArray(tObject({
origin: tString,
cert: tOptional(tBinary),
Expand Down Expand Up @@ -569,7 +569,7 @@ scheme.BrowserTypeLaunchPersistentContextParams = tObject({
})),
permissions: tOptional(tArray(tString)),
extraHTTPHeaders: tOptional(tArray(tType('NameValue'))),
fetchFailOnStatusCode: tOptional(tBoolean),
apiRequestFailsOnErrorStatus: tOptional(tBoolean),
offline: tOptional(tBoolean),
httpCredentials: tOptional(tObject({
username: tString,
Expand Down Expand Up @@ -656,7 +656,7 @@ scheme.BrowserNewContextParams = tObject({
})),
permissions: tOptional(tArray(tString)),
extraHTTPHeaders: tOptional(tArray(tType('NameValue'))),
fetchFailOnStatusCode: tOptional(tBoolean),
apiRequestFailsOnErrorStatus: tOptional(tBoolean),
offline: tOptional(tBoolean),
httpCredentials: tOptional(tObject({
username: tString,
Expand Down Expand Up @@ -726,7 +726,7 @@ scheme.BrowserNewContextForReuseParams = tObject({
})),
permissions: tOptional(tArray(tString)),
extraHTTPHeaders: tOptional(tArray(tType('NameValue'))),
fetchFailOnStatusCode: tOptional(tBoolean),
apiRequestFailsOnErrorStatus: tOptional(tBoolean),
offline: tOptional(tBoolean),
httpCredentials: tOptional(tObject({
username: tString,
Expand Down Expand Up @@ -2624,7 +2624,7 @@ scheme.AndroidDeviceLaunchBrowserParams = tObject({
})),
permissions: tOptional(tArray(tString)),
extraHTTPHeaders: tOptional(tArray(tType('NameValue'))),
fetchFailOnStatusCode: tOptional(tBoolean),
apiRequestFailsOnErrorStatus: tOptional(tBoolean),
offline: tOptional(tBoolean),
httpCredentials: tOptional(tObject({
username: tString,
Expand Down
8 changes: 4 additions & 4 deletions packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { TLSSocket } from 'tls';
type FetchRequestOptions = {
userAgent: string;
extraHTTPHeaders?: HeadersArray;
fetchFailOnStatusCode?: boolean;
apiRequestFailsOnErrorStatus?: boolean;
httpCredentials?: HTTPCredentials;
proxy?: ProxySettings;
timeoutSettings: TimeoutSettings;
Expand Down Expand Up @@ -206,7 +206,7 @@ export abstract class APIRequestContext extends SdkObject {
});
const fetchUid = this._storeResponseBody(fetchResponse.body);
this.fetchLog.set(fetchUid, controller.metadata.log);
const failOnStatusCode = params.failOnStatusCode !== undefined ? params.failOnStatusCode : !!defaults.fetchFailOnStatusCode;
const failOnStatusCode = params.failOnStatusCode !== undefined ? params.failOnStatusCode : !!defaults.apiRequestFailsOnErrorStatus;
if (failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400)) {
let responseText = '';
if (fetchResponse.body.byteLength) {
Expand Down Expand Up @@ -612,7 +612,7 @@ export class BrowserContextAPIRequestContext extends APIRequestContext {
return {
userAgent: this._context._options.userAgent || this._context._browser.userAgent(),
extraHTTPHeaders: this._context._options.extraHTTPHeaders,
fetchFailOnStatusCode: this._context._options.fetchFailOnStatusCode,
apiRequestFailsOnErrorStatus: this._context._options.apiRequestFailsOnErrorStatus,
httpCredentials: this._context._options.httpCredentials,
proxy: this._context._options.proxy || this._context._browser.options.proxy,
timeoutSettings: this._context._timeoutSettings,
Expand Down Expand Up @@ -664,7 +664,7 @@ export class GlobalAPIRequestContext extends APIRequestContext {
baseURL: options.baseURL,
userAgent: options.userAgent || getUserAgent(),
extraHTTPHeaders: options.extraHTTPHeaders,
fetchFailOnStatusCode: !!options.fetchFailOnStatusCode,
apiRequestFailsOnErrorStatus: !!options.apiRequestFailsOnErrorStatus,
ignoreHTTPSErrors: !!options.ignoreHTTPSErrors,
httpCredentials: options.httpCredentials,
clientCertificates: options.clientCertificates,
Expand Down
60 changes: 30 additions & 30 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9683,6 +9683,12 @@ export interface Browser {
*/
acceptDownloads?: boolean;

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
apiRequestFailsOnErrorStatus?: boolean;

/**
* When using [page.goto(url[, options])](https://playwright.dev/docs/api/class-page#page-goto),
* [page.route(url, handler[, options])](https://playwright.dev/docs/api/class-page#page-route),
Expand Down Expand Up @@ -9781,12 +9787,6 @@ export interface Browser {
*/
extraHTTPHeaders?: { [key: string]: string; };

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
fetchFailOnStatusCode?: boolean;

/**
* Emulates `'forced-colors'` media feature, supported values are `'active'`, `'none'`. See
* [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
Expand Down Expand Up @@ -14691,6 +14691,12 @@ export interface BrowserType<Unused = {}> {
*/
acceptDownloads?: boolean;

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
apiRequestFailsOnErrorStatus?: boolean;

/**
* **NOTE** Use custom browser args at your own risk, as some of them may break Playwright functionality.
*
Expand Down Expand Up @@ -14840,12 +14846,6 @@ export interface BrowserType<Unused = {}> {
*/
extraHTTPHeaders?: { [key: string]: string; };

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
fetchFailOnStatusCode?: boolean;

/**
* Firefox user preferences. Learn more about the Firefox user preferences at
* [`about:config`](https://support.mozilla.org/en-US/kb/about-config-editor-firefox).
Expand Down Expand Up @@ -16580,6 +16580,12 @@ export interface AndroidDevice {
*/
acceptDownloads?: boolean;

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
apiRequestFailsOnErrorStatus?: boolean;

/**
* **NOTE** Use custom browser args at your own risk, as some of them may break Playwright functionality.
*
Expand Down Expand Up @@ -16630,12 +16636,6 @@ export interface AndroidDevice {
*/
extraHTTPHeaders?: { [key: string]: string; };

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
fetchFailOnStatusCode?: boolean;

/**
* Emulates `'forced-colors'` media feature, supported values are `'active'`, `'none'`. See
* [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
Expand Down Expand Up @@ -17425,6 +17425,12 @@ export interface APIRequest {
* @param options
*/
newContext(options?: {
/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
apiRequestFailsOnErrorStatus?: boolean;

/**
* Methods like
* [apiRequestContext.get(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get)
Expand Down Expand Up @@ -17500,12 +17506,6 @@ export interface APIRequest {
*/
extraHTTPHeaders?: { [key: string]: string; };

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
fetchFailOnStatusCode?: boolean;

/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
Expand Down Expand Up @@ -21901,6 +21901,12 @@ export interface BrowserContextOptions {
*/
acceptDownloads?: boolean;

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
apiRequestFailsOnErrorStatus?: boolean;

/**
* When using [page.goto(url[, options])](https://playwright.dev/docs/api/class-page#page-goto),
* [page.route(url, handler[, options])](https://playwright.dev/docs/api/class-page#page-route),
Expand Down Expand Up @@ -21999,12 +22005,6 @@ export interface BrowserContextOptions {
*/
extraHTTPHeaders?: { [key: string]: string; };

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx. By
* default, response object is returned for all status codes.
*/
fetchFailOnStatusCode?: boolean;

/**
* Emulates `'forced-colors'` media feature, supported values are `'active'`, `'none'`. See
* [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
Expand Down
20 changes: 10 additions & 10 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export type PlaywrightNewRequestParams = {
userAgent?: string,
ignoreHTTPSErrors?: boolean,
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
clientCertificates?: {
origin: string,
cert?: Binary,
Expand Down Expand Up @@ -620,7 +620,7 @@ export type PlaywrightNewRequestOptions = {
userAgent?: string,
ignoreHTTPSErrors?: boolean,
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
clientCertificates?: {
origin: string,
cert?: Binary,
Expand Down Expand Up @@ -994,7 +994,7 @@ export type BrowserTypeLaunchPersistentContextParams = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -1075,7 +1075,7 @@ export type BrowserTypeLaunchPersistentContextOptions = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -1191,7 +1191,7 @@ export type BrowserNewContextParams = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -1258,7 +1258,7 @@ export type BrowserNewContextOptions = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -1328,7 +1328,7 @@ export type BrowserNewContextForReuseParams = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -1395,7 +1395,7 @@ export type BrowserNewContextForReuseOptions = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -4745,7 +4745,7 @@ export type AndroidDeviceLaunchBrowserParams = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down Expand Up @@ -4810,7 +4810,7 @@ export type AndroidDeviceLaunchBrowserOptions = {
},
permissions?: string[],
extraHTTPHeaders?: NameValue[],
fetchFailOnStatusCode?: boolean,
apiRequestFailsOnErrorStatus?: boolean,
offline?: boolean,
httpCredentials?: {
username: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ ContextOptions:
extraHTTPHeaders:
type: array?
items: NameValue
fetchFailOnStatusCode: boolean?
apiRequestFailsOnErrorStatus: boolean?
offline: boolean?
httpCredentials:
type: object?
Expand Down Expand Up @@ -694,7 +694,7 @@ Playwright:
extraHTTPHeaders:
type: array?
items: NameValue
fetchFailOnStatusCode: boolean?
apiRequestFailsOnErrorStatus: boolean?
clientCertificates:
type: array?
items:
Expand Down
14 changes: 7 additions & 7 deletions tests/library/browsercontext-fetchFailOnStatusCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import { browserTest as it, expect } from '../config/browserTest';

it('should throw when fetchFailOnStatusCode is set to true inside BrowserContext options', async ({ browser, server }) => {
it('should throw when apiRequestFailsOnErrorStatus is set to true inside BrowserContext options', async ({ browser, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
const context = await browser.newContext({ fetchFailOnStatusCode: true });
const context = await browser.newContext({ apiRequestFailsOnErrorStatus: true });
server.setRoute('/empty.html', (req, res) => {
res.writeHead(404, { 'Content-Length': 10, 'Content-Type': 'text/plain' });
res.end('Not found.');
Expand All @@ -30,7 +30,7 @@ it('should throw when fetchFailOnStatusCode is set to true inside BrowserContext

it('should not throw when failOnStatusCode is set to false inside BrowserContext options', async ({ browser, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
const context = await browser.newContext({ fetchFailOnStatusCode: false });
const context = await browser.newContext({ apiRequestFailsOnErrorStatus: false });
server.setRoute('/empty.html', (req, res) => {
res.writeHead(404, { 'Content-Length': 10, 'Content-Type': 'text/plain' });
res.end('Not found.');
Expand All @@ -40,10 +40,10 @@ it('should not throw when failOnStatusCode is set to false inside BrowserContext
await context.close();
});

it('should throw when fetchFailOnStatusCode is set to true inside browserType.launchPersistentContext options', async ({ browserType, server, createUserDataDir }) => {
it('should throw when apiRequestFailsOnErrorStatus is set to true inside browserType.launchPersistentContext options', async ({ browserType, server, createUserDataDir }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
const userDataDir = await createUserDataDir();
const context = await browserType.launchPersistentContext(userDataDir, { fetchFailOnStatusCode: true });
const context = await browserType.launchPersistentContext(userDataDir, { apiRequestFailsOnErrorStatus: true });
server.setRoute('/empty.html', (req, res) => {
res.writeHead(404, { 'Content-Length': 10, 'Content-Type': 'text/plain' });
res.end('Not found.');
Expand All @@ -53,10 +53,10 @@ it('should throw when fetchFailOnStatusCode is set to true inside browserType.la
await context.close();
});

it('should not throw when fetchFailOnStatusCode is set to false inside browserType.launchPersistentContext options', async ({ browserType, server, createUserDataDir }) => {
it('should not throw when apiRequestFailsOnErrorStatus is set to false inside browserType.launchPersistentContext options', async ({ browserType, server, createUserDataDir }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
const userDataDir = await createUserDataDir();
const context = await browserType.launchPersistentContext(userDataDir, { fetchFailOnStatusCode: false });
const context = await browserType.launchPersistentContext(userDataDir, { apiRequestFailsOnErrorStatus: false });
server.setRoute('/empty.html', (req, res) => {
res.writeHead(404, { 'Content-Length': 10, 'Content-Type': 'text/plain' });
res.end('Not found.');
Expand Down
Loading

0 comments on commit 592829c

Please sign in to comment.