license import * as assert from 'assert' ; import { describe , it , afterEach } from 'mocha' ; import * as http from 'http' ; import * as https from 'https' ; import * as sinon from 'sinon' ; import { getAgent , pool } from '../src/agents' ; const HttpProxyAgent = require ( 'http-proxy-agent' ) ; const HttpsProxyAgent = require ( 'https-proxy-agent' ) ; describe ( 'agents' , ( ) => { const httpUri = 'http://example.com' ; const httpsUri = 'https://example.com' ; const sandbox = sinon . createSandbox ( ) ; afterEach ( ( ) => { sandbox . restore ( ) ; pool . clear ( ) ; } ) ; describe ( 'getAgent' , ( ) => { const defaultOptions = { uri : httpUri } ; it ( 'should return undefined by default' , ( ) => { const agent = getAgent ( httpUri , defaultOptions ) ; assert . strictEqual ( agent , undefined ) ; } ) ; describe ( 'proxy' , ( ) => { const envVars = [ 'http_proxy' , 'https_proxy' , 'HTTP_PROXY' , 'HTTPS_PROXY' , ] ; describe ( 'http' , ( ) => { const uri = httpUri ; const proxy = 'http://hello.there:8080' ; const proxyExpected = { hostname : 'hello.there' , port : 8080 , protocol : 'http:' , } ; it ( 'should respect the proxy option' , ( ) => { const options = Object . assign ( { proxy } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert ( agent instanceof HttpProxyAgent ) ; const { proxy : proxyActual } = agent ! ; assert . strictEqual ( proxyActual . protocol , proxyExpected . protocol ) ; assert . strictEqual ( proxyActual . hostname , proxyExpected . hostname ) ; assert . strictEqual ( proxyActual . port , proxyExpected . port ) ; } ) ; envVars . forEach ( envVar => { it ( `should respect the ${ envVar } env var` , ( ) => { process . env [ envVar ] = proxy ; const agent = getAgent ( uri , defaultOptions ) ; assert ( agent instanceof HttpProxyAgent ) ; delete process . env [ envVar ] ; } ) ; } ) ; } ) ; describe ( 'https' , ( ) => { const uri = httpsUri ; const proxy = 'https://hello.there:8080' ; const proxyExpected = { hostname : 'hello.there' , port : 8080 , protocol : 'https:' , } ; it ( 'should respect the proxy option' , ( ) => { const options = Object . assign ( { proxy } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert ( agent instanceof HttpsProxyAgent ) ; const { proxy : proxyActual } = agent ! ; assert . strictEqual ( proxyActual . protocol , proxyExpected . protocol ) ; assert . strictEqual ( proxyActual . hostname , proxyExpected . hostname ) ; assert . strictEqual ( proxyActual . port , proxyExpected . port ) ; } ) ; envVars . forEach ( envVar => { it ( `should respect the ${ envVar } env var` , ( ) => { process . env [ envVar ] = proxy ; const agent = getAgent ( uri , defaultOptions ) ; assert ( agent instanceof HttpsProxyAgent ) ; delete process . env [ envVar ] ; } ) ; } ) ; } ) ; } ) ; describe ( 'forever' , ( ) => { describe ( 'http' , ( ) => { const uri = httpUri ; const options = Object . assign ( { forever : true } , defaultOptions ) ; it ( 'should return an http Agent' , ( ) => { const agent = getAgent ( uri , options ) ! ; assert ( agent instanceof http . Agent ) ; } ) ; it ( 'should cache the agent' , ( ) => { const agent1 = getAgent ( uri , options ) ; const agent2 = getAgent ( uri , options ) ; assert . strictEqual ( agent1 , agent2 ) ; } ) ; } ) ; describe ( 'https' , ( ) => { const uri = httpsUri ; const options = Object . assign ( { forever : true } , defaultOptions ) ; it ( 'should return an http Agent' , ( ) => { const agent = getAgent ( uri , options ) ! ; assert ( agent instanceof https . Agent ) ; } ) ; it ( 'should cache the agent' , ( ) => { const agent1 = getAgent ( uri , options ) ; const agent2 = getAgent ( uri , options ) ; assert . strictEqual ( agent1 , agent2 ) ; } ) ; } ) ; } ) ; describe ( 'pool' , ( ) => { describe ( 'http' , ( ) => { const uri = httpUri ; it ( 'should pass AgentOptions from pool config when providing agent' , ( ) => { const options = Object . assign ( { forever : true , pool : { maxSockets : 1000 , } , } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert . strictEqual ( agent ! . maxSockets , 1000 ) ; } ) ; it ( 'should not set global AgentOptions from only pool config' , ( ) => { const options = Object . assign ( { pool : { maxSockets : 1000 , } , } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert . strictEqual ( agent , undefined ) ; assert . notStrictEqual ( http . globalAgent . maxSockets , 1000 ) ; } ) ; } ) ; describe ( 'https' , ( ) => { const uri = httpsUri ; it ( 'should pass AgentOptions from pool config when providing agent' , ( ) => { const options = Object . assign ( { forever : true , pool : { maxSockets : 1000 , } , } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert . strictEqual ( agent ! . maxSockets , 1000 ) ; } ) ; it ( 'should not set global AgentOptions from only pool config' , ( ) => { const options = Object . assign ( { pool : { maxSockets : 1000 , } , } , defaultOptions ) ; const agent = getAgent ( uri , options ) ; assert . strictEqual ( agent , undefined ) ; assert . notStrictEqual ( https . globalAgent . maxSockets , 1000 ) ; } ) ; } ) ; } ) ; } ) ; } ) ;