Assert that all extra HTTP header values are strings (#781)

Since protocol ignores all HTTP headers that don't have string
value, this patch starts validating header key-values before
sending them over the protocol.

Fixes #713.
This commit is contained in:
Andrey Lushnikov 2017-09-14 19:08:48 -07:00 committed by GitHub
parent 86b05dadd5
commit d562db3140
3 changed files with 15 additions and 3 deletions

View File

@ -742,7 +742,7 @@ Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#ke
- returns: <[Promise]> - returns: <[Promise]>
#### page.setExtraHTTPHeaders(headers) #### page.setExtraHTTPHeaders(headers)
- `headers` <[Object]> An object containing additional http headers to be sent with every request. - `headers` <[Object]> An object containing additional http headers to be sent with every request. All header values must be strings.
- 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.

View File

@ -63,8 +63,11 @@ class NetworkManager extends EventEmitter {
*/ */
async setExtraHTTPHeaders(extraHTTPHeaders) { async setExtraHTTPHeaders(extraHTTPHeaders) {
this._extraHTTPHeaders = {}; this._extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders)) for (const key of Object.keys(extraHTTPHeaders)) {
this._extraHTTPHeaders[key.toLowerCase()] = extraHTTPHeaders[key]; const value = extraHTTPHeaders[key];
console.assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
this._extraHTTPHeaders[key.toLowerCase()] = value;
}
await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders }); await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
} }

View File

@ -1538,6 +1538,15 @@ describe('Page', function() {
]); ]);
expect(request.headers['foo']).toBe('bar'); expect(request.headers['foo']).toBe('bar');
})); }));
it('should throw for non-string header values', SX(async function() {
let error = null;
try {
await page.setExtraHTTPHeaders({ 'foo': 1 });
} catch (e) {
error = e;
}
expect(error.message).toBe('Expected value of header "foo" to be String, but "number" is found.');
}));
}); });
describe('Page.authenticate', function() { describe('Page.authenticate', function() {
it('should work', SX(async function() { it('should work', SX(async function() {