This repository was archived by the owner on Aug 20, 2020. It is now read-only.
File tree 2 files changed +43
-1
lines changed
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -27,9 +27,16 @@ var response = {
27
27
}
28
28
29
29
// Client allows for quick and easy access any REST or REST-like API.
30
- function Client ( globalRequest ) {
30
+ function Client ( globalRequest , maxSockets ) {
31
31
var emptyResponse = JSON . parse ( JSON . stringify ( response ) )
32
32
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
+ }
33
40
34
41
// utility function to create an empty request object
35
42
this . emptyRequest = function ( ) {
@@ -81,6 +88,10 @@ function Client (globalRequest) {
81
88
request . headers [ 'Content-Type' ] = 'application/json'
82
89
}
83
90
91
+ //if maxsockets where provided use the apropriate agent
92
+ if ( maxSockets ) {
93
+ request . agent = endpointRequest . test == true ? httpAgent : httpsAgent
94
+ }
84
95
return request
85
96
}
86
97
Original file line number Diff line number Diff line change @@ -209,4 +209,35 @@ describe('Client', function () {
209
209
} )
210
210
} )
211
211
} )
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
+ } )
212
243
} )
You can’t perform that action at this time.
0 commit comments