callback.test.js
1.51 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
'use strict';
const Crawler = require('../lib/crawler');
const nock = require('nock');
describe('Callback test', function() {
before(function() {
nock.cleanAll();
});
let crawler = null;
const url = 'http://www.whatever.com';
beforeEach(() => {
crawler = new Crawler({
retryTimeout:0,
retries:0,
timeout:100,
logger: {
log:() => {}
},
});
});
afterEach(() => {
crawler = null;
});
it('should end as expected without callback', function(done) {
nock(url)
.get('/get')
.reply(200, '<html></html>',{
'Content-Type': 'text/html'
});
crawler.on('drain', done);
crawler.queue(`${url}/get`);
});
it('should end as expected without callback when timedout', function(done) {
/*
* TODO: request.js claim that it has ETIMEDOUT error which means time spent by the server to send response headers
* But the source code reflects the point is `connect` event on socket.
*/
nock(url)
.get('/delay')
//.delay({head:1000})
//.delayConnection(5000)
.delayBody(500)
//.socketDelay(2000)
.reply(200, '<html></html>',{
'Content-Type': 'text/html'
});
crawler.on('drain', done);
crawler.queue(`${url}/delay`);
});
it('should end as expected without callback when encoding error', function(done) {
nock(url)
.get('/get')
.reply(200, '<html></html>',{
'Content-Type': 'text/html'
});
crawler._doEncoding = function(){throw new Error('Error for testing.');};
crawler.on('drain', done);
crawler.queue(`${url}/get`);
});
});