Skip to content

Commit

Permalink
Add cookie URL defaulting value capability
Browse files Browse the repository at this point in the history
  • Loading branch information
RynatSibahatau committed Jun 15, 2019
1 parent 1677a37 commit 074eee1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class GatherRunner {

/**
* Initialize network settings for the pass, e.g. throttling, blocked URLs,
* and manual request headers.
* manual request headers and cookies.
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<void>}
*/
Expand All @@ -213,11 +213,32 @@ class GatherRunner {
// neccessary at the beginning of the next pass.
await passContext.driver.blockUrlPatterns(blockedUrls);
await passContext.driver.setExtraHTTPHeaders(passContext.settings.extraHeaders);
await passContext.driver.setCookies(passContext.settings.extraCookies);
await GatherRunner.setupCookies(passContext)

log.timeEnd(status);
}

/**
* Initialize cookies settings for pass
* and manual request headers.
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<void>}
*/
static async setupCookies(passContext) {
const extraCookies = passContext.settings.extraCookies;
if (!extraCookies) {
return;
}
extraCookies.forEach(cookie => {
if (!cookie.url || !cookie.domain) {
// Default cookie URL to to current URL, if neither domain nor url is specified
cookie.url = passContext.url;
}
});
await passContext.driver.setCookies(extraCookies);
}


/**
* Beging recording devtoolsLog and trace (if requested).
* @param {LH.Gatherer.PassContext} passContext
Expand Down
30 changes: 30 additions & 0 deletions lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ describe('GatherRunner', function() {
const cookies = [{
'name': 'cookie1',
'value': 'monster',
'domain': 'test.com',
}];

return GatherRunner.setupPassNetwork({
Expand All @@ -604,6 +605,35 @@ describe('GatherRunner', function() {
));
});

it('uses current url as cookie\'s url if neither domain nor url is specified', () => {
let receivedCookies = null;
const driver = getMockedEmulationDriver(null, null, null, null, null, params => {
receivedCookies = params.cookies;
});
const inputCookies = [{
'name': 'cookie1',
'value': 'monster',
}];
const expectedCookies = [{
'name': 'cookie1',
'value': 'monster',
'url': 'http://test.com/some_path',
}];

return GatherRunner.setupPassNetwork({
url: 'http://test.com/some_path',
driver,
settings: {
extraCookies: inputCookies,
},
passConfig: {gatherers: []},
}).then(() => assert.deepStrictEqual(
receivedCookies,
expectedCookies
));
});


it('tells the driver to begin tracing', async () => {
let calledTrace = false;
const driver = {
Expand Down

0 comments on commit 074eee1

Please sign in to comment.