diff --git a/docs/src/api/class-apirequestcontext.md b/docs/src/api/class-apirequestcontext.md index 24b3a85b69..9163eef8c4 100644 --- a/docs/src/api/class-apirequestcontext.md +++ b/docs/src/api/class-apirequestcontext.md @@ -6,6 +6,20 @@ environment or the service to your e2e test. When used on [Page] or a [BrowserCo the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test will be logged in and vice versa. +## async method: ApiRequestContext.delete +- returns: <[ApiResponse]> + +Sends HTTP(S) [DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE) request and returns its response. +The method will populate request cookies from the context and update +context cookies from the response. The method will automatically follow redirects. + +### param: ApiRequestContext.delete.url = %%-fetch-param-url-%% +### option: ApiRequestContext.delete.params = %%-fetch-option-params-%% +### option: ApiRequestContext.delete.headers = %%-fetch-option-headers-%% +### option: ApiRequestContext.delete.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.delete.failOnStatusCode = %%-fetch-option-failonstatuscode-%% +### option: ApiRequestContext.delete.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + ## async method: ApiRequestContext.dispose All responses received through [`method: ApiRequestContext.fetch`], [`method: ApiRequestContext.get`], [`method: ApiRequestContext.post`] @@ -15,121 +29,109 @@ discards all stored responses, and makes [`method: ApiResponse.body`] throw "Res ## async method: ApiRequestContext.fetch - returns: <[ApiResponse]> -Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update +Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. ### param: ApiRequestContext.fetch.urlOrRequest - `urlOrRequest` <[string]|[Request]> -Target URL or Request to get all fetch parameters from. +Target URL or Request to get all parameters from. -### option: ApiRequestContext.fetch.params -- `params` <[Object]<[string], [string]>> - -Query parameters to be send with the URL. +### option: ApiRequestContext.fetch.params = %%-fetch-option-params-%% ### option: ApiRequestContext.fetch.method - `method` <[string]> -If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. - -### option: ApiRequestContext.fetch.headers -- `headers` <[Object]<[string], [string]>> - -Allows to set HTTP headers. +If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) or +[POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)). If not specified, GET method is used. +### option: ApiRequestContext.fetch.headers = %%-fetch-option-headers-%% ### option: ApiRequestContext.fetch.data = %%-fetch-option-data-%% ### option: ApiRequestContext.fetch.form = %%-fetch-option-form-%% ### option: ApiRequestContext.fetch.multipart = %%-fetch-option-multipart-%% - -### option: ApiRequestContext.fetch.timeout -- `timeout` <[float]> - -Request timeout in milliseconds. - -### option: ApiRequestContext.fetch.failOnStatusCode -- `failOnStatusCode` <[boolean]> - -Whether to throw on response codes other than 2xx and 3xx. By default response object is returned -for all status codes. - +### option: ApiRequestContext.fetch.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.fetch.failOnStatusCode = %%-fetch-option-failonstatuscode-%% ### option: ApiRequestContext.fetch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% - ## async method: ApiRequestContext.get - returns: <[ApiResponse]> -Sends HTTP(S) GET request and returns its response. The method will populate fetch cookies from the context and update +Sends HTTP(S) [GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) request and returns its response. +The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. -### param: ApiRequestContext.get.url -- `url` <[string]> - -Target URL. - -### option: ApiRequestContext.get.params -- `params` <[Object]<[string], [string]>> - -Query parameters to be send with the URL. - -### option: ApiRequestContext.get.headers -- `headers` <[Object]<[string], [string]>> - -Allows to set HTTP headers. - -### option: ApiRequestContext.get.timeout -- `timeout` <[float]> - -Request timeout in milliseconds. - -### option: ApiRequestContext.get.failOnStatusCode -- `failOnStatusCode` <[boolean]> - -Whether to throw on response codes other than 2xx and 3xx. By default response object is returned -for all status codes. - +### param: ApiRequestContext.get.url = %%-fetch-param-url-%% +### option: ApiRequestContext.get.params = %%-fetch-option-params-%% +### option: ApiRequestContext.get.headers = %%-fetch-option-headers-%% +### option: ApiRequestContext.get.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.get.failOnStatusCode = %%-fetch-option-failonstatuscode-%% ### option: ApiRequestContext.get.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% +## async method: ApiRequestContext.head +- returns: <[ApiResponse]> + +Sends HTTP(S) [HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) request and returns its response. +The method will populate request cookies from the context and update +context cookies from the response. The method will automatically follow redirects. + +### param: ApiRequestContext.head.url = %%-fetch-param-url-%% +### option: ApiRequestContext.head.params = %%-fetch-option-params-%% +### option: ApiRequestContext.head.headers = %%-fetch-option-headers-%% +### option: ApiRequestContext.head.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.head.failOnStatusCode = %%-fetch-option-failonstatuscode-%% +### option: ApiRequestContext.head.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + +## async method: ApiRequestContext.patch +- returns: <[ApiResponse]> + +Sends HTTP(S) [PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) request and returns its response. +The method will populate request cookies from the context and update +context cookies from the response. The method will automatically follow redirects. + +### param: ApiRequestContext.patch.url = %%-fetch-param-url-%% +### option: ApiRequestContext.patch.params = %%-fetch-option-params-%% +### option: ApiRequestContext.patch.headers = %%-fetch-option-headers-%% +### option: ApiRequestContext.patch.data = %%-fetch-option-data-%% +### option: ApiRequestContext.patch.form = %%-fetch-option-form-%% +### option: ApiRequestContext.patch.multipart = %%-fetch-option-multipart-%% +### option: ApiRequestContext.patch.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.patch.failOnStatusCode = %%-fetch-option-failonstatuscode-%% +### option: ApiRequestContext.patch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + ## async method: ApiRequestContext.post - returns: <[ApiResponse]> -Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update +Sends HTTP(S) [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request and returns its response. +The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. -### param: ApiRequestContext.post.url -- `url` <[string]> - -Target URL. - -### option: ApiRequestContext.post.params -- `params` <[Object]<[string], [string]>> - -Query parameters to be send with the URL. - -### option: ApiRequestContext.post.headers -- `headers` <[Object]<[string], [string]>> - -Allows to set HTTP headers. - +### param: ApiRequestContext.post.url = %%-fetch-param-url-%% +### option: ApiRequestContext.post.params = %%-fetch-option-params-%% +### option: ApiRequestContext.post.headers = %%-fetch-option-headers-%% ### option: ApiRequestContext.post.data = %%-fetch-option-data-%% - ### option: ApiRequestContext.post.form = %%-fetch-option-form-%% - ### option: ApiRequestContext.post.multipart = %%-fetch-option-multipart-%% - -### option: ApiRequestContext.post.timeout -- `timeout` <[float]> - -Request timeout in milliseconds. - -### option: ApiRequestContext.post.failOnStatusCode -- `failOnStatusCode` <[boolean]> - -Whether to throw on response codes other than 2xx and 3xx. By default response object is returned -for all status codes. - +### option: ApiRequestContext.post.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.post.failOnStatusCode = %%-fetch-option-failonstatuscode-%% ### option: ApiRequestContext.post.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% +## async method: ApiRequestContext.put +- returns: <[ApiResponse]> + +Sends HTTP(S) [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) request and returns its response. +The method will populate request cookies from the context and update +context cookies from the response. The method will automatically follow redirects. + +### param: ApiRequestContext.put.url = %%-fetch-param-url-%% +### option: ApiRequestContext.put.params = %%-fetch-option-params-%% +### option: ApiRequestContext.put.headers = %%-fetch-option-headers-%% +### option: ApiRequestContext.put.data = %%-fetch-option-data-%% +### option: ApiRequestContext.put.form = %%-fetch-option-form-%% +### option: ApiRequestContext.put.multipart = %%-fetch-option-multipart-%% +### option: ApiRequestContext.put.timeout = %%-fetch-option-timeout-%% +### option: ApiRequestContext.put.failOnStatusCode = %%-fetch-option-failonstatuscode-%% +### option: ApiRequestContext.put.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + ## async method: ApiRequestContext.storageState - returns: <[Object]> - `cookies` <[Array]<[Object]>> diff --git a/docs/src/api/params.md b/docs/src/api/params.md index 1585aa2cf9..f47f5e745b 100644 --- a/docs/src/api/params.md +++ b/docs/src/api/params.md @@ -297,6 +297,32 @@ Emulates consistent viewport for each page. Defaults to an 1280x720 viewport. Us Emulates consistent window screen size available inside web page via `window.screen`. Is only used when the [`option: viewport`] is set. +## fetch-param-url +- `url` <[string]> + +Target URL. + +## fetch-option-params +- `params` <[Object]<[string], [string]>> + +Query parameters to be send with the URL. + +## fetch-option-headers +- `headers` <[Object]<[string], [string]>> + +Allows to set HTTP headers. + +## fetch-option-timeout +- `timeout` <[float]> + +Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + +## fetch-option-failonstatuscode +- `failOnStatusCode` <[boolean]> + +Whether to throw on response codes other than 2xx and 3xx. By default response object is returned +for all status codes. + ## fetch-option-form - `form` <[Object]<[string], [string]|[float]|[boolean]>> diff --git a/src/client/fetch.ts b/src/client/fetch.ts index 0a702191da..109af2e500 100644 --- a/src/client/fetch.ts +++ b/src/client/fetch.ts @@ -46,6 +46,9 @@ type NewContextOptions = Omit; +type RequestWithoutBodyOptions = Omit; + export class Fetch implements api.ApiRequest { private _playwright: Playwright; constructor(playwright: Playwright) { @@ -81,28 +84,48 @@ export class FetchRequest extends ChannelOwner { + async delete(url: string, options?: RequestWithoutBodyOptions): Promise { + return this.fetch(url, { + ...options, + method: 'DELETE', + }); + } + + async head(url: string, options?: RequestWithoutBodyOptions): Promise { + return this.fetch(url, { + ...options, + method: 'HEAD', + }); + } + + async get(url: string, options?: RequestWithoutBodyOptions): Promise { return this.fetch(url, { ...options, method: 'GET', }); } - async post(url: string, options?: Omit): Promise { + async patch(url: string, options?: RequestWithBodyOptions): Promise { + return this.fetch(url, { + ...options, + method: 'PATCH', + }); + } + + async post(url: string, options?: RequestWithBodyOptions): Promise { return this.fetch(url, { ...options, method: 'POST', }); } + async put(url: string, options?: RequestWithBodyOptions): Promise { + return this.fetch(url, { + ...options, + method: 'PUT', + }); + } + async fetch(urlOrRequest: string | api.Request, options: FetchOptions = {}): Promise { return this._wrapApiCall(async (channel: channels.FetchRequestChannel) => { const request: network.Request | undefined = (urlOrRequest instanceof network.Request) ? urlOrRequest as network.Request : undefined; diff --git a/src/server/fetch.ts b/src/server/fetch.ts index b257c1e0f0..f507be4e20 100644 --- a/src/server/fetch.ts +++ b/src/server/fetch.ts @@ -134,7 +134,7 @@ export abstract class FetchRequest extends SdkObject { } let postData; - if (['POST', 'PUSH', 'PATCH'].includes(method)) + if (['POST', 'PUT', 'PATCH'].includes(method)) postData = serializePostData(params, headers); else if (params.postData || params.jsonData || params.formData || params.multipartData) throw new Error(`Method ${method} does not accept post data`); diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.ts index d1c0d334b7..e389b793a0 100644 --- a/tests/browsercontext-fetch.spec.ts +++ b/tests/browsercontext-fetch.spec.ts @@ -130,7 +130,7 @@ it('should add session cookies to request', async ({ context, server }) => { expect(req.headers.cookie).toEqual('username=John Doe'); }); -for (const method of ['get', 'post', 'fetch']) { +for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']) { it(`${method} should support queryParams`, async ({ context, server }) => { let request; const url = new URL(server.EMPTY_PAGE); diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts index b10f1f5518..43b2e503cb 100644 --- a/tests/global-fetch.spec.ts +++ b/tests/global-fetch.spec.ts @@ -38,7 +38,7 @@ it.afterAll(() => { http.globalAgent = prevAgent; }); -for (const method of ['get', 'post', 'fetch']) { +for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']) { it(`${method} should work`, async ({ playwright, server }) => { const request = await playwright.request.newContext(); const response = await request[method](server.PREFIX + '/simple.json'); @@ -49,7 +49,7 @@ for (const method of ['get', 'post', 'fetch']) { expect(response.url()).toBe(server.PREFIX + '/simple.json'); expect(response.headers()['content-type']).toBe('application/json; charset=utf-8'); expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' }); - expect(await response.text()).toBe('{"foo": "bar"}\n'); + expect(await response.text()).toBe(method === 'head' ? '' : '{"foo": "bar"}\n'); }); it(`should dispose global ${method} request`, async function({ playwright, context, server }) { diff --git a/types/types.d.ts b/types/types.d.ts index 5d9c5536b5..2c632b058e 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -11721,6 +11721,40 @@ export interface ApiRequest { * logged in and vice versa. */ export interface ApiRequestContext { + /** + * Sends HTTP(S) [DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE) request and returns its + * response. The method will populate request cookies from the context and update context cookies from the response. The + * method will automatically follow redirects. + * @param url Target URL. + * @param options + */ + delete(url: string, options?: { + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: boolean; + + /** + * Allows to set HTTP headers. + */ + 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. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + */ + timeout?: number; + }): Promise; + /** * All responses received through * [apiRequestContext.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-fetch), @@ -11734,9 +11768,9 @@ export interface ApiRequestContext { dispose(): Promise; /** - * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context - * cookies from the response. The method will automatically follow redirects. - * @param urlOrRequest Target URL or Request to get all fetch parameters from. + * Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update + * context cookies from the response. The method will automatically follow redirects. + * @param urlOrRequest Target URL or Request to get all parameters from. * @param options */ fetch(urlOrRequest: string|Request, options?: { @@ -11770,7 +11804,8 @@ export interface ApiRequestContext { ignoreHTTPSErrors?: boolean; /** - * If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. + * If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) or + * [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)). If not specified, GET method is used. */ method?: string; @@ -11803,14 +11838,15 @@ export interface ApiRequestContext { params?: { [key: string]: string; }; /** - * Request timeout in milliseconds. + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. */ timeout?: number; }): Promise; /** - * Sends HTTP(S) GET request and returns its response. The method will populate fetch cookies from the context and update - * context cookies from the response. The method will automatically follow redirects. + * Sends HTTP(S) [GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) request and returns its response. The + * method will populate request cookies from the context and update context cookies from the response. The method will + * automatically follow redirects. * @param url Target URL. * @param options */ @@ -11836,14 +11872,120 @@ export interface ApiRequestContext { params?: { [key: string]: string; }; /** - * Request timeout in milliseconds. + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. */ timeout?: number; }): Promise; /** - * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context - * cookies from the response. The method will automatically follow redirects. + * Sends HTTP(S) [HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) request and returns its response. + * The method will populate request cookies from the context and update context cookies from the response. The method will + * automatically follow redirects. + * @param url Target URL. + * @param options + */ + head(url: string, options?: { + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: boolean; + + /** + * Allows to set HTTP headers. + */ + 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. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + */ + timeout?: number; + }): Promise; + + /** + * Sends HTTP(S) [PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) request and returns its response. + * The method will populate request cookies from the context and update context cookies from the response. The method will + * automatically follow redirects. + * @param url Target URL. + * @param options + */ + patch(url: string, options?: { + /** + * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and + * `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` header will + * be set to `application/octet-stream` if not explicitly set. + */ + data?: string|Buffer|Serializable; + + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: boolean; + + /** + * Provides an object that will be serialized as html form using `application/x-www-form-urlencoded` encoding and sent as + * this request body. If this parameter is specified `content-type` header will be set to + * `application/x-www-form-urlencoded` unless explicitly provided. + */ + form?: { [key: string]: string|number|boolean; }; + + /** + * Allows to set HTTP headers. + */ + headers?: { [key: string]: string; }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * Provides an object that will be serialized as html form using `multipart/form-data` encoding and sent as this request + * body. If this parameter is specified `content-type` header will be set to `multipart/form-data` unless explicitly + * provided. File values can be passed either as [`fs.ReadStream`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) + * or as file-like object containing file name, mime-type and its content. + */ + multipart?: { [key: string]: string|number|boolean|ReadStream|{ + /** + * File name + */ + name: string; + + /** + * File type + */ + mimeType: string; + + /** + * File content + */ + buffer: Buffer; + }; }; + + /** + * Query parameters to be send with the URL. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + */ + timeout?: number; + }): Promise; + + /** + * Sends HTTP(S) [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request and returns its response. + * The method will populate request cookies from the context and update context cookies from the response. The method will + * automatically follow redirects. * @param url Target URL. * @param options */ @@ -11906,7 +12048,78 @@ export interface ApiRequestContext { params?: { [key: string]: string; }; /** - * Request timeout in milliseconds. + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + */ + timeout?: number; + }): Promise; + + /** + * Sends HTTP(S) [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) request and returns its response. The + * method will populate request cookies from the context and update context cookies from the response. The method will + * automatically follow redirects. + * @param url Target URL. + * @param options + */ + put(url: string, options?: { + /** + * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and + * `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` header will + * be set to `application/octet-stream` if not explicitly set. + */ + data?: string|Buffer|Serializable; + + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: boolean; + + /** + * Provides an object that will be serialized as html form using `application/x-www-form-urlencoded` encoding and sent as + * this request body. If this parameter is specified `content-type` header will be set to + * `application/x-www-form-urlencoded` unless explicitly provided. + */ + form?: { [key: string]: string|number|boolean; }; + + /** + * Allows to set HTTP headers. + */ + headers?: { [key: string]: string; }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * Provides an object that will be serialized as html form using `multipart/form-data` encoding and sent as this request + * body. If this parameter is specified `content-type` header will be set to `multipart/form-data` unless explicitly + * provided. File values can be passed either as [`fs.ReadStream`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) + * or as file-like object containing file name, mime-type and its content. + */ + multipart?: { [key: string]: string|number|boolean|ReadStream|{ + /** + * File name + */ + name: string; + + /** + * File type + */ + mimeType: string; + + /** + * File content + */ + buffer: Buffer; + }; }; + + /** + * Query parameters to be send with the URL. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. */ timeout?: number; }): Promise;