Rename page.setHTTPHeaders into page.setExtraHTTPHeaders

This patch:
- renames page.setHTTPHeaders into page.setExtraHTTPHeaders
- starts using Map instead of Object to align with other headers
  arguments

Fixes #112.
This commit is contained in:
Andrey Lushnikov 2017-07-27 12:25:52 -07:00
parent bbde8fd1c2
commit e39d8602b0
5 changed files with 31 additions and 24 deletions

View File

@ -48,7 +48,7 @@
+ [page.reload(options)](#pagereloadoptions) + [page.reload(options)](#pagereloadoptions)
+ [page.screenshot([options])](#pagescreenshotoptions) + [page.screenshot([options])](#pagescreenshotoptions)
+ [page.setContent(html)](#pagesetcontenthtml) + [page.setContent(html)](#pagesetcontenthtml)
+ [page.setHTTPHeaders(headers)](#pagesethttpheadersheaders) + [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
+ [page.setInPageCallback(name, callback)](#pagesetinpagecallbackname-callback) + [page.setInPageCallback(name, callback)](#pagesetinpagecallbackname-callback)
+ [page.setRequestInterceptor(interceptor)](#pagesetrequestinterceptorinterceptor) + [page.setRequestInterceptor(interceptor)](#pagesetrequestinterceptorinterceptor)
+ [page.setUserAgent(userAgent)](#pagesetuseragentuseragent) + [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. - `html` <[string]> HTML markup to assign to the page.
- returns: <[Promise]> Promise which resolves when the content is successfully assigned. - returns: <[Promise]> Promise which resolves when the content is successfully assigned.
#### page.setHTTPHeaders(headers) #### page.setExtraHTTPHeaders(headers)
- `headers` <[Object]> Key-value set of additional http headers to be sent with every request. - `headers` <[Map]> A map of additional http headers to be sent with every request.
- returns: <[Promise]> Promise which resolves when additional headers are installed - 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) #### page.setInPageCallback(name, callback)
- `name` <[string]> Name of the callback to be assigned on window object - `name` <[string]> Name of the callback to be assigned on window object
- `callback` <[function]> Callback function which will be called in puppeteer's context. - `callback` <[function]> Callback function which will be called in puppeteer's context.

View File

@ -24,9 +24,10 @@ class NetworkManager extends EventEmitter {
super(); super();
this._client = client; this._client = client;
this._requestInterceptor = null; this._requestInterceptor = null;
/* @type {!Map<string, !Request>} */ /** @type {!Map<string, !Request>} */
this._idToRequest = new Map(); this._idToRequest = new Map();
this._httpHeaders = {}; /** @type {!Map<string, string>} */
this._extraHTTPHeaders = new Map();
this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this)); this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this));
this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this)); this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this));
@ -36,22 +37,22 @@ class NetworkManager extends EventEmitter {
} }
/** /**
* @param {!Object} headers * @param {!Map<string, string>} extraHTTPHeaders
* @return {!Promise} * @return {!Promise}
*/ */
async setHTTPHeaders(headers) { async setExtraHTTPHeaders(extraHTTPHeaders) {
this._httpHeaders = {}; this._extraHTTPHeaders = new Map(extraHTTPHeaders);
// Note: header names are case-insensitive. let headers = {};
for (let key of Object.keys(headers)) for (let entry of extraHTTPHeaders.entries())
this._httpHeaders[key.toLowerCase()] = headers[key]; headers[entry[0]] = entry[1];
return this._client.send('Network.setExtraHTTPHeaders', { headers }); await this._client.send('Network.setExtraHTTPHeaders', { headers });
} }
/** /**
* @return {!Object} * @return {!Map<string, string>}
*/ */
httpHeaders() { extraHTTPHeaders() {
return Object.assign({}, this._httpHeaders); return new Map(this._extraHTTPHeaders);
} }
/** /**

View File

@ -161,11 +161,11 @@ class Page extends EventEmitter {
} }
/** /**
* @param {!Object} headers * @param {!Map<string, string>} headers
* @return {!Promise} * @return {!Promise}
*/ */
async setHTTPHeaders(headers) { async setExtraHTTPHeaders(headers) {
return this._networkManager.setHTTPHeaders(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 listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response));
const result = watcher.waitForNavigation(); const result = watcher.waitForNavigation();
const referrer = this._networkManager.httpHeaders()['referer']; const referrer = this._networkManager.extraHTTPHeaders().get('referer');
try { try {
// Await for the command to throw exception in case of illegal arguments. // Await for the command to throw exception in case of illegal arguments.
await this._client.send('Page.navigate', {url, referrer}); await this._client.send('Page.navigate', {url, referrer});

View File

@ -284,7 +284,7 @@ class WebPage {
*/ */
set customHeaders(value) { set customHeaders(value) {
this._customHeaders = value; this._customHeaders = value;
await(this._page.setHTTPHeaders(value)); await(this._page.setExtraHTTPHeaders(new Map(Object.entries(value))));
} }
/** /**

View File

@ -611,9 +611,9 @@ describe('Puppeteer', function() {
expect(response.ok).toBe(true); expect(response.ok).toBe(true);
})); }));
it('should show custom HTTP headers', SX(async function() { it('should show custom HTTP headers', SX(async function() {
await page.setHTTPHeaders({ await page.setExtraHTTPHeaders(new Map(Object.entries({
foo: 'bar' foo: 'bar'
}); })));
page.setRequestInterceptor(request => { page.setRequestInterceptor(request => {
expect(request.headers.get('foo')).toBe('bar'); expect(request.headers.get('foo')).toBe('bar');
request.continue(); request.continue();
@ -1046,9 +1046,11 @@ describe('Puppeteer', function() {
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari'); expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
})); }));
}); });
describe('Page.setHTTPHeaders', function() { describe('Page.setExtraHTTPHeaders', function() {
it('should work', SX(async function() { it('should work', SX(async function() {
page.setHTTPHeaders({'foo': 'bar'}); await page.setExtraHTTPHeaders(new Map(Object.entries({
foo: 'bar'
})));
page.navigate(EMPTY_PAGE); page.navigate(EMPTY_PAGE);
let request = await server.waitForRequest('/empty.html'); let request = await server.waitForRequest('/empty.html');
expect(request.headers['foo']).toBe('bar'); expect(request.headers['foo']).toBe('bar');