Skip to content

Commit

Permalink
test: add origins & sameOrigin suites
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Dec 14, 2018
1 parent 2a31aee commit 21f601b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/quicklink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,35 @@ describe('quicklink tests', function () {
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include(`${server}/main.css`);
});

it('should only prefetch links if allowed in origins list', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto(`${server}/test-allow-origin.html`);

await page.waitFor(1000);

expect(responseURLs).to.be.an('array');
//=> origins: ['github.githubassets.com']
expect(responseURLs).to.not.include(`${server}/2.html`);
expect(responseURLs).to.include('https://example.com/1.html');
expect(responseURLs).to.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
});

it('should only prefetch links of same origin', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto(`${server}/test-same-origin.html`);

await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
//=> sameOrigin: true
expect(responseURLs).to.include(`${server}/2.html`);
expect(responseURLs).to.not.include('https://example.com/1.html');
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
});
});
26 changes: 26 additions & 0 deletions test/test-allow-origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: Allowed Origins</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="https://example.com/1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script src="../dist/quicklink.umd.js"></script>
<script>
quicklink({
origins: ['github.githubassets.com', 'example.com']
});
</script>
</body>
</html>
26 changes: 26 additions & 0 deletions test/test-same-origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: Same Origin</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="https://example.com/1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script src="../dist/quicklink.umd.js"></script>
<script>
quicklink({
sameOrigin: true
});
</script>
</body>
</html>

0 comments on commit 21f601b

Please sign in to comment.