Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update tests and add options
  • Loading branch information
NicolasMassart committed Sep 14, 2020
commit 7a37366272efcf7ad22be9a5715c37a78d3f33aa
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
}
}

opts.retryOn429 = true;
opts.aliveStatusCodes = [200,206];

linkCheck(link, opts, function (err, result) {

if (opts.showProgressBar) {
bar.tick();
}
Expand Down
30 changes: 28 additions & 2 deletions test/markdown-link-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,38 @@ 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');
});
Expand Down Expand Up @@ -62,7 +82,7 @@ describe('markdown-link-check', function () {
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),
Expand All @@ -75,7 +95,7 @@ describe('markdown-link-check', function () {
urls: [baseUrl + '/basic-auth'],
headers: { 'Authorization': 'Basic Zm9vOmJhcg==', 'Foo': 'Bar' }
}
]
]
}, function (err, results) {
expect(err).to.be(null);
expect(results).to.be.an('array');
Expand Down Expand Up @@ -108,6 +128,12 @@ describe('markdown-link-check', function () {
// 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' },

Expand Down
2 changes: 2 additions & 0 deletions test/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This is a test file:
* [basic-auth](%%BASE_URL%%/basic-auth) (alive)
* [ignored](%%BASE_URL%%/something/not-working-and-ignored/something) (ignored)
* [replaced](%%BASE_URL%%/boo/bar)
* [later](%%BASE_URL%%/later)
* [partial](%%BASE_URL%%/partial)

![img](%%BASE_URL%%/hello.jpg) (alive)
![img](hello.jpg) (alive)
Expand Down