preRequest.test.js
4.73 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
/*jshint expr:true */
'use strict';
const Crawler = require('../lib/crawler');
const expect = require('chai').expect;
const sinon = require('sinon');
// settings for nock to mock http server
const nock = require('nock');
// init variables
let cb;
let crawler;
describe('preRequest feature tests', function() {
before(function() {
nock.cleanAll();
nock('http://test.crawler.com').get('/').reply(200, 'ok').persist();
});
beforeEach(function() {
cb = sinon.spy();
});
it('should do preRequest before request when preRequest defined in crawler options', function(finishTest) {
crawler = new Crawler({
jQuery: false,
preRequest: (options, done) => {
setTimeout(function() {
cb('preRequest');
done();
}, 50);
}
});
crawler.queue({
uri: 'http://test.crawler.com/',
callback: (error, response, done) => {
expect(error).to.be.null;
expect(cb.getCalls().length).to.equal(1);
expect(cb.getCalls()[0].args[0]).to.equal('preRequest');
done();
finishTest();
}
});
});
it('should do preRequest before request when preRequest defined in queue options', function(finishTest) {
crawler = new Crawler({ jQuery: false });
crawler.queue({
uri: 'http://test.crawler.com/',
preRequest: (options, done) => {
setTimeout(function() {
cb('preRequest');
done();
}, 50);
},
callback: (error, response, done) => {
expect(error).to.be.null;
expect(cb.getCalls().length).to.equal(1);
expect(cb.getCalls()[0].args[0]).to.equal('preRequest');
done();
finishTest();
}
});
});
it('preRequest should be executed the same times as request', function(finishTest) {
crawler = new Crawler({
jQuery: false,
rateLimit: 50,
preRequest: (options, done) => {
cb('preRequest');
done();
},
callback: (error, response, done) => {
expect(error).to.be.null;
cb('callback');
done();
}
});
const seq = [];
for(var i = 0; i < 5; i++) {
crawler.queue('http://test.crawler.com/');
seq.push('preRequest');
seq.push('callback');
}
crawler.queue({
uri: 'http://test.crawler.com/',
preRequest: (options, done) => { done(); },
callback: (error, response, done) => {
expect(cb.getCalls().map(c => c.args[0]).join()).to.equal(seq.join());
done();
finishTest();
}
});
});
it('when preRequest fail, should retry three times by default', function(finishTest) {
crawler = new Crawler({
jQuery: false,
rateLimit: 20,
retryTimeout: 0,
preRequest: (options, done) => {
cb('preRequest');
done(new Error());
},
callback: (error, response, done) => {
expect(error).to.exist;
expect(cb.getCalls().length).to.equal(4);
done();
finishTest();
}
});
crawler.queue('http://test.crawler.com/');
});
it('when preRequest fail, should return error when error.op = \'fail\'', function(finishTest) {
crawler = new Crawler({
jQuery: false,
rateLimit: 20,
retryTimeout: 0,
preRequest: (options, done) => {
cb('preRequest');
const error = new Error();
error.op = 'fail';
done(error);
},
callback: (error, response, done) => {
expect(error).to.exist;
expect(cb.getCalls().length).to.equal(1);
done();
finishTest();
}
});
crawler.queue('http://test.crawler.com/');
});
it('when preRequest fail, callback should not be called when error.op = \'abort\'', function(finishTest) {
crawler = new Crawler({
jQuery: false,
rateLimit: 20,
retries: 0,
preRequest: (options, done) => {
cb('preRequest');
let error = new Error();
error.op = 'abort';
done(error);
setTimeout(function() {
expect(cb.getCalls().length).to.equal(1);
for (let i = 0; i < cb.getCalls().length; i++) {
expect(cb.getCalls()[i].args[0]).to.equal('preRequest');
}
finishTest();
}, 100);
},
callback: () => {
expect(null).to.equal(1);
}
});
crawler.queue('http://test.crawler.com/');
});
it('when preRequest fail, should put request back in queue when error.op = \'queue\'', function(finishTest) {
let counter = 0;
crawler = new Crawler({
jQuery: false,
rateLimit: 20,
preRequest: (options, done) => {
expect(options.retries).to.equal(3);
let error = new Error();
error.op = 'queue';
if(++counter > 3) {
expect(cb.getCalls().length).to.equal(3);
for (let i = 0; i < cb.getCalls().length; i++) {
expect(cb.getCalls()[i].args[0]).to.equal('preRequest');
}
// if error.op not set to abort, the task will continue, test will fail if you have more tests to go other than this
error.op = 'abort';
finishTest();
}
cb('preRequest');
done(error);
},
callback: () => {
expect(null).to.equal(1);
}
});
crawler.queue('http://test.crawler.com/');
});
});