/
test.ts
312 lines (279 loc) 路 9.27 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import * as assert from 'assert';
import * as gaxios from 'gaxios';
import * as nock from 'nock';
import * as sinon from 'sinon';
import * as path from 'path';
import {describe, it, afterEach} from 'mocha';
import {check, LinkState, LinkChecker} from '../src';
nock.disableNetConnect();
nock.enableNetConnect('localhost');
describe('linkinator', () => {
afterEach(() => {
nock.cleanAll();
});
it('should perform a basic shallow scan', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({path: 'test/fixtures/basic'});
assert.ok(results.passed);
scope.done();
});
it('should only try a link once', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({path: 'test/fixtures/twice'});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 2);
scope.done();
});
it('should only queue a link once', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const checker = new LinkChecker();
const checkerSpy = sinon.spy(checker, 'crawl');
const results = await checker.check({path: 'test/fixtures/twice'});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 2);
assert.strictEqual(checkerSpy.callCount, 2);
scope.done();
});
it('should skip links if asked nicely', async () => {
const results = await check({
path: 'test/fixtures/skip',
linksToSkip: ['http://very.bad'],
});
assert.ok(results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.SKIPPED).length,
1
);
});
it('should skip links if passed a linksToSkip function', async () => {
const scope = nock('https://good.com').head('/').reply(200);
const results = await check({
path: 'test/fixtures/filter',
linksToSkip: link => Promise.resolve(link.includes('filterme')),
});
assert.ok(results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.SKIPPED).length,
2
);
scope.done();
});
it('should report broken links', async () => {
const scope = nock('http://fake.local').head('/').reply(404);
const results = await check({path: 'test/fixtures/broke'});
assert.ok(!results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
1
);
scope.done();
});
it('should handle relative links', async () => {
const results = await check({
path: 'test/fixtures/relative',
recurse: true,
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 4);
});
it('should handle fetch exceptions', async () => {
const requestStub = sinon.stub(gaxios, 'request');
requestStub.throws('Fetch error');
const results = await check({path: 'test/fixtures/basic'});
assert.ok(!results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
1
);
requestStub.restore();
});
it('should report malformed links as broken', async () => {
const results = await check({path: 'test/fixtures/malformed'});
assert.ok(!results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
1
);
});
it('should detect relative urls with relative base', async () => {
const cases = [
{
fixture: 'test/fixtures/basetag/relative-to-root.html',
nonBrokenUrl: '/anotherBase/ok',
},
{
fixture: 'test/fixtures/basetag/relative-folder.html',
nonBrokenUrl: '/pageBase/anotherBase/ok',
},
{
fixture: 'test/fixtures/basetag/relative-dot-folder.html',
nonBrokenUrl: '/pageBase/anotherBase/ok',
},
{
fixture: 'test/fixtures/basetag/relative-page.html',
nonBrokenUrl: '/pageBase/ok',
},
{
fixture: 'test/fixtures/basetag/empty-base.html',
nonBrokenUrl: '/pageBase/ok',
},
];
for (let i = 0; i < cases.length; i++) {
const {fixture, nonBrokenUrl} = cases[i];
const scope = nock('http://fake.local')
.get('/pageBase/index')
.replyWithFile(200, fixture, {
'Content-Type': 'text/html; charset=UTF-8',
})
.head(nonBrokenUrl)
.reply(200);
const results = await check({
path: 'http://fake.local/pageBase/index',
});
assert.strictEqual(results.links.length, 3);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
1
);
scope.done();
}
});
it('should detect relative urls with absolute base', async () => {
const scope = nock('http://fake.local')
.get('/pageBase/index')
.replyWithFile(200, 'test/fixtures/basetag/absolute.html', {
'Content-Type': 'text/html; charset=UTF-8',
});
const anotherScope = nock('http://another.fake.local')
.head('/ok')
.reply(200);
const results = await check({
path: 'http://fake.local/pageBase/index',
});
assert.strictEqual(results.links.length, 3);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
1
);
scope.done();
anotherScope.done();
});
it('should detect broken image links', async () => {
const results = await check({path: 'test/fixtures/image'});
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length,
2
);
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').head('/').reply(200);
const results = await check({
path: 'test/fixtures/recurse',
recurse: true,
});
assert.strictEqual(results.links.length, 4);
scope.done();
});
it('should not recurse non-html files', async () => {
const results = await check({
path: 'test/fixtures/scripts',
recurse: true,
});
assert.strictEqual(results.links.length, 2);
});
it('should not follow non-http[s] links', async () => {
// includes mailto, data urls, and irc
const results = await check({path: 'test/fixtures/protocols'});
assert.ok(results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.SKIPPED).length,
3
);
});
it('should not recurse by default', async () => {
const results = await check({path: 'test/fixtures/recurse'});
assert.strictEqual(results.links.length, 2);
});
it('should retry with a GET after a HEAD', async () => {
const scopes = [
nock('http://fake.local').head('/').reply(405),
nock('http://fake.local').get('/').reply(200),
];
const results = await check({path: 'test/fixtures/basic'});
assert.ok(results.passed);
scopes.forEach(x => x.done());
});
it('should only follow links on the same origin domain', async () => {
const scopes = [
nock('http://fake.local')
.get('/')
.replyWithFile(200, path.resolve('test/fixtures/baseurl/index.html'), {
'content-type': 'text/html',
}),
nock('http://fake.local.br').head('/deep.html').reply(200),
];
const results = await check({
path: 'http://fake.local',
recurse: true,
});
assert.strictEqual(results.links.length, 2);
assert.ok(results.passed);
scopes.forEach(x => x.done());
});
it('should not attempt to validate preconnect or prefetch urls', async () => {
const scope = nock('http://fake.local').head('/site.css').reply(200, '');
const results = await check({path: 'test/fixtures/prefetch'});
scope.done();
assert.ok(results.passed);
assert.strictEqual(results.links.length, 2);
});
it('should attempt a GET request if a HEAD request fails on external links', async () => {
const scopes = [
nock('http://fake.local').head('/').reply(403),
nock('http://fake.local').get('/').reply(200),
];
const results = await check({path: 'test/fixtures/basic'});
assert.ok(results.passed);
scopes.forEach(x => x.done());
});
it('should support a configurable timeout', async () => {
nock('http://fake.local').head('/').delay(200).reply(200);
const results = await check({
path: 'test/fixtures/basic',
timeout: 1,
});
assert.ok(!results.passed);
});
it('should handle markdown', async () => {
const results = await check({
path: 'test/fixtures/markdown/README.md',
markdown: true,
});
assert.strictEqual(results.links.length, 3);
assert.ok(results.passed);
});
it('should throw an error if you pass server-root and an http based path', async () => {
await assert.rejects(
check({
path: 'https://jbeckwith.com',
serverRoot: process.cwd(),
}),
/cannot be defined/
);
});
it('should allow overriding the server root', async () => {
const results = await check({
serverRoot: 'test/fixtures/markdown',
markdown: true,
path: 'README.md',
});
assert.strictEqual(results.links.length, 3);
assert.ok(results.passed);
});
});