From b9b6ca1825c0329dc22dfab047e819d861ed15d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Mon, 5 Aug 2019 19:26:17 -0300 Subject: [PATCH] fix(network): be able to remove headers using request.continue (#4797) I think this was a regression caused here https://github.com/GoogleChrome/puppeteer/pull/4265/files#diff-d2ac7cb061b0c51644d0a5d6140e3a32R446 Fix #4743 --- .../puppeteer-firefox/lib/NetworkManager.js | 2 +- lib/NetworkManager.js | 6 ++++-- test/requestinterception.spec.js | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/experimental/puppeteer-firefox/lib/NetworkManager.js b/experimental/puppeteer-firefox/lib/NetworkManager.js index deb4e2d9..4ff57bb2 100644 --- a/experimental/puppeteer-firefox/lib/NetworkManager.js +++ b/experimental/puppeteer-firefox/lib/NetworkManager.js @@ -158,7 +158,7 @@ class Request { } = overrides; await this._session.send('Network.resumeSuspendedRequest', { requestId: this._id, - headers: headers ? Object.entries(headers).map(([name, value]) => ({name, value})) : undefined, + headers: headers ? Object.entries(headers).filter(([, value]) => !Object.is(value, undefined)).map(([name, value]) => ({name, value})) : undefined, }).catch(error => { debugError(error); }); diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index 37a59d05..ea7ce4f5 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -723,8 +723,10 @@ class SecurityDetails { */ function headersArray(headers) { const result = []; - for (const name in headers) - result.push({name, value: headers[name] + ''}); + for (const name in headers) { + if (!Object.is(headers[name], undefined)) + result.push({name, value: headers[name] + ''}); + } return result; } diff --git a/test/requestinterception.spec.js b/test/requestinterception.spec.js index 5e2a0ed6..e0c7d9b7 100644 --- a/test/requestinterception.spec.js +++ b/test/requestinterception.spec.js @@ -72,6 +72,24 @@ module.exports.addTests = function({testRunner, expect, CHROME}) { }); await page.goto(server.PREFIX + '/rrredirect'); }); + // @see https://github.com/GoogleChrome/puppeteer/issues/4743 + it('should be able to remove headers', async({page, server}) => { + await page.setRequestInterception(true); + page.on('request', request => { + const headers = Object.assign({}, request.headers(), { + foo: 'bar', + origin: undefined, // remove "origin" header + }); + request.continue({ headers }); + }); + + const [serverRequest] = await Promise.all([ + server.waitForRequest('/empty.html'), + page.goto(server.PREFIX + '/empty.html') + ]); + + expect(serverRequest.headers.origin).toBe(undefined); + }); it('should contain referer header', async({page, server}) => { await page.setRequestInterception(true); const requests = [];