1+ /* global describe, it */
2+ import Networking from '../../src/networking' ;
3+ import superagent from 'superagent' ;
4+ import sinon from 'sinon' ;
5+ import nock from 'nock' ;
6+ import assert from 'assert' ;
7+ import { del , get , post } from '../../src/networking/modules/web-node' ;
8+ import { keepAlive , proxy } from '../../src/networking/modules/node' ;
9+
10+ describe ( 'keep-alive agent' , ( ) => {
11+
12+ before ( ( ) => nock . disableNetConnect ( ) ) ;
13+ after ( ( ) => nock . enableNetConnect ( ) ) ;
14+
15+ const setupNetwork = ( shouldSecure , enableKeepAlive ) => {
16+ const config = { origin : 'ps.pndsn.com' , secure : shouldSecure , keepAlive : enableKeepAlive , logVerbosity : false } ;
17+ const networking = new Networking ( { keepAlive, del, get, post, proxy } ) ;
18+ networking . init ( config ) ;
19+
20+ return networking ;
21+ }
22+
23+ it ( 'should not create if \'keepAlive\' is \'false\'' , ( ) => {
24+ const networking = setupNetwork ( false , false ) ;
25+ const superagentGetSpy = sinon . spy ( superagent , 'get' ) ;
26+
27+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
28+ assert ( superagentGetSpy . called , 'superagent not called with get' ) ;
29+ assert ( superagentGetSpy . returnValues [ 0 ] , 'request instance not created' ) ;
30+ assert ( ! superagentGetSpy . returnValues [ 0 ] . _agent , 'keep-alive agent should not be created' ) ;
31+
32+ superagentGetSpy . restore ( ) ;
33+ } )
34+
35+ it ( 'should create agent for insecure connection' , ( ) => {
36+ const networking = setupNetwork ( false , true ) ;
37+ const superagentGetSpy = sinon . spy ( superagent , 'get' ) ;
38+
39+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
40+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent , 'keep-alive agent not created' ) ;
41+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent . defaultPort !== 443 , 'keep-alive agent should access TLS (80) port' ) ;
42+
43+ superagentGetSpy . restore ( ) ;
44+ } )
45+
46+ it ( 'should re-use created agent for insecure connection' , ( ) => {
47+ const networking = setupNetwork ( false , true ) ;
48+ const superagentGetSpy = sinon . spy ( superagent , 'get' ) ;
49+
50+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
51+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
52+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent === superagentGetSpy . returnValues [ 1 ] . _agent , 'same user-agent should be used' ) ;
53+
54+ superagentGetSpy . restore ( ) ;
55+ } )
56+
57+ it ( 'should create agent for secure connection' , ( ) => {
58+ const networking = setupNetwork ( true , true ) ;
59+ const superagentGetSpy = sinon . spy ( superagent , 'get' ) ;
60+
61+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
62+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent , 'keep-alive agent not created' ) ;
63+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent . defaultPort === 443 , 'keep-alive agent should access SSL (443) port' ) ;
64+
65+ superagentGetSpy . restore ( ) ;
66+ } )
67+
68+ it ( 'should re-use created agent for secure connection' , ( ) => {
69+ const networking = setupNetwork ( true , true ) ;
70+ const superagentGetSpy = sinon . spy ( superagent , 'get' ) ;
71+
72+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
73+ networking . GET ( { } , { url : '/time/0' } , ( ) => { } ) ;
74+ assert ( superagentGetSpy . returnValues [ 0 ] . _agent === superagentGetSpy . returnValues [ 1 ] . _agent , 'same user-agent should be used' ) ;
75+
76+ superagentGetSpy . restore ( ) ;
77+ } )
78+ } ) ;
0 commit comments