Skip to content

Commit

Permalink
feat(page): Add 'referer' as a parameter to Page.goto() (#3128)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aliahmad490 authored and aslushnikov committed Sep 3, 2018
1 parent 1702928 commit 464f3b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/Page.js
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions test/page.spec.js
Expand Up @@ -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() {
Expand Down

0 comments on commit 464f3b4

Please sign in to comment.