jar.test.js
1.6 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
/*jshint expr:true */
'use strict';
const Crawler = require('../lib/crawler');
const request = require('request');
const expect = require('chai').expect;
// settings for nock to mock http server
const nock = require('nock');
describe('Jar Options', function() {
before(function() {
nock.cleanAll();
nock('http://test.crawler.com/').get('/setCookie').reply(function() {
let response = [
200,
'ok',
{ 'Set-Cookie': `ping=pong; Domain=.crawler.com; Expires=${new Date(Date.now()+86400000).toUTCString()}; Path=/` }
];
return response;
}).persist();
nock('http://test.crawler.com/').get('/getCookie').reply(200, function() {
return this.req.headers.cookie;
}).persist();
});
after(function() {
delete require.cache[nock];
});
let jar = request.jar();
jar.setCookie(request.cookie('foo=bar'), 'http://test.crawler.com');
let crawler = new Crawler({
jquery: false,
jar: jar
});
it('should send with cookie when setting jar options', function(finishTest) {
crawler.queue({
uri: 'http://test.crawler.com/getCookie',
callback: (error, response, done) => {
expect(error).to.be.null;
expect(response.body).to.equal(jar.getCookieString('http://test.crawler.com'));
done();
finishTest();
}
});
});
it('should set cookie when response set-cookie headers exist', function(finishTest) {
crawler.queue({
uri: 'http://test.crawler.com/setCookie',
callback: (error, response, done) => {
expect(error).to.be.null;
expect(jar.getCookieString('http://test.crawler.com').indexOf('ping=pong') > -1).to.be.true;
done();
finishTest();
}
});
});
});