Skip to content

Commit

Permalink
Assert that all extra HTTP header values are strings (#781)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aslushnikov committed Sep 15, 2017
1 parent 86b05da commit d562db3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -742,7 +742,7 @@ Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#ke
- returns: <[Promise]>

#### 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]>

The extra HTTP headers will be sent with every request the page initiates.
Expand Down
7 changes: 5 additions & 2 deletions lib/NetworkManager.js
Expand Up @@ -63,8 +63,11 @@ class NetworkManager extends EventEmitter {
*/
async setExtraHTTPHeaders(extraHTTPHeaders) {
this._extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders))
this._extraHTTPHeaders[key.toLowerCase()] = extraHTTPHeaders[key];
for (const key of Object.keys(extraHTTPHeaders)) {
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 });
}

Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Expand Up @@ -1538,6 +1538,15 @@ describe('Page', function() {
]);
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() {
it('should work', SX(async function() {
Expand Down

0 comments on commit d562db3

Please sign in to comment.