Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add failOnStatusCode option to API request context #34346

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JacksonLei123
Copy link

Add failOnStatusCode option to API request context so that the flag is applied to every request made unless specified.

References issue #34204

Copy link
Contributor

Test results for "tests 1"

15 failed
❌ [chromium] › tests/components/codeMirrorWrapper.spec.tsx:71:5 › highlight JavaScript @web-components-web
❌ [chromium] › tests/components/codeMirrorWrapper.spec.tsx:76:5 › highlight Python @web-components-web
❌ [chromium] › tests/components/codeMirrorWrapper.spec.tsx:81:5 › highlight Java @web-components-web
❌ [chromium] › tests/components/codeMirrorWrapper.spec.tsx:86:5 › highlight C# @web-components-web
❌ [chromium] › tests/components/codeMirrorWrapper.spec.tsx:91:5 › highlight lines @web-components-web
❌ [chromium] › tests/components/expandable.spec.tsx:22:5 › should render collapsed @web-components-web
❌ [chromium] › tests/components/expandable.spec.tsx:29:5 › should render expanded @web-components-web
❌ [chromium] › tests/components/expandable.spec.tsx:36:5 › click should expand @web-components-web
❌ [chromium] › tests/components/splitView.spec.tsx:22:5 › should render @web-components-web
❌ [chromium] › tests/components/splitView.spec.tsx:35:5 › should render sidebar first @web-components-web
❌ [chromium] › tests/components/splitView.spec.tsx:49:5 › should render horizontal split @web-components-web
❌ [chromium] › tests/components/splitView.spec.tsx:64:5 › should hide sidebar @web-components-web
❌ [chromium] › tests/components/splitView.spec.tsx:77:5 › drag resize @web-components-web
❌ [chromium] › tests/shared/imageDiffView.spec.tsx:37:5 › should render links @web-components-web
❌ [chromium] › tests/shared/imageDiffView.spec.tsx:46:5 › should show diff by default @web-components-web

0 passed
✔️✔️✔️

Merge workflow run.

@@ -620,6 +620,12 @@ 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-apirequest
- `apiRequest` <[Object]>
- `failOnStatusCode` <[boolean]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apiRequest.newContext({apiRequest: {...}}) call would look weird. Since the option will be present on both browser and API contexts, but makes sense only for the later, we can probably use a more specific name for the property failAPIRequestOnStatusCode (also not great). Also the documentation should make it clear that it will only affect API requests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I still have that option inside the apiRequest object? Or just replace the object with a more specific variable name such as failAPIRequestOnStatusCode?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to me like the option will be present on both browser and API contexts since the browser.newContext() and playwright.request.newContext() returns different types. The changes I've made so far should only affect API contexts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I still have that option inside the apiRequest object? Or just replace the object with a more specific variable name such as failAPIRequestOnStatusCode?

I'd just replace it with a flat property.

It doesn't seem to me like the option will be present on both browser and API contexts since the browser.newContext() and playwright.request.newContext() returns different types.

It depends on where you add references to the new context-option-apirequest, see my comment below. I'd expect it to be present in both newContext methods mentioned above, so that it works for page.request.fetch() too. Arguably we can omit this feature in page.request and only have in global fetch, but I believe it'd be convenient if it could be set in the config.

expect(error.message).toContain('404 Not Found');
});

it('should throw when failOnStatusCode is set to true inside the fetch API call', async ({ playwright, server }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already ...should support failOnStatusCode tests that cover this scenario, no need to repeat.


it('should throw when failOnStatusCode is set to true', async ({ playwright, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
const request = await playwright.request.newContext({ apiRequest: { failOnStatusCode: true } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a test where browser.newContext is called with this option.

/**
* An object containing an option to throw an error when API request returns status codes other than 2xx and 3xx.
*/
apiRequest?: {failOnStatusCode?: boolean;}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is auto generated from docs/, don't edit it manually. Once you add references to context-option-apirequest in corresponding classes, this file will be automatically updated when you run build.

See context-option-extrahttpheaders for example.

@@ -620,6 +620,12 @@ 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-apirequest
- `apiRequest` <[Object]>
- `failOnStatusCode` <[boolean]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I still have that option inside the apiRequest object? Or just replace the object with a more specific variable name such as failAPIRequestOnStatusCode?

I'd just replace it with a flat property.

It doesn't seem to me like the option will be present on both browser and API contexts since the browser.newContext() and playwright.request.newContext() returns different types.

It depends on where you add references to the new context-option-apirequest, see my comment below. I'd expect it to be present in both newContext methods mentioned above, so that it works for page.request.fetch() too. Arguably we can omit this feature in page.request and only have in global fetch, but I believe it'd be convenient if it could be set in the config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants