[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:
Andrey Lushnikov
2017-08-28 12:09:24 -07:00
committed by GitHub
parent 39b9081747
commit 66912a7277
5 changed files with 30 additions and 38 deletions

View File

@@ -29,8 +29,8 @@ class NetworkManager extends EventEmitter {
this._requestIdToRequest = new Map();
/** @type {!Map<string, !Request>} */
this._interceptionIdToRequest = new Map();
/** @type {!Map<string, string>} */
this._extraHTTPHeaders = new Map();
/** @type {!Object<string, string>} */
this._extraHTTPHeaders = {};
this._requestInterceptionEnabled = false;
/** @type {!Multimap<string, string>} */
@@ -46,22 +46,21 @@ class NetworkManager extends EventEmitter {
}
/**
* @param {!Map<string, string>} extraHTTPHeaders
* @param {!Object<string, string>} extraHTTPHeaders
* @return {!Promise}
*/
async setExtraHTTPHeaders(extraHTTPHeaders) {
this._extraHTTPHeaders = new Map(extraHTTPHeaders);
const headers = {};
for (const entry of extraHTTPHeaders.entries())
headers[entry[0]] = entry[1];
await this._client.send('Network.setExtraHTTPHeaders', { headers });
this._extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders))
this._extraHTTPHeaders[key.toLowerCase()] = extraHTTPHeaders[key];
await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
}
/**
* @return {!Map<string, string>}
* @return {!Object<string, string>}
*/
extraHTTPHeaders() {
return new Map(this._extraHTTPHeaders);
return Object.assign({}, this._extraHTTPHeaders);
}
/**
@@ -226,7 +225,9 @@ class Request {
this.url = url;
this.method = payload.method;
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._interceptionHandled, 'Request is already handled!');
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', {
interceptionId: this._interceptionId,
url: overrides.url,
method: overrides.method,
postData: overrides.postData,
headers: headers
headers: overrides.headers,
});
}
@@ -288,10 +283,12 @@ class Response {
this._request = request;
this._contentPromise = null;
this.headers = new Map(Object.entries(headers));
this.status = status;
this.ok = status >= 200 && status <= 299;
this.url = request.url;
this.headers = {};
for (const key of Object.keys(headers))
this.headers[key.toLowerCase()] = headers[key];
}
/**