Skip to content

Preparing for setting headers when FF supports it in Bidi (Firefox 1XX) #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class FirefoxBidi {
}
});
}

async setBasicAuth(basicAuth) {
const parts = basicAuth.split('@');
const bidi = new Bidi(this.driver, this.options.browser);
Expand Down Expand Up @@ -146,4 +147,52 @@ export class FirefoxBidi {
}
}
}

async setRequestHeaders(requestHeaders) {
const headersArray = toArray(requestHeaders);
const headers = [];
for (let header of headersArray) {
if (header.indexOf && header.includes(':')) {
const parts = header.split(':');
headers.push({
name: parts[0],
value: {
type: 'string',
value: parts[1]
}
});
} else {
log.error(
'Request headers need to be of the format key:value not ' + header
);
}
}

const command = {
method: 'network.addIntercept',
params: {
phases: ['beforeRequestSent']
}
};
const bidi = new Bidi(this.driver, this.options.browser);

await bidi.send(command);
await bidi.subscribe('network.beforeRequestSent');
await bidi.onMessage(async function (event) {
const parsedEvent = JSON.parse(Buffer.from(event.toString()));
if (parsedEvent.method === 'network.beforeRequestSent') {
const continueRequest = {
method: 'network.continueRequest',
params: {
request: parsedEvent.params.request.request,
headers: [...parsedEvent.params.request.headers, ...headers]
}
};
const result = await bidi.send(continueRequest);
if (result.type != 'success') {
log.error(result);
}
}
});
}
}
33 changes: 18 additions & 15 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,25 @@ export class Firefox {
await this.browsertimeBidi.setBasicAuth(this.options.basicAuth);
}

if (this.options.block) {
await this.browsertimeBidi.blockUrls(this.options.block);
}
if (this.options.requestheader) {
await this.browsertimeBidi.setRequestHeaders(this.options.requestheader);
if (this.options.block) {
await this.browsertimeBidi.blockUrls(this.options.block);
}

if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
) {
const currentUserAgent = await runner.runScript(
'return navigator.userAgent;',
'GET_USER_AGENT'
);
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
}');`;
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
) {
const currentUserAgent = await runner.runScript(
'return navigator.userAgent;',
'GET_USER_AGENT'
);
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
}');`;
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
}
}
}

Expand Down
Loading