From 0469a7552b715e5d3227f654e053699e249942d4 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 5 Oct 2021 16:36:15 -0700 Subject: [PATCH] fix(fetch): do not allow to call get/post with request (#9326) --- docs/src/api/class-fetchrequest.md | 12 ++++++------ src/client/fetch.ts | 8 ++++---- tests/browsercontext-fetch.spec.ts | 3 ++- tests/download.spec.ts | 2 +- tests/page/page-request-fulfill.spec.ts | 2 +- types/types.d.ts | 19 +++++++++---------- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/docs/src/api/class-fetchrequest.md b/docs/src/api/class-fetchrequest.md index 27aad8fded..4fcd187552 100644 --- a/docs/src/api/class-fetchrequest.md +++ b/docs/src/api/class-fetchrequest.md @@ -63,10 +63,10 @@ for all status codes. 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. -### param: FetchRequest.get.urlOrRequest -- `urlOrRequest` <[string]|[Request]> +### param: FetchRequest.get.url +- `url` <[string]> -Target URL or Request to get all fetch parameters from. +Target URL. ### option: FetchRequest.get.params - `params` <[Object]<[string], [string]>> @@ -97,10 +97,10 @@ for all status codes. 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: FetchRequest.post.urlOrRequest -- `urlOrRequest` <[string]|[Request]> +### param: FetchRequest.post.url +- `url` <[string]> -Target URL or Request to get all fetch parameters from. +Target URL. ### option: FetchRequest.post.params - `params` <[Object]<[string], [string]>> diff --git a/src/client/fetch.ts b/src/client/fetch.ts index cd03432506..4af738da19 100644 --- a/src/client/fetch.ts +++ b/src/client/fetch.ts @@ -56,7 +56,7 @@ export class FetchRequest extends ChannelOwner { - return this.fetch(urlOrRequest, { + return this.fetch(url, { ...options, method: 'GET', }); } - async post(urlOrRequest: string | api.Request, options?: Omit): Promise { - return this.fetch(urlOrRequest, { + async post(url: string, options?: Omit): Promise { + return this.fetch(url, { ...options, method: 'POST', }); diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.ts index e83e27a28f..ed23bc92d9 100644 --- a/tests/browsercontext-fetch.spec.ts +++ b/tests/browsercontext-fetch.spec.ts @@ -710,7 +710,8 @@ it('should override request parameters', async function({ context, page, server ]); const [req] = await Promise.all([ server.waitForRequest('/empty.html'), - context.request.post(pageReq, { + context.request.fetch(pageReq, { + method: 'POST', headers: { 'foo': 'bar' }, diff --git a/tests/download.spec.ts b/tests/download.spec.ts index 26bc019694..959ed52fa6 100644 --- a/tests/download.spec.ts +++ b/tests/download.spec.ts @@ -601,7 +601,7 @@ it('should be able to download a inline PDF file', async ({ browser, server, ass const page = await browser.newPage({ acceptDownloads: true }); await page.goto(server.EMPTY_PAGE); await page.route('**/empty.pdf', async route => { - const response = await page.context().request.get(route.request()); + const response = await page.context().request.fetch(route.request()); await route.fulfill({ response, headers: { diff --git a/tests/page/page-request-fulfill.spec.ts b/tests/page/page-request-fulfill.spec.ts index 971ddb0f5d..d9dd74c45f 100644 --- a/tests/page/page-request-fulfill.spec.ts +++ b/tests/page/page-request-fulfill.spec.ts @@ -239,7 +239,7 @@ it('should fulfill with fetch result and overrides', async ({ page, server, isEl it('should fetch original request and fulfill', async ({ page, server, isElectron }) => { it.fixme(isElectron, 'error: Browser context management is not supported.'); await page.route('**/*', async route => { - const response = await page.request.get(route.request()); + const response = await page.request.fetch(route.request()); route.fulfill({ response, }); diff --git a/types/types.d.ts b/types/types.d.ts index fbe3daf678..28ff99f817 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -12665,9 +12665,9 @@ export interface FetchRequest { /** * All responses received through * [fetchRequest.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-fetch), - * [fetchRequest.get(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), - * [fetchRequest.post(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post) and - * other methods are stored in the memory, so that you can later call + * [fetchRequest.get(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), + * [fetchRequest.post(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post) and other + * methods are stored in the memory, so that you can later call * [fetchResponse.body()](https://playwright.dev/docs/api/class-fetchresponse#fetch-response-body). This method discards * all stored responses, and makes * [fetchResponse.body()](https://playwright.dev/docs/api/class-fetchresponse#fetch-response-body) throw "Response @@ -12753,10 +12753,10 @@ export interface FetchRequest { /** * 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. - * @param urlOrRequest Target URL or Request to get all fetch parameters from. + * @param url Target URL. * @param options */ - get(urlOrRequest: string|Request, options?: { + get(url: string, options?: { /** * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. */ @@ -12786,10 +12786,10 @@ export interface FetchRequest { /** * 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. + * @param url Target URL. * @param options */ - post(urlOrRequest: string|Request, options?: { + post(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 @@ -13387,9 +13387,8 @@ export const firefox: BrowserType; */ export const newRequest: (options?: { /** - * When using - * [fetchRequest.get(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), - * [fetchRequest.post(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post), + * When using [fetchRequest.get(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), + * [fetchRequest.post(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post), * [fetchRequest.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-fetch) it * takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) * constructor for building the corresponding URL. Examples: