diff --git a/docs/api.md b/docs/api.md index a6ced9cf3e1..07120c4400a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -48,7 +48,7 @@ + [page.reload(options)](#pagereloadoptions) + [page.screenshot([options])](#pagescreenshotoptions) + [page.setContent(html)](#pagesetcontenthtml) - + [page.setHTTPHeaders(headers)](#pagesethttpheadersheaders) + + [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders) + [page.setInPageCallback(name, callback)](#pagesetinpagecallbackname-callback) + [page.setRequestInterceptor(interceptor)](#pagesetrequestinterceptorinterceptor) + [page.setUserAgent(userAgent)](#pagesetuseragentuseragent) @@ -544,10 +544,14 @@ Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#ke - `html` <[string]> HTML markup to assign to the page. - returns: <[Promise]> Promise which resolves when the content is successfully assigned. -#### page.setHTTPHeaders(headers) -- `headers` <[Object]> Key-value set of additional http headers to be sent with every request. +#### page.setExtraHTTPHeaders(headers) +- `headers` <[Map]> A map of additional http headers to be sent with every request. - returns: <[Promise]> Promise which resolves when additional headers are installed +The extra HTTP headers will be sent with every request the page initiates. + +> **NOTE** page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests. + #### page.setInPageCallback(name, callback) - `name` <[string]> Name of the callback to be assigned on window object - `callback` <[function]> Callback function which will be called in puppeteer's context. diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index 91cc681339f..7e997abedc9 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -24,9 +24,10 @@ class NetworkManager extends EventEmitter { super(); this._client = client; this._requestInterceptor = null; - /* @type {!Map} */ + /** @type {!Map} */ this._idToRequest = new Map(); - this._httpHeaders = {}; + /** @type {!Map} */ + this._extraHTTPHeaders = new Map(); this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this)); this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this)); @@ -36,22 +37,22 @@ class NetworkManager extends EventEmitter { } /** - * @param {!Object} headers + * @param {!Map} extraHTTPHeaders * @return {!Promise} */ - async setHTTPHeaders(headers) { - this._httpHeaders = {}; - // Note: header names are case-insensitive. - for (let key of Object.keys(headers)) - this._httpHeaders[key.toLowerCase()] = headers[key]; - return this._client.send('Network.setExtraHTTPHeaders', { headers }); + async setExtraHTTPHeaders(extraHTTPHeaders) { + this._extraHTTPHeaders = new Map(extraHTTPHeaders); + let headers = {}; + for (let entry of extraHTTPHeaders.entries()) + headers[entry[0]] = entry[1]; + await this._client.send('Network.setExtraHTTPHeaders', { headers }); } /** - * @return {!Object} + * @return {!Map} */ - httpHeaders() { - return Object.assign({}, this._httpHeaders); + extraHTTPHeaders() { + return new Map(this._extraHTTPHeaders); } /** diff --git a/lib/Page.js b/lib/Page.js index 8a172640618..d886b3ba814 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -161,11 +161,11 @@ class Page extends EventEmitter { } /** - * @param {!Object} headers + * @param {!Map} headers * @return {!Promise} */ - async setHTTPHeaders(headers) { - return this._networkManager.setHTTPHeaders(headers); + async setExtraHTTPHeaders(headers) { + return this._networkManager.setExtraHTTPHeaders(headers); } /** @@ -250,7 +250,7 @@ class Page extends EventEmitter { const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response)); const result = watcher.waitForNavigation(); - const referrer = this._networkManager.httpHeaders()['referer']; + const referrer = this._networkManager.extraHTTPHeaders().get('referer'); try { // Await for the command to throw exception in case of illegal arguments. await this._client.send('Page.navigate', {url, referrer}); diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index 076611edca9..82f650ac2bf 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -284,7 +284,7 @@ class WebPage { */ set customHeaders(value) { this._customHeaders = value; - await(this._page.setHTTPHeaders(value)); + await(this._page.setExtraHTTPHeaders(new Map(Object.entries(value)))); } /** diff --git a/test/test.js b/test/test.js index f8414c4f779..ca51a7a22da 100644 --- a/test/test.js +++ b/test/test.js @@ -611,9 +611,9 @@ describe('Puppeteer', function() { expect(response.ok).toBe(true); })); it('should show custom HTTP headers', SX(async function() { - await page.setHTTPHeaders({ + await page.setExtraHTTPHeaders(new Map(Object.entries({ foo: 'bar' - }); + }))); page.setRequestInterceptor(request => { expect(request.headers.get('foo')).toBe('bar'); request.continue(); @@ -1046,9 +1046,11 @@ describe('Puppeteer', function() { expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari'); })); }); - describe('Page.setHTTPHeaders', function() { + describe('Page.setExtraHTTPHeaders', function() { it('should work', SX(async function() { - page.setHTTPHeaders({'foo': 'bar'}); + await page.setExtraHTTPHeaders(new Map(Object.entries({ + foo: 'bar' + }))); page.navigate(EMPTY_PAGE); let request = await server.waitForRequest('/empty.html'); expect(request.headers['foo']).toBe('bar');