Skip to content

Commit

Permalink
Add check for network requests made
Browse files Browse the repository at this point in the history
 🐿 v2.6.0
  • Loading branch information
adgad committed Feb 15, 2018
1 parent 433ce77 commit 9f78bae
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 64 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = [
cacheHeaders: true, //verify Cache-Control and Surrogate headers are sensible
pageErrors: 0, // NOTE: should probably only use this with ads disabled
performance: true //checks firstPaint/firstContentfulPaint against baseline. default = 2000, or can specify.
networkRequests: {
'/some-third-party.js': 1,
'tracking.pixel': 4 //asserts 4 network requests were made to a URL containing 'tracking.pixel'
}
]
},
'/some/path': 200,
Expand Down
32 changes: 14 additions & 18 deletions lib/smoke/checks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const verifyCacheHeaders = require('./verify-cache-headers');

module.exports = {
status: (testPage) => {
if (testPage.check.status === 204) {
Expand Down Expand Up @@ -80,22 +78,6 @@ module.exports = {
return results;
},

cacheHeaders: (testPage) => {
let okay = true;
let problems;
try {
verifyCacheHeaders(testPage.headers, testPage.url);
} catch(errors) {
okay = false;
problems = errors;
}
return {
expected: 'Cache-Control headers should be sensible',
actual: okay || problems,
result: okay
};
},

cssCoverage: (testPage) => {
const results = [];
for(const url in testPage.check.cssCoverage) {
Expand Down Expand Up @@ -128,6 +110,20 @@ module.exports = {
{ expected: `First Paint to be less than ${threshold}ms`, actual: `${paints['first-paint']}ms`, result: paints['first-paint'] < threshold },
{ expected: `First Contentful Paint to be less than ${threshold}ms`, actual: `${paints['first-contentful-paint']}ms`, result: paints['first-contentful-paint'] < threshold }
];
},

networkRequests: (testPage) => {
const expected = testPage.check.networkRequests;
const results = [];
Object.entries(expected).forEach(([url, count]) => {
const matches = testPage.requests.filter(req => req.url.includes(url));
results.push({
expected: `${count} network requests to URL matching ${url}`,
actual: `${matches.length} requests`,
result: matches.length === count
});
});
return results;
}


Expand Down
10 changes: 8 additions & 2 deletions lib/smoke/test-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestPage {
this.consoleMessages = [];
this.coverageReports = [];
this.redirects = [];
this.requests = [];
this.response = null;
this.headers = null;

Expand Down Expand Up @@ -119,16 +120,21 @@ class TestPage {
this.page.on('response', response => {
const request = response.request();
const status = response.status();
const url = request.url();

if(url !== this.url) {
this.requests.push({ url, status });
}
// if this response is a redirect
if (REDIRECT_CODES.includes(status)) {
this.redirects[request.url()] = {
this.redirects[url] = {
code: status,
to: response.headers().location
};
}
});

const waitUntil = (this.check.cssCoverage || this.check.pageErrors) ? 'load' : 'domcontentloaded';
const waitUntil = 'load';
this.response = await this.page.goto(this.url, { waitUntil });
this.headers = this.response.headers();
if(this.check.cssCoverage) {
Expand Down
44 changes: 0 additions & 44 deletions lib/smoke/verify-cache-headers.js

This file was deleted.

7 changes: 7 additions & 0 deletions test/fixtures/smoke-status-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ module.exports = [{
},
'/json' : {
content: (content) => JSON.parse(content).key === 'value'
},
'/network-requests': {
networkRequests: {
'/status/200': 1,
'/status': 2,
'/json': 0
}
}

}
Expand Down
10 changes: 10 additions & 0 deletions test/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ app.get('/json', (req, res) => {
res.json({ key: 'value' });
});

app.get('/network-requests', (req, res) => {
res.send(`
<body>
<img src="/status/200" />
<img src="/status/302" />
</body>
`);
});


if (!module.parent) {
app.listen(process.env.PORT || 3004);
} else {
Expand Down

0 comments on commit 9f78bae

Please sign in to comment.