Skip to content

Commit

Permalink
manually merge pr GoogleChrome#9170
Browse files Browse the repository at this point in the history
  • Loading branch information
朱坤坤 committed Jul 31, 2019
1 parent c45f3a2 commit 97ec8d6
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ async function begin() {
cliFlags.extraHeaders = JSON.parse(extraHeadersStr);
}

if (cliFlags.extraCookies) {
// TODO: LH.Flags.extraCookies is actually a string at this point, but needs to be
// copied over to LH.Settings.extraCookies, which is LH.Crdp.Network.Cookies. Force
// the conversion here, but long term either the CLI flag or the setting should have
// a different name.
// @ts-ignore
const extraCookiesStr = /** @type {string} */ (cliFlags.extraCookies);
cliFlags.extraCookies = JSON.parse(extraCookiesStr);
if (!Array.isArray(cliFlags.extraCookies)) {
throw new Error('extraCookies parameter must be a valid JSON array');
}
}

if (cliFlags.precomputedLanternDataPath) {
const lanternDataStr = fs.readFileSync(cliFlags.precomputedLanternDataPath, 'utf8');
/** @type {LH.PrecomputedLanternData} */
Expand Down
10 changes: 9 additions & 1 deletion lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ function getFlags(manualArgv) {
'lighthouse <url> --quiet --chrome-flags="--headless"',
'Launch Headless Chrome, turn off logging')
.example(
'lighthouse <url> --extra-headers "{\\"Cookie\\":\\"monster=blue\\", \\"x-men\\":\\"wolverine\\"}"',
'lighthouse <url> --extra-headers "{\\"x-men\\":\\"wolverine\\"}"',
'Stringify\'d JSON HTTP Header key/value pairs to send in requests')
.example(
'lighthouse <url> --extra-headers=./path/to/file.json',
'Path to JSON file of HTTP Header key/value pairs to send in requests')
.example(
'lighthouse <url> --extra-cookies "[{\\"name\\":\\"session_id\\",\\"value\\":\\"x-men\\" }]"',
'Stringify\'d JSON array of HTTP Cookies to send in requests')
.example(
'lighthouse <url> --extra-cookies=./path/to/file.json',
'Path to JSON file of HTTP Cookies to send in requests')
.example(
'lighthouse <url> --only-categories=performance,pwa',
'Only run specific categories.')
Expand Down Expand Up @@ -126,6 +132,7 @@ function getFlags(manualArgv) {
'max-wait-for-load':
'The timeout (in milliseconds) to wait before the page is considered done loading and the run should continue. WARNING: Very high values can lead to large traces and instability',
'extra-headers': 'Set extra HTTP Headers to pass with request',
'extra-cookies': 'Set extra HTTP Cookies to pass with request',
'precomputed-lantern-data-path': 'Path to the file where lantern simulation data should be read from, overwriting the lantern observed estimates for RTT and server latency.',
'lantern-data-output-path': 'Path to the file where lantern simulation data should be written to, can be used in a future run with the `precomputed-lantern-data-path` flag.',
'only-audits': 'Only run the specified audits',
Expand Down Expand Up @@ -166,6 +173,7 @@ function getFlags(manualArgv) {
.array('output')
.array('plugins')
.string('extraHeaders')
.string('extraCookies')
.string('channel')
.string('precomputedLanternDataPath')
.string('lanternDataOutputPath')
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ Object {
"channel": "cli",
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"extraCookies": null,
"extraHeaders": null,
"gatherMode": false,
"locale": "en-US",
Expand Down Expand Up @@ -1379,6 +1380,7 @@ Object {
"channel": "cli",
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"extraCookies": null,
"extraHeaders": null,
"gatherMode": false,
"locale": "en-US",
Expand Down
16 changes: 16 additions & 0 deletions lighthouse-cli/test/cli/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ describe('CLI bin', function() {
});
});

describe('extraCookies', () => {
it('should convert extra cookies to object', async () => {
// @ts-ignore - see TODO: in bin.js
cliFlags = { ...cliFlags, extraCookies: '[{"name":"foo", "value": "bar", "url": "http://localhost"}]' };
await bin.begin();

expect(getRunLighthouseArgs()[1]).toHaveProperty('extraCookies', [{ 'name': 'foo', 'value': 'bar', 'url': 'http://localhost' }]);
});

it('should throw when invalid array is used', async () => {
// @ts-ignore - see TODO: in bin.js
cliFlags = { ...cliFlags, extraCookies: 'INVALID_JSON_ARRAY' };
await expect(bin.begin()).rejects.toBeTruthy();
});
});

describe('precomputedLanternData', () => {
it('should read lantern data from file', async () => {
const lanternDataFile = require.resolve('../fixtures/lantern-data.json');
Expand Down
13 changes: 13 additions & 0 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ function requestHandler(request, response) {
}
}

if (params.has('extra_cookie')) {
const extraCookies = new URLSearchParams(params.get('extra_cookie'));
let cookeString = '';
for (const [cookieName, cookieValue] of extraCookies) {
cookeString += cookieName + '=' + cookieValue + ';';
}

// Extra cookie we allways override possible 'Set-Cookie' header
// which may be already present in request by extra_header
headers['Set-Cookie'] = [];
headers['Set-Cookie'].push(cookeString);
}

if (params.has('gzip')) {
useGzip = Boolean(params.get('gzip'));
}
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const defaultSettings = {
blockedUrlPatterns: null,
additionalTraceCategories: null,
extraHeaders: null,
extraCookies: null,
precomputedLanternData: null,
onlyAudits: null,
onlyCategories: null,
Expand Down
12 changes: 12 additions & 0 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,18 @@ class Driver {
return this.sendCommand('Network.setExtraHTTPHeaders', {headers});
}

/**
* @param {LH.Crdp.Network.CookieParam[]|null} cookies key/value pairs of HTTP Cookies.
* @return {Promise<void>}
*/
async setCookies(cookies) {
if (!cookies) {
return;
}

return this.sendCommand('Network.setCookies', {cookies});
}

/**
* @param {string} url
* @return {Promise<void>}
Expand Down
23 changes: 22 additions & 1 deletion lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,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 @@ -249,10 +249,31 @@ class GatherRunner {
// neccessary at the beginning of the next pass.
await passContext.driver.blockUrlPatterns(blockedUrls);
await passContext.driver.setExtraHTTPHeaders(passContext.settings.extraHeaders);
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
26 changes: 26 additions & 0 deletions lighthouse-core/test/gather/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,32 @@ describe('.setExtraHTTPHeaders', () => {
});
});

describe('.setCookies', () => {
it('should call Network.setCookies when there are extra-cookies', async () => {
connectionStub.sendCommand = createMockSendCommandFn().mockResponse(
'Network.setCookies',
{}
);

await driver.setCookies([{
'name': 'cookie1',
'value': 'monster',
}]);

expect(connectionStub.sendCommand).toHaveBeenCalledWith(
'Network.setCookies',
expect.anything()
);
});

it('should not call Network.setCookies when there aren\'t extra-cookies', async () => {
connectionStub.sendCommand = createMockSendCommandFn();
await driver.setCookies();

expect(connectionStub.sendCommand).not.toHaveBeenCalled();
});
});

describe('.getAppManifest', () => {
it('should return null when no manifest', async () => {
connectionStub.sendCommand = createMockSendCommandFn().mockResponse(
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/test/gather/fake-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function makeFakeDriver({protocolGetVersionResponse}) {
setExtraHTTPHeaders() {
return Promise.resolve();
},
setCookies() {
return Promise.resolve();
}
};
}

Expand Down
58 changes: 57 additions & 1 deletion lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const fakeDriver = require('./fake-driver.js');
const fakeDriverUsingRealMobileDevice = fakeDriver.fakeDriverUsingRealMobileDevice;

function getMockedEmulationDriver(emulationFn, netThrottleFn, cpuThrottleFn,
blockUrlFn, extraHeadersFn) {
blockUrlFn, extraHeadersFn, extraCookiesFn) {
const Driver = require('../../gather/driver.js');
const Connection = require('../../gather/connections/connection.js');
const EmulationDriver = class extends Driver {
Expand Down Expand Up @@ -81,6 +81,9 @@ function getMockedEmulationDriver(emulationFn, netThrottleFn, cpuThrottleFn,
case 'Network.setExtraHTTPHeaders':
fn = extraHeadersFn;
break;
case 'Network.setCookies':
fn = extraCookiesFn;
break;
default:
fn = null;
break;
Expand Down Expand Up @@ -397,6 +400,7 @@ describe('GatherRunner', function() {
setThrottling: asyncFunc,
blockUrlPatterns: asyncFunc,
setExtraHTTPHeaders: asyncFunc,
setCookies: asyncFunc,
endTrace: asyncFunc,
endDevtoolsLog: () => [],
getBrowserVersion: async () => ({userAgent: ''}),
Expand Down Expand Up @@ -577,6 +581,58 @@ describe('GatherRunner', function() {
));
});

it('tells the driver to set additional cookies when extraCookies flag is given', () => {
let receivedCookies = null;
const driver = getMockedEmulationDriver(null, null, null, null, null, params => {
receivedCookies = params.cookies;
});
const cookies = [{
'name': 'cookie1',
'value': 'monster',
'domain': 'test.com',
}];

return GatherRunner.setupPassNetwork({
driver,
settings: {
extraCookies: cookies,
},
passConfig: { gatherers: [] },
}).then(() => assert.deepStrictEqual(
receivedCookies,
cookies
));
});

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
1 change: 1 addition & 0 deletions lighthouse-core/test/results/artifacts/artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"extraCookies": null,
"precomputedLanternData": null,
"onlyAudits": null,
"onlyCategories": null,
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,7 @@
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"extraCookies": null,
"precomputedLanternData": null,
"onlyAudits": null,
"onlyCategories": null,
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Options:
--throttling.uploadThroughputKbps Controls emulated network upload throughput
--throttling.cpuSlowdownMultiplier Controls simulated + emulated CPU throttling
--extra-headers Set extra HTTP Headers to pass with request [string]
--extra-cookies Set extra HTTP Cookies to pass with request
Examples:
lighthouse <url> --view Opens the HTML report in a browser after the run completes
Expand All @@ -113,6 +114,8 @@ Examples:
lighthouse <url> --quiet --chrome-flags="--headless" Launch Headless Chrome, turn off logging
lighthouse <url> --extra-headers "{\"Cookie\":\"monster=blue\"}" Stringify\'d JSON HTTP Header key/value pairs to send in requests
lighthouse <url> --extra-headers=./path/to/file.json Path to JSON file of HTTP Header key/value pairs to send in requests
lighthouse <url> --extra-cookies "[{\"name\":\"session_id\",\"value\":\"x-men\" }]" Stringify'd JSON array of HTTP Cookies to send in requests
lighthouse <url> --extra-cookies=./path/to/file.json Path to JSON file of HTTP Cookies to send in requests
For more information on Lighthouse, see https://developers.google.com/web/tools/lighthouse/.
```
Expand Down
2 changes: 2 additions & 0 deletions types/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ declare global {
onlyCategories?: string[] | null;
/** If present, the run should skip this list of audits. */
skipAudits?: string[] | null;
/** List of extra HTTP Cookies to include. */
extraCookies?: Crdp.Network.CookieParam[] | null; // See extraCookies TODO in bin.js
/** List of extra HTTP Headers to include. */
extraHeaders?: Crdp.Network.Headers | null; // See extraHeaders TODO in bin.js
/** How Lighthouse was run, e.g. from the Chrome extension or from the npm module */
Expand Down

0 comments on commit 97ec8d6

Please sign in to comment.