Skip to content

Commit

Permalink
chore: simplify extra http headers handling across browsers (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Dec 17, 2019
1 parent 9161586 commit 24eb6d8
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/chromium/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ export class FrameManager implements PageDelegate {
this._page._onFileChooserOpened(handle);
}

setExtraHTTPHeaders(extraHTTPHeaders: network.Headers): Promise<void> {
return this._networkManager.setExtraHTTPHeaders(extraHTTPHeaders);
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
await this._client.send('Network.setExtraHTTPHeaders', { headers });
}

setUserAgent(userAgent: string): Promise<void> {
Expand Down
15 changes: 0 additions & 15 deletions src/chromium/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class NetworkManager {
private _page: Page;
private _requestIdToRequest = new Map<string, InterceptableRequest>();
private _requestIdToRequestWillBeSentEvent = new Map<string, Protocol.Network.requestWillBeSentPayload>();
private _extraHTTPHeaders: network.Headers = {};
private _offline = false;
private _credentials: {username: string, password: string} | null = null;
private _attemptedAuthentications = new Set<string>();
Expand Down Expand Up @@ -68,20 +67,6 @@ export class NetworkManager {
await this._updateProtocolRequestInterception();
}

async setExtraHTTPHeaders(extraHTTPHeaders: network.Headers) {
this._extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders)) {
const value = extraHTTPHeaders[key];
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 });
}

extraHTTPHeaders(): network.Headers {
return Object.assign({}, this._extraHTTPHeaders);
}

async setOfflineMode(value: boolean) {
if (this._offline === value)
return;
Expand Down
7 changes: 5 additions & 2 deletions src/firefox/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ export class FrameManager implements PageDelegate {
return true;
}

setExtraHTTPHeaders(extraHTTPHeaders: network.Headers): Promise<void> {
return this._networkManager.setExtraHTTPHeaders(extraHTTPHeaders);
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
const array = [];
for (const [name, value] of Object.entries(headers))
array.push({ name, value });
await this._session.send('Network.setExtraHTTPHeaders', { headers: array });
}

async setUserAgent(userAgent: string): Promise<void> {
Expand Down
9 changes: 0 additions & 9 deletions src/firefox/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ export class NetworkManager {
helper.removeEventListeners(this._eventListeners);
}

async setExtraHTTPHeaders(headers: network.Headers) {
const array = [];
for (const [name, value] of Object.entries(headers)) {
assert(helper.isString(value), `Expected value of header "${name}" to be String, but "${typeof value}" is found.`);
array.push({name, value});
}
await this._session.send('Network.setExtraHTTPHeaders', {headers: array});
}

async setRequestInterception(enabled) {
await this._session.send('Network.setRequestInterception', {enabled});
}
Expand Down
7 changes: 6 additions & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ export class Page extends EventEmitter {
}

setExtraHTTPHeaders(headers: network.Headers) {
this._state.extraHTTPHeaders = {...headers};
this._state.extraHTTPHeaders = {};
for (const key of Object.keys(headers)) {
const value = headers[key];
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
this._state.extraHTTPHeaders[key.toLowerCase()] = value;
}
return this._delegate.setExtraHTTPHeaders(headers);
}

Expand Down
14 changes: 10 additions & 4 deletions src/webkit/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export class FrameManager implements PageDelegate {
promises.push(this._setJavaScriptEnabled(session, this._page._state.javascriptEnabled));
if (this._page._state.bypassCSP !== null)
promises.push(this._setBypassCSP(session, this._page._state.bypassCSP));
if (this._page._state.extraHTTPHeaders !== null)
promises.push(this._setExtraHTTPHeaders(session, this._page._state.extraHTTPHeaders));
await Promise.all(promises);
}

Expand Down Expand Up @@ -240,10 +242,6 @@ export class FrameManager implements PageDelegate {
this._page._onFileChooserOpened(handle);
}

setExtraHTTPHeaders(extraHTTPHeaders: network.Headers): Promise<void> {
return this._networkManager.setExtraHTTPHeaders(extraHTTPHeaders);
}

async _ensureIsolatedWorld(name: string) {
if (this._isolatedWorlds.has(name))
return;
Expand All @@ -254,6 +252,10 @@ export class FrameManager implements PageDelegate {
});
}

private async _setExtraHTTPHeaders(session: TargetSession, headers: network.Headers): Promise<void> {
await session.send('Network.setExtraHTTPHeaders', { headers });
}

private async _setUserAgent(session: TargetSession, userAgent: string): Promise<void> {
await session.send('Page.overrideUserAgent', { value: userAgent });
}
Expand All @@ -280,6 +282,10 @@ export class FrameManager implements PageDelegate {
await Promise.all(promises);
}

async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
await this._setExtraHTTPHeaders(this._session, headers);
}

async setUserAgent(userAgent: string): Promise<void> {
await this._setUserAgent(this._session, userAgent);
await this._page.reload();
Expand Down
20 changes: 1 addition & 19 deletions src/webkit/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class NetworkManager {
private _session: TargetSession;
private _page: Page;
private _requestIdToRequest = new Map<string, InterceptableRequest>();
private _extraHTTPHeaders: network.Headers = {};
private _attemptedAuthentications = new Set<string>();
private _userCacheDisabled = false;
private _sessionListeners: RegisteredListener[] = [];
Expand All @@ -47,30 +46,13 @@ export class NetworkManager {
}

async initializeSession(session: TargetSession) {
await Promise.all([
session.send('Network.enable'),
session.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders }),
]);
await session.send('Network.enable');
}

dispose() {
helper.removeEventListeners(this._sessionListeners);
}

async setExtraHTTPHeaders(extraHTTPHeaders: { [s: string]: string; }) {
this._extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders)) {
const value = extraHTTPHeaders[key];
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
this._extraHTTPHeaders[key.toLowerCase()] = value;
}
await this._session.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
}

extraHTTPHeaders(): { [s: string]: string; } {
return Object.assign({}, this._extraHTTPHeaders);
}

async setCacheEnabled(enabled: boolean) {
this._userCacheDisabled = !enabled;
await this._updateProtocolCacheDisabled();
Expand Down
6 changes: 3 additions & 3 deletions test/network.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
});
it('should fire events in proper order', async({page, server}) => {
const events = [];
page.on('request', request => events.push('request'));
page.on('response', response => events.push('response'));
page.on('requestfinished', request => events.push('requestfinished'));
page.on('request', request => !utils.isFavicon(request) && events.push('request'));
page.on('response', response => !utils.isFavicon(response.request()) && events.push('response'));
page.on('requestfinished', request => !utils.isFavicon(request) && events.push('requestfinished'));
await page.goto(server.EMPTY_PAGE);
expect(events).toEqual(['request', 'response', 'requestfinished']);
});
Expand Down

0 comments on commit 24eb6d8

Please sign in to comment.