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.
This commit is contained in:
Ali Ahmad 2018-09-04 00:06:58 +05:00 committed by Andrey Lushnikov
parent 17029281a9
commit 464f3b4ca2
3 changed files with 13 additions and 1 deletions

View File

@ -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]<?[Response]>> 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:

View File

@ -577,7 +577,7 @@ class Page extends EventEmitter {
* @return {!Promise<?Puppeteer.Response>}
*/
async goto(url, options = {}) {
const referrer = this._networkManager.extraHTTPHeaders()['referer'];
const referrer = typeof options.referer === 'string' ? options.referer : this._networkManager.extraHTTPHeaders()['referer'];
/** @type {Map<string, !Puppeteer.Request>} */
const requests = new Map();

View File

@ -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() {