-
Notifications
You must be signed in to change notification settings - Fork 79
/
test.ts
500 lines (451 loc) 路 14.9 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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, CheckOptions, headers} from '../src';
nock.disableNetConnect();
nock.enableNetConnect('localhost');
describe('linkinator', () => {
afterEach(() => {
sinon.restore();
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',
path: 'README.md',
});
assert.strictEqual(results.links.length, 3);
assert.ok(results.passed);
});
it('should accept multiple filesystem paths', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({
path: ['test/fixtures/basic', 'test/fixtures/image'],
});
assert.strictEqual(results.passed, false);
assert.strictEqual(results.links.length, 6);
scope.done();
});
it('should not allow mixed local and remote paths', async () => {
await assert.rejects(
check({
path: ['https://jbeckwith.com', 'test/fixtures/basic'],
}),
/cannot be mixed/
);
});
it('should require at least one path', async () => {
await assert.rejects(
check({
path: [],
}),
/At least one/
);
});
it('should not pollute the original options after merge', async () => {
const options: CheckOptions = Object.freeze({path: 'test/fixtures/basic'});
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check(options);
assert.ok(results.passed);
scope.done();
assert.strictEqual(options.serverRoot, undefined);
});
it('should accept multiple http paths', async () => {
const scopes = [
nock('http://fake.local')
.get('/')
.replyWithFile(200, 'test/fixtures/local/index.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
nock('http://fake.local')
.get('/page2.html')
.replyWithFile(200, 'test/fixtures/local/page2.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
nock('http://fake2.local')
.get('/')
.replyWithFile(200, 'test/fixtures/local/index.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
nock('http://fake2.local')
.get('/page2.html')
.replyWithFile(200, 'test/fixtures/local/page2.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
];
const results = await check({
path: ['http://fake.local', 'http://fake2.local'],
});
assert.ok(results.passed);
scopes.forEach(x => x.done());
});
it('should print debug information when the env var is set', async () => {
sinon.stub(process, 'env').value({
LINKINATOR_DEBUG: true,
});
const consoleSpy = sinon.stub(console, 'log');
const results = await check({
path: 'test/fixtures/markdown/README.md',
});
assert.ok(results.passed);
assert.ok(consoleSpy.calledOnce);
});
it('should respect globs', async () => {
const results = await check({
path: 'test/fixtures/markdown/**/*.md',
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 6);
});
it('should autoscan markdown if specifically in path', async () => {
const results = await check({
path: 'test/fixtures/markdown/README.md',
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 3);
});
it('should throw if a glob provides no paths to scan', async () => {
await assert.rejects(
check({
path: 'test/fixtures/basic/*.md',
}),
/returned 0 results/
);
});
it('should always send a human looking User-Agent', async () => {
const scopes = [
nock('http://fake.local')
.get('/', undefined, {reqheaders: headers})
.replyWithFile(200, 'test/fixtures/local/index.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
nock('http://fake.local')
.get('/page2.html', undefined, {reqheaders: headers})
.replyWithFile(200, 'test/fixtures/local/page2.html', {
'Content-Type': 'text/html; charset=UTF-8',
}),
];
const results = await check({
path: 'http://fake.local',
});
assert.ok(results.passed);
scopes.forEach(x => x.done());
});
it('should surface call stacks on failures in the API', async () => {
const results = await check({
path: 'http://fake.local',
});
assert.ok(!results.passed);
const err = results.links[0].failureDetails![0] as Error;
assert.ok(/Nock: Disallowed net connect for/.test(err.message));
});
it('should respect server root with globs', async () => {
const scope = nock('http://fake.local')
.get('/doll1')
.reply(200)
.get('/doll2')
.reply(200);
const results = await check({
serverRoot: 'test/fixtures/nested',
path: '*/*.html',
});
assert.strictEqual(results.links.length, 4);
assert.ok(results.passed);
scope.done();
});
it('should respect absolute server root', async () => {
const scope = nock('http://fake.local')
.get('/doll1')
.reply(200)
.get('/doll2')
.reply(200);
const results = await check({
serverRoot: path.resolve('test/fixtures/nested'),
path: '*/*.html',
});
assert.strictEqual(results.links.length, 4);
assert.ok(results.passed);
scope.done();
});
it('should scan links in <meta content="URL"> tags', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({path: 'test/fixtures/twittercard'});
assert.ok(results.passed);
scope.done();
assert.strictEqual(results.links.length, 2);
});
it('should support directory index', async () => {
const results = await check({
path: 'test/fixtures/directoryIndex/README.md',
directoryListing: true,
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 3);
});
it('should disabling directory index by default', async () => {
const results = await check({
path: 'test/fixtures/directoryIndex/README.md',
});
assert.ok(!results.passed);
assert.strictEqual(results.links.length, 3);
});
});