From 8d751cfe507bbfee229b89f0636fc7f63235de99 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 6 Feb 2025 15:16:45 +0000 Subject: [PATCH] fix(fetch): filter out undefined `params` (#34654) --- packages/playwright-core/src/client/fetch.ts | 6 ++++-- tests/library/browsercontext-fetch.spec.ts | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index 58928532ac..efacce417d 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -416,8 +416,10 @@ function objectToArray(map?: { [key: string]: any }): NameValue[] | undefined { if (!map) return undefined; const result = []; - for (const [name, value] of Object.entries(map)) - result.push({ name, value: String(value) }); + for (const [name, value] of Object.entries(map)) { + if (value !== undefined) + result.push({ name, value: String(value) }); + } return result; } diff --git a/tests/library/browsercontext-fetch.spec.ts b/tests/library/browsercontext-fetch.spec.ts index 38848dd497..f23b59f97c 100644 --- a/tests/library/browsercontext-fetch.spec.ts +++ b/tests/library/browsercontext-fetch.spec.ts @@ -1183,7 +1183,7 @@ it('should send secure cookie over http for localhost', async ({ page, server }) expect(serverRequest.headers.cookie).toBe('a=v'); }); -it('should accept bool and numeric params', async ({ page, server }) => { +it('should accept bool and numeric params and filter out undefined', async ({ page, server }) => { let request; const url = new URL(server.EMPTY_PAGE); url.searchParams.set('str', 's'); @@ -1200,6 +1200,7 @@ it('should accept bool and numeric params', async ({ page, server }) => { 'num': 10, 'bool': true, 'bool2': false, + 'none': undefined, } }); const params = new URLSearchParams(request!.url.substr(request!.url.indexOf('?'))); @@ -1207,6 +1208,7 @@ it('should accept bool and numeric params', async ({ page, server }) => { expect(params.get('num')).toEqual('10'); expect(params.get('bool')).toEqual('true'); expect(params.get('bool2')).toEqual('false'); + expect(params.has('none')).toBe(false); }); it('should abort requests when browser context closes', async ({ contextFactory, server }) => {