-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathnetworking.test.js
More file actions
79 lines (59 loc) · 3.17 KB
/
Copy pathnetworking.test.js
File metadata and controls
79 lines (59 loc) · 3.17 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
/* global describe, it, before, after */
import superagent from 'superagent';
import sinon from 'sinon';
import nock from 'nock';
import assert from 'assert';
import Networking from '../../src/networking';
import { del, get, post } from '../../src/networking/modules/web-node';
import { keepAlive, proxy } from '../../src/networking/modules/node';
describe('keep-alive agent', () => {
before(() => nock.disableNetConnect());
after(() => nock.enableNetConnect());
const setupNetwork = (shouldSecure, enableKeepAlive) => {
const config = { origin: 'ps.pndsn.com', secure: shouldSecure, keepAlive: enableKeepAlive, logVerbosity: false };
const networking = new Networking({ keepAlive, del, get, post, proxy });
networking.init(config);
return networking;
};
it('should not create if \'keepAlive\' is \'false\'', () => {
const networking = setupNetwork(false, false);
const superagentGetSpy = sinon.spy(superagent, 'get');
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
assert(superagentGetSpy.called, 'superagent not called with get');
assert(superagentGetSpy.returnValues[0], 'request instance not created');
assert(!superagentGetSpy.returnValues[0]._agent, 'keep-alive agent should not be created');
superagentGetSpy.restore();
});
it('should create agent for insecure connection', () => {
const networking = setupNetwork(false, true);
const superagentGetSpy = sinon.spy(superagent, 'get');
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
assert(superagentGetSpy.returnValues[0]._agent, 'keep-alive agent not created');
assert(superagentGetSpy.returnValues[0]._agent.defaultPort !== 443, 'keep-alive agent should access TLS (80) port');
superagentGetSpy.restore();
});
it('should re-use created agent for insecure connection', () => {
const networking = setupNetwork(false, true);
const superagentGetSpy = sinon.spy(superagent, 'get');
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
assert(superagentGetSpy.returnValues[0]._agent === superagentGetSpy.returnValues[1]._agent, 'same user-agent should be used');
superagentGetSpy.restore();
});
it('should create agent for secure connection', () => {
const networking = setupNetwork(true, true);
const superagentGetSpy = sinon.spy(superagent, 'get');
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
assert(superagentGetSpy.returnValues[0]._agent, 'keep-alive agent not created');
assert(superagentGetSpy.returnValues[0]._agent.defaultPort === 443, 'keep-alive agent should access SSL (443) port');
superagentGetSpy.restore();
});
it('should re-use created agent for secure connection', () => {
const networking = setupNetwork(true, true);
const superagentGetSpy = sinon.spy(superagent, 'get');
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
networking.GET({}, { url: '/time/0', headers: {} }, () => {});
assert(superagentGetSpy.returnValues[0]._agent === superagentGetSpy.returnValues[1]._agent, 'same user-agent should be used');
superagentGetSpy.restore();
});
});