Skip to content

Commit d1e4e4b

Browse files
committed
tests: mock external requests
1 parent 4e3449e commit d1e4e4b

1 file changed

Lines changed: 46 additions & 9 deletions

File tree

test/quicklink.spec.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const host = 'http://127.0.0.1:8080';
88
const server = `${host}/test/fixtures`;
99
const mainSuite = suite('quicklink tests');
1010

11+
const isExternalURL = url => {
12+
try {
13+
const parsed = new URL(url);
14+
15+
// "null" origins cover about:blank, data:, etc.
16+
if (parsed.origin === 'null') return false;
17+
18+
return parsed.origin !== host;
19+
} catch (_) {
20+
return false;
21+
}
22+
};
23+
1124
// Default 1000 ms
1225
const sleep = (ms = 1000) => new Promise(resolve => {
1326
setTimeout(resolve, ms);
@@ -22,6 +35,33 @@ const puppeteerOptions = {
2235
mainSuite.before(async context => {
2336
context.browser = await puppeteer.launch(puppeteerOptions);
2437
context.page = await context.browser.newPage();
38+
39+
// Mock external network calls so that tests do not reach the internet
40+
context.handleRequest = null;
41+
42+
await context.page.setRequestInterception(true);
43+
context.page.on('request', async req => {
44+
const url = req.url();
45+
46+
if (isExternalURL(url)) {
47+
return req.respond({
48+
status: 200,
49+
contentType: 'text/plain',
50+
body: 'mocked external response',
51+
});
52+
}
53+
54+
if (typeof context.handleRequest === 'function') {
55+
return context.handleRequest(req);
56+
}
57+
58+
return req.continue();
59+
});
60+
});
61+
62+
mainSuite.before.each(async context => {
63+
context.handleRequest = null;
64+
context.page.removeAllListeners('response');
2565
});
2666

2767
mainSuite.after(async context => {
@@ -107,10 +147,10 @@ mainSuite('should only prefetch links if allowed in origins list', async context
107147
responseURLs.push(resp.url());
108148
});
109149
await context.page.goto(`${server}/test-allow-origin.html`);
110-
await sleep(1000);
150+
await sleep();
111151
assert.instance(responseURLs, Array);
112152

113-
// => origins: ['github.githubassets.com']
153+
// => origins: ['github.githubassets.com', 'example.com']
114154
assert.not.ok(responseURLs.includes(`${server}/2.html`));
115155
assert.ok(responseURLs.includes('https://example.com/1.html'));
116156
assert.ok(responseURLs.includes('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'));
@@ -262,20 +302,17 @@ mainSuite('should not exceed the `limit` total', async context => {
262302
mainSuite('should respect the `throttle` concurrency', async context => {
263303
const URLs = []; // Note: Page makes 4 requests
264304

265-
// Make HTML requests take a long time
266-
// ~> so that we can ensure throttling occurs
267-
await context.page.setRequestInterception(true);
268-
269-
context.page.on('request', async req => {
305+
// Make HTML requests take a long time so that we can ensure throttling occurs
306+
context.handleRequest = async req => {
270307
const url = req.url();
271308
if (/test\/fixtures\/\d+\.html$/i.test(url)) {
272309
await sleep(100);
273310
URLs.push(url);
274311
return req.respond({status: 200});
275312
}
276313

277-
req.continue();
278-
});
314+
return req.continue();
315+
};
279316

280317
await context.page.goto(`${server}/test-throttle.html`);
281318

0 commit comments

Comments
 (0)