Skip to content

Commit 21f601b

Browse files
committed
test: add origins & sameOrigin suites
1 parent 2a31aee commit 21f601b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

test/quicklink.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,35 @@ describe('quicklink tests', function () {
7979
expect(responseURLs).to.be.an('array');
8080
expect(responseURLs).to.include(`${server}/main.css`);
8181
});
82+
83+
it('should only prefetch links if allowed in origins list', async function () {
84+
const responseURLs = [];
85+
page.on('response', resp => {
86+
responseURLs.push(resp.url());
87+
});
88+
await page.goto(`${server}/test-allow-origin.html`);
89+
90+
await page.waitFor(1000);
91+
92+
expect(responseURLs).to.be.an('array');
93+
//=> origins: ['github.githubassets.com']
94+
expect(responseURLs).to.not.include(`${server}/2.html`);
95+
expect(responseURLs).to.include('https://example.com/1.html');
96+
expect(responseURLs).to.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
97+
});
98+
99+
it('should only prefetch links of same origin', async function () {
100+
const responseURLs = [];
101+
page.on('response', resp => {
102+
responseURLs.push(resp.url());
103+
});
104+
await page.goto(`${server}/test-same-origin.html`);
105+
106+
await page.waitFor(1000);
107+
expect(responseURLs).to.be.an('array');
108+
//=> sameOrigin: true
109+
expect(responseURLs).to.include(`${server}/2.html`);
110+
expect(responseURLs).to.not.include('https://example.com/1.html');
111+
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
112+
});
82113
});

test/test-allow-origin.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Prefetch: Allowed Origins</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
9+
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
10+
</head>
11+
<body>
12+
<a href="https://example.com/1.html">Link 1</a>
13+
<a href="2.html">Link 2</a>
14+
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
15+
<section id="stuff">
16+
<a href="main.css">CSS</a>
17+
</section>
18+
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
19+
<script src="../dist/quicklink.umd.js"></script>
20+
<script>
21+
quicklink({
22+
origins: ['github.githubassets.com', 'example.com']
23+
});
24+
</script>
25+
</body>
26+
</html>

test/test-same-origin.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Prefetch: Same Origin</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
9+
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
10+
</head>
11+
<body>
12+
<a href="https://example.com/1.html">Link 1</a>
13+
<a href="2.html">Link 2</a>
14+
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
15+
<section id="stuff">
16+
<a href="main.css">CSS</a>
17+
</section>
18+
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
19+
<script src="../dist/quicklink.umd.js"></script>
20+
<script>
21+
quicklink({
22+
sameOrigin: true
23+
});
24+
</script>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)