Skip to content

Commit

Permalink
Format & lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benschwarz committed Jul 4, 2024
1 parent cb0d94b commit 5bc02b3
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const PERFLOGSPATH = path.resolve(__dirname, 'perflogs');
*/
function validateConnectionOverlap(t, entries) {
const entriesByConnection = entries
.filter(entry => !['h3', 'h2', 'spdy/3.1'].includes(entry.response.httpVersion))
.filter(
entry => !['h3', 'h2', 'spdy/3.1'].includes(entry.response.httpVersion)
)
.filter(entry => !(entry.cache || {}).beforeRequest)
.reduce((entries, entry) => {
const e = entries.get(entry.connection) || [];
Expand All @@ -36,50 +38,51 @@ function validateConnectionOverlap(t, entries) {

function perflog(filename) {
return path.resolve(PERFLOGSPATH, filename);
};
}
function perflogs() {
return fs.readdir(PERFLOGSPATH)
.then(dirListing => dirListing.filter(filename => path.extname(filename) === '.json'));
return fs
.readdir(PERFLOGSPATH)
.then(dirListing =>
dirListing.filter(filename => path.extname(filename) === '.json')
);
}

function parsePerflog(perflogPath, options) {
return fs.readFile(perflogPath, { encoding: 'utf8' })
.then(data => {
const log = JSON.parse(data);
const har = parser.harFromMessages(log, options);
validator.har(har);
return har;
});
return fs.readFile(perflogPath, { encoding: 'utf8' }).then(data => {
const log = JSON.parse(data);
const har = parser.harFromMessages(log, options);
validator.har(har);
return har;
});
}

function sortedByRequestTime(entries) {
return entries.sort((e1, e2) => e1._requestTime - e2._requestTime);
}

function testAllHARs(t, options) {
return perflogs()
.then(filenames => {
const promises = filenames.map(filename => {
return parsePerflog(perflog(filename), options)
.then(har => {
t.deepEqual(sortedByRequestTime(har.log.entries), har.log.entries);
validateConnectionOverlap(t, har.log.entries);
})
.catch(e => {
t.log(`Failed to generate valid HAR from ${filename}`);
throw e;
});
});
return Promise.all(promises);
return perflogs().then(filenames => {
const promises = filenames.map(filename => {
return parsePerflog(perflog(filename), options)
.then(har => {
t.deepEqual(sortedByRequestTime(har.log.entries), har.log.entries);
validateConnectionOverlap(t, har.log.entries);
})
.catch(e => {
t.log(`Failed to generate valid HAR from ${filename}`);
throw e;
});
});
return Promise.all(promises);
});
}

test('Generates valid HARs', async t => {
return await testAllHARs(t);
test('Generates valid HARs', t => {
return testAllHARs(t);
});

test('Generates valid HARs including cached entries', async t => {
return await testAllHARs(t, { includeResourcesFromDiskCache: true });
test('Generates valid HARs including cached entries', t => {
return testAllHARs(t, { includeResourcesFromDiskCache: true });
});

test('zdnet', t => {
Expand Down Expand Up @@ -112,7 +115,7 @@ test('chrome66', t => {
.then(har => har.log)
.then(log => {
t.is(log.entries.length, 9);
return log
return log;
});
});

Expand Down Expand Up @@ -147,7 +150,7 @@ test('navigatedWithinDocument', t => {
return parsePerflog(perflogPath)
.then(har => har.log)
.then(log => {
t.is(log.entries.length, 1)
t.is(log.entries.length, 1);
return log;
});
});
Expand All @@ -156,15 +159,15 @@ test('Generates multiple pages', t => {
const perflogPath = perflog('www.wikipedia.org.json');
return parsePerflog(perflogPath).then(har => {
t.is(har.log.pages.length, 2);
return har
return har;
});
});

test('Skips empty pages', t => {
const perflogPath = perflog('www.wikipedia.org-empty.json');
return parsePerflog(perflogPath).then(har => {
t.is(har.log.pages.length, 1);
return har
return har;
});
});

Expand Down Expand Up @@ -198,14 +201,13 @@ test('Includes pushed assets', t => {

test('Includes early hints requests', t => {
const perflogPath = perflog('early-hints.json');
return parsePerflog(perflogPath)
.then(har => {
const earlyHints = har.log.entries.filter(e => e.response.fromEarlyHints);
t.is(earlyHints.length, 11);
return parsePerflog(perflogPath).then(har => {
const earlyHints = har.log.entries.filter(e => e.response.fromEarlyHints);
t.is(earlyHints.length, 11);

return har;
});
})
return har;
});
});

test('Includes response bodies', t => {
const perflogPath = perflog('www.sitepeed.io.chrome66.json');
Expand Down

0 comments on commit 5bc02b3

Please sign in to comment.