From 08c9dd7e8be23fbdbcb82f3cb62ad70fa60331a5 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Thu, 25 Oct 2018 13:43:35 +0200 Subject: [PATCH] fix(keep-alive): dont pass undefined property resolves PERS-OPS --- lib/wrapper.js | 9 ++++++--- lib/wrapper.spec.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/wrapper.js b/lib/wrapper.js index 36020e0..137d6a3 100644 --- a/lib/wrapper.js +++ b/lib/wrapper.js @@ -34,11 +34,14 @@ class RequestWrapper { transformResponse: [body => body], maxContentLength: this.requestOptions.maxContentLength, validateStatus: () => true, - cancelToken: source.token, - httpAgent: this.requestOptions.httpAgent, - httpsAgent: this.requestOptions.httpsAgent + cancelToken: source.token }; + if (this.requestOptions.httpAgent && this.requestOptions.httpsAgent) { + axiosOptions.httpAgent = this.requestOptions.httpAgent; + axiosOptions.httpsAgent = this.requestOptions.httpsAgent; + } + return axios .request(axiosOptions) .then( diff --git a/lib/wrapper.spec.js b/lib/wrapper.spec.js index bc299a6..dc39bb2 100644 --- a/lib/wrapper.spec.js +++ b/lib/wrapper.spec.js @@ -4,6 +4,8 @@ const axios = require('axios'); const Wrapper = require('./wrapper'); const SuiteRequestError = require('./requestError'); const RequestOption = require('./requestOption'); +const http = require('http'); +const https = require('https'); describe('Wrapper', function() { let apiResponse; @@ -73,6 +75,25 @@ describe('Wrapper', function() { expect(response).to.be.eql(expectedApiResponse); const requestArgument = requestGetStub.args[0][0]; expect(requestArgument).to.containSubset(expectedRequestOptions); + expect(requestArgument).not.to.have.own.property('httpAgent'); + expect(requestArgument).not.to.have.own.property('httpsAgent'); + }); + + it('should pass http agents to axios', function *() { + const agents = { + httpAgent: new http.Agent({ keepAlive: true }), + httpsAgent: new https.Agent({ keepAlive: true }) + }; + wrapper = new Wrapper( + Object.assign(agents, escherRequestOptions), + 'http:' + ); + + yield wrapper.send(); + + const requestArgument = requestGetStub.args[0][0]; + expect(requestArgument.httpAgent).to.eql(agents.httpAgent); + expect(requestArgument.httpsAgent).to.eql(agents.httpsAgent); }); it('should throw error when response code is 400 or above', function *() {