Skip to content

Commit

Permalink
fix: respect recursive rules (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Feb 13, 2019
1 parent ddf1fa4 commit 877a32b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"gaxios": "^1.6.0",
"is-absolute-url": "^2.1.0",
"meow": "^5.0.0",
"server-destroy": "^1.0.1",
"update-notifier": "^2.5.0"
},
"devDependencies": {
Expand All @@ -37,6 +38,7 @@
"@types/mocha": "^5.2.5",
"@types/nock": "^9.3.1",
"@types/node": "^11.9.3",
"@types/server-destroy": "^1.0.0",
"@types/sinon": "^7.0.5",
"@types/update-notifier": "^2.5.0",
"codecov": "^3.1.0",
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {EventEmitter} from 'events';
import * as gaxios from 'gaxios';
import * as http from 'http';
import enableDestroy = require('server-destroy');

import {getLinks} from './links';

Expand Down Expand Up @@ -55,6 +56,7 @@ export class LinkChecker extends EventEmitter {
if (!options.path.startsWith('http')) {
const port = options.port || 5000 + Math.round(Math.random() * 1000);
server = await this.startWebServer(options.path, port);
enableDestroy(server);
options.path = `http://localhost:${port}`;
}
const results = await this.crawl(
Expand All @@ -64,7 +66,7 @@ export class LinkChecker extends EventEmitter {
passed: results.filter(x => x.state === LinkState.BROKEN).length === 0
};
if (server) {
server.close();
server.destroy();
}
return result;
}
Expand Down Expand Up @@ -141,7 +143,8 @@ export class LinkChecker extends EventEmitter {
const urls = getLinks(data, opts.checkOptions.path);
for (const url of urls) {
// only crawl links that start with the same host
const crawl = url.startsWith(opts.checkOptions.path);
const crawl = opts.checkOptions.recurse! &&
url.startsWith(opts.checkOptions.path);
await this.crawl({
url,
crawl,
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/recurse/fake.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<a href='/nothere.html'></a>
<a href='http://www.google.com'></a>
</body>
</html>
6 changes: 6 additions & 0 deletions test/fixtures/recurse/first.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<a href='/'></a>
<a href='/second.html'></a>
</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/recurse/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="first.html">go right over there</a>
</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/recurse/second.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href='http://fake.local'>Link to another castle</a>
</body>
</html>
16 changes: 16 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ describe('linkinator', () => {
assert.strictEqual(
results.links.filter(x => x.state === LinkState.OK).length, 2);
});

it('should perform a recursive scan', async () => {
// This test is making sure that we do a recursive scan of links,
// but also that we don't follow links to another site
const scope = nock('http://fake.local')
.get('/')
.replyWithFile(200, 'test/fixtures/recurse/fake.html');
const results = await check({path: 'test/fixtures/recurse', recurse: true});
assert.strictEqual(results.links.length, 5);
scope.done();
});

it('should not recurse by default', async () => {
const results = await check({path: 'test/fixtures/recurse'});
assert.strictEqual(results.links.length, 2);
});
});

0 comments on commit 877a32b

Please sign in to comment.