direct.test.js
2.83 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
/*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('Direct feature tests', function() {
before(function() {
nock.cleanAll();
nock('http://test.crawler.com').get('/').reply(200, 'ok').persist();
});
beforeEach(function() {
cb = sinon.spy();
crawler = new Crawler({
jQuery: false,
rateLimit: 100,
preRequest: (options, done) => {
cb('preRequest');
done();
},
callback: (err, res, done) => {
if (err) {
cb('error');
} else {
cb('callback');
}
done();
}
});
crawler.on('request', () => {
cb('Event:request');
});
});
it('should not trigger preRequest or callback of crawler instance', function(finishTest) {
crawler.direct({
uri: 'http://test.crawler.com/',
callback: (error, res) => {
expect(error).to.be.null;
expect(res.statusCode).to.equal(200);
expect(res.body).to.equal('ok');
expect(cb.called).to.be.false;
finishTest();
}
});
});
it('should be sent directly regardless of current queue of crawler', function(finishTest) {
crawler.queue({
uri: 'http://test.crawler.com/',
callback: (error, res, done) => {
expect(error).to.be.null;
crawler.direct({
uri: 'http://test.crawler.com/',
callback: () => {
expect(cb.getCalls().length).to.equal(2);
cb('direct');
}
});
done();
}
});
crawler.queue('http://test.crawler.com/');
crawler.queue('http://test.crawler.com/');
crawler.queue({
uri: 'http://test.crawler.com/',
callback: (error, res, done) => {
expect(error).to.be.null;
let seq = ['preRequest','Event:request','direct','preRequest','Event:request','callback','preRequest','Event:request','callback','preRequest','Event:request'];
expect(cb.getCalls().map(c => c.args[0]).join()).to.equal(seq.join());
expect(cb.getCalls().length).to.equal(11);
done();
finishTest();
}
});
});
it('should not trigger Event:request by default', function(finishTest) {
crawler.direct({
uri: 'http://test.crawler.com/',
callback: (error, res) => {
expect(error).to.be.null;
expect(res.statusCode).to.equal(200);
expect(res.body).to.equal('ok');
expect(cb.called).to.be.false;
finishTest();
}
});
});
it('should trigger Event:request if specified in options', function(finishTest) {
crawler.direct({
uri: 'http://test.crawler.com/',
skipEventRequest: false,
callback: (error, res) => {
expect(error).to.be.null;
expect(res.statusCode).to.equal(200);
expect(res.body).to.equal('ok');
expect(cb.calledOnce).to.be.true;
expect(cb.firstCall.args[0]).to.equal('Event:request');
finishTest();
}
});
});
});