Skip to content
This repository was archived by the owner on Aug 20, 2020. It is now read-only.

feat: Added maxSockets support. #15

Merged
merged 1 commit into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ var response = {
}

// Client allows for quick and easy access any REST or REST-like API.
function Client (globalRequest) {
function Client (globalRequest, maxSockets) {
var emptyResponse = JSON.parse(JSON.stringify(response))
var body = ''
var httpAgent = null;
var httpsAgent = null;

if(maxSockets) {
httpAgent = new http.Agent({ maxSockets: maxSockets });
httpsAgent = new https.Agent({ maxSockets: maxSockets });
}

// utility function to create an empty request object
this.emptyRequest = function () {
Expand Down Expand Up @@ -81,6 +88,10 @@ function Client (globalRequest) {
request.headers['Content-Type'] = 'application/json'
}

//if maxsockets where provided use the apropriate agent
if(maxSockets) {
request.agent = endpointRequest.test == true ? httpAgent : httpsAgent
}
return request
}

Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,35 @@ describe('Client', function () {
})
})
})

// limit maxSockets
describe('#API()', function () {
it('should limit maxSockets', function (done) {
var expectedSockets = 10
var maxSocketsClient = new Client(globalRequest, expectedSockets)

nock(TEST_HOST)
.get('/testMax')
.reply(200)

// monkey patch the http.request
var http = require('http')
var originalRequest = http.request
http.request = function(options, callback){
assert.isDefined(options.agent, 'the request should use a custom agent');
assert.equal(options.agent.maxSockets, expectedSockets, 'agent.maxSockets should equal expectedSockets')
return originalRequest(options, callback)
}

var requestGet = maxSocketsClient.emptyRequest()
requestGet.method = 'GET'
requestGet.test = true
requestGet.path = '/testMax'
maxSocketsClient.API(requestGet, function (response) {
//restore the opriginal request
http.request = originalRequest
done()
})
})
})
})