From 464f3b4ca28563d799f40a3c647e3567b4f1cd68 Mon Sep 17 00:00:00 2001 From: Ali Ahmad Date: Tue, 4 Sep 2018 00:06:58 +0500 Subject: [PATCH] feat(page): Add 'referer' as a parameter to Page.goto() (#3128) If referer is passed to the options object its value will be used as the referer instead of the value set by `Page.setExtraHTTPHeaders()`. This is the correct way to set referer header: otherwise, the `referer` header will override all the document subrequests. Fixes #3090. --- docs/api.md | 1 + lib/Page.js | 2 +- test/page.spec.js | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 29a09cb8..c598b90a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1318,6 +1318,7 @@ Navigate to the next page in history. - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more than 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more than 2 network connections for at least `500` ms. + - `referer` <[string]> Referer header value. If provided it will take prefrence over the referer header value set by [page.setExtraHTTPHeaders()](#pagesetextrahttpheadersheaders). - returns: <[Promise]> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. The `page.goto` will throw an error if: diff --git a/lib/Page.js b/lib/Page.js index 4ba3ef84..e53cb672 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -577,7 +577,7 @@ class Page extends EventEmitter { * @return {!Promise} */ async goto(url, options = {}) { - const referrer = this._networkManager.extraHTTPHeaders()['referer']; + const referrer = typeof options.referer === 'string' ? options.referer : this._networkManager.extraHTTPHeaders()['referer']; /** @type {Map} */ const requests = new Map(); diff --git a/test/page.spec.js b/test/page.spec.js index 0a4abf79..66189b10 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -754,6 +754,17 @@ module.exports.addTests = function({testRunner, expect, headless}) { } expect(error.message).toContain(url); }); + it('should send referer', async({page, server}) => { + await page.setRequestInterception(true); + page.on('request', request => request.continue()); + const [request] = await Promise.all([ + server.waitForRequest('/grid.html'), + page.goto(server.PREFIX + '/grid.html', { + referer: 'http://google.com/', + }), + ]); + expect(request.headers['referer']).toBe('http://google.com/'); + }); }); describe('Page.waitForNavigation', function() {