-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
65 lines (59 loc) · 1.77 KB
/
test.js
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
'use strict';
require('mocha');
const assert = require('assert');
const request = require('./');
function next(limit) {
return async function(url, res, acc) {
const regex = /href=[^\s]*?\/page\/(\d+)\//g;
let matches;
while (matches = regex.exec(res.data)) {
if (matches === null) {
break;
}
const num = parseInt(matches[1], 10);
if (isNaN(num)) {
break;
}
const url = `${acc.orig}/page/${num}/`;
// If we have already requested this `url`, we will skip it, and try the next `url` on the page.
if (acc.urls.includes(url)) {
continue;
}
if (num > limit) {
break;
}
return url;
}
};
}
describe('paged-request', function() {
this.timeout(10000);
it('should make a request for paged content', function() {
const limit = 2;
return request('https://www.smashingmagazine.com/category/css', {}, next(limit))
.then(acc => {
assert.strictEqual(acc.pages.length, limit);
});
});
it('should throw an error when page does not exist', function() {
const limit = 1;
return request('https://www.smashingmagazine.com/dflsjfslkfskfjds', {}, next(limit))
.then(() => {
throw new Error('expected an error');
})
.catch(err => {
assert.ok(/404/.test(err.message));
});
});
it('should keep the history of `urls`', function() {
const limit = 3;
return request('https://www.smashingmagazine.com/category/css', {}, next(limit))
.then(acc => {
assert.deepStrictEqual(acc.urls, [
'https://www.smashingmagazine.com/category/css',
'https://www.smashingmagazine.com/category/css/page/2/',
'https://www.smashingmagazine.com/category/css/page/3/'
]);
});
});
});