This repository was archived by the owner on Aug 20, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -27,9 +27,16 @@ var response = {
2727}
2828
2929// Client allows for quick and easy access any REST or REST-like API.
30- function Client ( globalRequest ) {
30+ function Client ( globalRequest , maxSockets ) {
3131 var emptyResponse = JSON . parse ( JSON . stringify ( response ) )
3232 var body = ''
33+ var httpAgent = null ;
34+ var httpsAgent = null ;
35+
36+ if ( maxSockets ) {
37+ httpAgent = new http . Agent ( { maxSockets : maxSockets } ) ;
38+ httpsAgent = new https . Agent ( { maxSockets : maxSockets } ) ;
39+ }
3340
3441 // utility function to create an empty request object
3542 this . emptyRequest = function ( ) {
@@ -81,6 +88,10 @@ function Client (globalRequest) {
8188 request . headers [ 'Content-Type' ] = 'application/json'
8289 }
8390
91+ //if maxsockets where provided use the apropriate agent
92+ if ( maxSockets ) {
93+ request . agent = endpointRequest . test == true ? httpAgent : httpsAgent
94+ }
8495 return request
8596 }
8697
Original file line number Diff line number Diff line change @@ -209,4 +209,35 @@ describe('Client', function () {
209209 } )
210210 } )
211211 } )
212+
213+ // limit maxSockets
214+ describe ( '#API()' , function ( ) {
215+ it ( 'should limit maxSockets' , function ( done ) {
216+ var expectedSockets = 10
217+ var maxSocketsClient = new Client ( globalRequest , expectedSockets )
218+
219+ nock ( TEST_HOST )
220+ . get ( '/testMax' )
221+ . reply ( 200 )
222+
223+ // monkey patch the http.request
224+ var http = require ( 'http' )
225+ var originalRequest = http . request
226+ http . request = function ( options , callback ) {
227+ assert . isDefined ( options . agent , 'the request should use a custom agent' ) ;
228+ assert . equal ( options . agent . maxSockets , expectedSockets , 'agent.maxSockets should equal expectedSockets' )
229+ return originalRequest ( options , callback )
230+ }
231+
232+ var requestGet = maxSocketsClient . emptyRequest ( )
233+ requestGet . method = 'GET'
234+ requestGet . test = true
235+ requestGet . path = '/testMax'
236+ maxSocketsClient . API ( requestGet , function ( response ) {
237+ //restore the opriginal request
238+ http . request = originalRequest
239+ done ( )
240+ } )
241+ } )
242+ } )
212243} )
You can’t perform that action at this time.
0 commit comments