forked from tcort/markdown-link-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-link-check.test.js
More file actions
222 lines (177 loc) · 6.88 KB
/
markdown-link-check.test.js
File metadata and controls
222 lines (177 loc) · 6.88 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const expect = require('expect.js');
const http = require('http');
const express = require('express');
const markdownLinkCheck = require('../');
describe('markdown-link-check', function () {
// add a longer timeout on tests so we can really test real cases.
// Mocha default is 2s, make it 5s here.
this.timeout(5000);
let baseUrl;
before(function (done) {
const app = express();
var laterRetryCount = 0;
app.head('/nohead', function (req, res) {
res.sendStatus(405); // method not allowed
});
app.get('/nohead', function (req, res) {
res.sendStatus(200);
});
app.get('/partial', function (req, res) {
res.sendStatus(206);
});
app.get('/later', function (req, res) {
if(laterRetryCount<2){
laterRetryCount++;
res.append('retry-after', '2s');
res.sendStatus(429);
}else{
res.sendStatus(200);
}
});
app.get('/foo/redirect', function (req, res) {
res.redirect('/foo/bar');
});
app.get('/foo/bar', function (req, res) {
res.json({foo:'bar'});
});
app.get('/basic-auth', function (req, res) {
if (req.headers["authorization"] === "Basic Zm9vOmJhcg==") {
res.sendStatus(200);
}
else {
res.sendStatus(401);
}
});
app.get('/loop', function (req, res) {
res.redirect('/loop');
});
app.get('/hello.jpg', function (req, res) {
res.sendFile('hello.jpg', {
root: __dirname,
dotfiles: 'deny'
});
});
app.get('/foo\\(a=b.42\\).aspx', function (req, res) {
res.json({a:'b'});
});
const server = http.createServer(app);
server.listen(0 /* random open port */, 'localhost', function serverListen(err) {
if (err) {
done(err);
return;
}
baseUrl = 'http://' + server.address().address + ':' + server.address().port;
done();
});
});
it('should check the links in sample.md', function (done) {
markdownLinkCheck(
fs.readFileSync(path.join(__dirname, 'sample.md')).toString().replace(/%%BASE_URL%%/g, baseUrl),
{
baseUrl: baseUrl,
ignorePatterns: [{ pattern: /not-working-and-ignored/ }],
replacementPatterns: [{ pattern: /boo/, replacement: "foo" }],
httpHeaders: [
{
urls: [baseUrl + '/basic-auth'],
headers: { 'Authorization': 'Basic Zm9vOmJhcg==', 'Foo': 'Bar' }
}
],
"aliveStatusCodes":[200, 206],
"retryOn429":true
}, function (err, results) {
expect(err).to.be(null);
expect(results).to.be.an('array');
const expected = [
// redirect-loop
{ statusCode: 0, status: 'dead' },
// valid
{ statusCode: 200, status: 'alive' },
// invalid
{ statusCode: 404, status: 'dead' },
// dns-resolution-fail
{ statusCode: 0, status: 'dead' },
// nohead-get-ok
{ statusCode: 200, status: 'alive' },
// redirect
{ statusCode: 200, status: 'alive' },
// basic-auth
{ statusCode: 200, status: 'alive' },
// ignored
{ statusCode: 0, status: 'ignored' },
// replaced
{ statusCode: 200, status: 'alive' },
// request rate limit return 429, retry later and get 200
{ statusCode: 200, status: 'alive' },
// partial
{ statusCode: 206, status: 'alive' },
// hello image
{ statusCode: 200, status: 'alive' },
// hello image
{ statusCode: 200, status: 'alive' },
// valid e-mail
{ statusCode: 200, status: 'alive' },
// invalid e-mail
{ statusCode: 400, status: 'dead' },
// invalid protocol
{ statusCode: 500, status: 'error' },
// invalid protocol
{ statusCode: 500, status: 'error' },
];
expect(results.length).to.be(expected.length);
for (let i = 0; i < results.length; i++) {
expect(results[i].statusCode).to.be(expected[i].statusCode);
expect(results[i].status).to.be(expected[i].status);
}
done();
});
});
it('should check the links in file.md', function (done) {
markdownLinkCheck(fs.readFileSync(path.join(__dirname, 'file.md')).toString().replace(/%%BASE_URL%%/g, 'file://' + __dirname), { baseUrl: baseUrl }, function (err, results) {
expect(err).to.be(null);
expect(results).to.be.an('array');
const expected = [
{ statusCode: 200, status: 'alive' },
{ statusCode: 200, status: 'alive' },
{ statusCode: 404, status: 'dead' },
];
expect(results.length).to.be(expected.length);
for (let i = 0; i < results.length; i++) {
expect(results[i].statusCode).to.be(expected[i].statusCode);
expect(results[i].status).to.be(expected[i].status);
}
done();
});
});
it('should handle thousands of links (this test takes up to a minute)', function (done) {
this.timeout(60000);
let md = '';
const nlinks = 10000;
for (let i = 0; i < nlinks; i++) {
md += '[test](' + baseUrl + '/foo/bar?i=' + i + ')\n';
}
markdownLinkCheck(md, function (err, results) {
expect(err).to.be(null);
expect(results).to.be.an('array');
expect(results).to.have.length(nlinks);
for (let i = 0; i < results.length; i++) {
expect(results[i].statusCode).to.be(200);
expect(results[i].status).to.be('alive');
}
done();
});
});
it('should handle links with parens', function (done) {
markdownLinkCheck('[test](' + baseUrl + '/foo\(a=b.42\).aspx)', function (err, results) {
expect(err).to.be(null);
expect(results).to.be.an('array');
expect(results).to.have.length(1);
expect(results[0].statusCode).to.be(200);
expect(results[0].status).to.be('alive');
done();
});
});
});