mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
[api] use object instead of map for headers (#567)
This patch: - switches to objects instead of maps for headers (in Request, Response and page.setExtraHTTPHeaders) - converts all header names to lower case Fixes #547, fixes #509
This commit is contained in:
parent
39b9081747
commit
66912a7277
@ -693,7 +693,7 @@ Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#ke
|
|||||||
- returns: <[Promise]>
|
- returns: <[Promise]>
|
||||||
|
|
||||||
#### page.setExtraHTTPHeaders(headers)
|
#### page.setExtraHTTPHeaders(headers)
|
||||||
- `headers` <[Map]> A map of additional http headers to be sent with every request.
|
- `headers` <[Object]> An object containing additional http headers to be sent with every request.
|
||||||
- returns: <[Promise]>
|
- returns: <[Promise]>
|
||||||
|
|
||||||
The extra HTTP headers will be sent with every request the page initiates.
|
The extra HTTP headers will be sent with every request the page initiates.
|
||||||
@ -1244,13 +1244,13 @@ Exception is immediately thrown if the request interception is not enabled.
|
|||||||
- `url` <[string]> If set, the request url will be changed
|
- `url` <[string]> If set, the request url will be changed
|
||||||
- `method` <[string]> If set changes the request method (e.g. `GET` or `POST`)
|
- `method` <[string]> If set changes the request method (e.g. `GET` or `POST`)
|
||||||
- `postData` <[string]> If set changes the post data of request
|
- `postData` <[string]> If set changes the post data of request
|
||||||
- `headers` <[Map]> If set changes the request HTTP headers
|
- `headers` <[Object]> If set changes the request HTTP headers
|
||||||
|
|
||||||
Continues request with optional request overrides. To use this, request interception should be enabled with `page.setRequestInterceptionEnabled`.
|
Continues request with optional request overrides. To use this, request interception should be enabled with `page.setRequestInterceptionEnabled`.
|
||||||
Exception is immediately thrown if the request interception is not enabled.
|
Exception is immediately thrown if the request interception is not enabled.
|
||||||
|
|
||||||
#### request.headers
|
#### request.headers
|
||||||
- <[Map]> A map of HTTP headers associated with the request.
|
- <[Object]> An object with HTTP headers associated with the request. All header names are lower-case.
|
||||||
|
|
||||||
#### request.method
|
#### request.method
|
||||||
- <[string]>
|
- <[string]>
|
||||||
@ -1278,7 +1278,7 @@ Contains the URL of the request.
|
|||||||
- returns: <Promise<[Buffer]>> Promise which resolves to a buffer with response body.
|
- returns: <Promise<[Buffer]>> Promise which resolves to a buffer with response body.
|
||||||
|
|
||||||
#### response.headers
|
#### response.headers
|
||||||
- <[Map]> A map of HTTP headers associated with the response.
|
- <[Object]> An object with HTTP headers associated with the response. All header names are lower-case.
|
||||||
|
|
||||||
#### response.json()
|
#### response.json()
|
||||||
- returns: <Promise<[Object]>> Promise which resolves to a JSON representation of response body.
|
- returns: <Promise<[Object]>> Promise which resolves to a JSON representation of response body.
|
||||||
|
@ -29,8 +29,8 @@ class NetworkManager extends EventEmitter {
|
|||||||
this._requestIdToRequest = new Map();
|
this._requestIdToRequest = new Map();
|
||||||
/** @type {!Map<string, !Request>} */
|
/** @type {!Map<string, !Request>} */
|
||||||
this._interceptionIdToRequest = new Map();
|
this._interceptionIdToRequest = new Map();
|
||||||
/** @type {!Map<string, string>} */
|
/** @type {!Object<string, string>} */
|
||||||
this._extraHTTPHeaders = new Map();
|
this._extraHTTPHeaders = {};
|
||||||
|
|
||||||
this._requestInterceptionEnabled = false;
|
this._requestInterceptionEnabled = false;
|
||||||
/** @type {!Multimap<string, string>} */
|
/** @type {!Multimap<string, string>} */
|
||||||
@ -46,22 +46,21 @@ class NetworkManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Map<string, string>} extraHTTPHeaders
|
* @param {!Object<string, string>} extraHTTPHeaders
|
||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async setExtraHTTPHeaders(extraHTTPHeaders) {
|
async setExtraHTTPHeaders(extraHTTPHeaders) {
|
||||||
this._extraHTTPHeaders = new Map(extraHTTPHeaders);
|
this._extraHTTPHeaders = {};
|
||||||
const headers = {};
|
for (const key of Object.keys(extraHTTPHeaders))
|
||||||
for (const entry of extraHTTPHeaders.entries())
|
this._extraHTTPHeaders[key.toLowerCase()] = extraHTTPHeaders[key];
|
||||||
headers[entry[0]] = entry[1];
|
await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
|
||||||
await this._client.send('Network.setExtraHTTPHeaders', { headers });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {!Map<string, string>}
|
* @return {!Object<string, string>}
|
||||||
*/
|
*/
|
||||||
extraHTTPHeaders() {
|
extraHTTPHeaders() {
|
||||||
return new Map(this._extraHTTPHeaders);
|
return Object.assign({}, this._extraHTTPHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -226,7 +225,9 @@ class Request {
|
|||||||
this.url = url;
|
this.url = url;
|
||||||
this.method = payload.method;
|
this.method = payload.method;
|
||||||
this.postData = payload.postData;
|
this.postData = payload.postData;
|
||||||
this.headers = new Map(Object.entries(payload.headers));
|
this.headers = {};
|
||||||
|
for (const key of Object.keys(payload.headers))
|
||||||
|
this.headers[key.toLowerCase()] = payload.headers[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -246,18 +247,12 @@ class Request {
|
|||||||
console.assert(this._interceptionId, 'Request Interception is not enabled!');
|
console.assert(this._interceptionId, 'Request Interception is not enabled!');
|
||||||
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
||||||
this._interceptionHandled = true;
|
this._interceptionHandled = true;
|
||||||
let headers = undefined;
|
|
||||||
if (overrides.headers) {
|
|
||||||
headers = {};
|
|
||||||
for (const entry of overrides.headers)
|
|
||||||
headers[entry[0]] = entry[1];
|
|
||||||
}
|
|
||||||
this._client.send('Network.continueInterceptedRequest', {
|
this._client.send('Network.continueInterceptedRequest', {
|
||||||
interceptionId: this._interceptionId,
|
interceptionId: this._interceptionId,
|
||||||
url: overrides.url,
|
url: overrides.url,
|
||||||
method: overrides.method,
|
method: overrides.method,
|
||||||
postData: overrides.postData,
|
postData: overrides.postData,
|
||||||
headers: headers
|
headers: overrides.headers,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,10 +283,12 @@ class Response {
|
|||||||
this._request = request;
|
this._request = request;
|
||||||
this._contentPromise = null;
|
this._contentPromise = null;
|
||||||
|
|
||||||
this.headers = new Map(Object.entries(headers));
|
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.ok = status >= 200 && status <= 299;
|
this.ok = status >= 200 && status <= 299;
|
||||||
this.url = request.url;
|
this.url = request.url;
|
||||||
|
this.headers = {};
|
||||||
|
for (const key of Object.keys(headers))
|
||||||
|
this.headers[key.toLowerCase()] = headers[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -241,7 +241,7 @@ class Page extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Map<string, string>} headers
|
* @param {!Object<string, string>} headers
|
||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async setExtraHTTPHeaders(headers) {
|
async setExtraHTTPHeaders(headers) {
|
||||||
@ -344,7 +344,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.extraHTTPHeaders().get('referer');
|
const referrer = this._networkManager.extraHTTPHeaders()['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});
|
||||||
|
@ -218,9 +218,4 @@ class Helper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
|
|
||||||
if (!Object.entries)
|
|
||||||
Object.entries = obj => Object.keys(obj).map(key => [key, obj[key]]);
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = Helper;
|
module.exports = Helper;
|
||||||
|
16
test/test.js
16
test/test.js
@ -802,7 +802,7 @@ describe('Page', function() {
|
|||||||
await page.setRequestInterceptionEnabled(true);
|
await page.setRequestInterceptionEnabled(true);
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.url).toContain('empty.html');
|
expect(request.url).toContain('empty.html');
|
||||||
expect(request.headers.has('User-Agent')).toBeTruthy();
|
expect(request.headers['user-agent']).toBeTruthy();
|
||||||
expect(request.method).toBe('GET');
|
expect(request.method).toBe('GET');
|
||||||
expect(request.postData).toBe(undefined);
|
expect(request.postData).toBe(undefined);
|
||||||
request.continue();
|
request.continue();
|
||||||
@ -811,12 +811,12 @@ describe('Page', 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.setExtraHTTPHeaders(new Map(Object.entries({
|
await page.setExtraHTTPHeaders({
|
||||||
foo: 'bar'
|
foo: 'bar'
|
||||||
})));
|
});
|
||||||
await page.setRequestInterceptionEnabled(true);
|
await page.setRequestInterceptionEnabled(true);
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.headers.get('foo')).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
request.continue();
|
request.continue();
|
||||||
});
|
});
|
||||||
const response = await page.goto(EMPTY_PAGE);
|
const response = await page.goto(EMPTY_PAGE);
|
||||||
@ -839,8 +839,8 @@ describe('Page', function() {
|
|||||||
it('should amend HTTP headers', SX(async function() {
|
it('should amend HTTP headers', SX(async function() {
|
||||||
await page.setRequestInterceptionEnabled(true);
|
await page.setRequestInterceptionEnabled(true);
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
const headers = new Map(request.headers);
|
const headers = Object.assign({}, request.headers);
|
||||||
headers.set('foo', 'bar');
|
headers['FOO'] = 'bar';
|
||||||
request.continue({ headers });
|
request.continue({ headers });
|
||||||
});
|
});
|
||||||
await page.goto(EMPTY_PAGE);
|
await page.goto(EMPTY_PAGE);
|
||||||
@ -1475,9 +1475,9 @@ describe('Page', function() {
|
|||||||
});
|
});
|
||||||
describe('Page.setExtraHTTPHeaders', function() {
|
describe('Page.setExtraHTTPHeaders', function() {
|
||||||
it('should work', SX(async function() {
|
it('should work', SX(async function() {
|
||||||
await page.setExtraHTTPHeaders(new Map(Object.entries({
|
await page.setExtraHTTPHeaders({
|
||||||
foo: 'bar'
|
foo: 'bar'
|
||||||
})));
|
});
|
||||||
page.goto(EMPTY_PAGE);
|
page.goto(EMPTY_PAGE);
|
||||||
const request = await server.waitForRequest('/empty.html');
|
const request = await server.waitForRequest('/empty.html');
|
||||||
expect(request.headers['foo']).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
|
Loading…
Reference in New Issue
Block a user