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.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.

View File

@ -24,9 +24,10 @@ class NetworkManager extends EventEmitter {
super();
this._client = client;
this._requestInterceptor = null;
/* @type {!Map<string, !Request>} */
/** @type {!Map<string, !Request>} */
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.requestIntercepted', this._onRequestIntercepted.bind(this));
@ -36,22 +37,22 @@ class NetworkManager extends EventEmitter {
}
/**
* @param {!Object} headers
* @param {!Map<string, string>} 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<string, string>}
*/
httpHeaders() {
return Object.assign({}, this._httpHeaders);
extraHTTPHeaders() {
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}
*/
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});

View File

@ -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))));
}
/**

View File

@ -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');