Skip to content

Commit

Permalink
add testing cases for the agentcache
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Nov 6, 2024
1 parent 02444e3 commit 6731437
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/http/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ function getProxyAgent(proxyOptions, parsedUrl, destination, mock) {
try {
destHost = new URL(destination).hostname;
} catch (err) {
Logger.getInstance().error(`Failed to parse the destination to URL with the error: ${err}. Use the full destination as the host: ${destHost}`);
destHost = destination;
}
Logger.getInstance().debug(`Destination is ${destHost}`);
Logger.getInstance().debug(`The destination host is: ${destHost}`);

const agentId = createAgentId(agentOptions.protocol, agentOptions.hostname, destHost, agentOptions.keepAlive);
const bypassProxy = isBypassProxy(proxyOptions, destination);
let agent;
Expand All @@ -155,4 +157,9 @@ function createAgentId(protocol, hostname, destination, keepAlive) {
return `${protocol}//${hostname}-${destination}-${keepAlive ? 'keepAlive' : 'noKeepAlive'}`;
}

module.exports = { NodeHttpClient, getProxyAgent };
//This is for the testing purpose.
function getAgentCacheSize() {
return httpsAgentCache.size;
}

module.exports = { NodeHttpClient, getProxyAgent, getAgentCacheSize };
70 changes: 70 additions & 0 deletions test/unit/agent_cache_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const GlobalConfig = require('./../../lib/global_config');
const getProxyAgent = require('./../../lib/http/node').getProxyAgent;
const getAgentCacheSize = require('./../../lib/http/node').getAgentCacheSize;
const assert = require('assert');

describe('getProxtAgent', function () {
const mockProxy = new URL('https://user:[email protected]:1234');
const fakeAccessUrl = new URL('http://fakeaccount.snowflakecomputing.com');

const testCases = [
{
destination: 'test.destination.com',
isNewAgent: true,
keepAlive: true
},
{
destination: 's3.amazonaws.com',
isNewAgent: true,
keepAlive: true
},
{
destination: 'http://test.destination.com/login/somewhere',
isNewAgent: false,
keepAlive: true
},
{
destination: 'http://s3.amazonaws.com',
isNewAgent: false,
keepAlive: true
},
{
destination: 'https://s3.amazonaws.com',
isNewAgent: true,
keepAlive: false
},
{
destination: 'https://test.destination.com/login/somewhere',
isNewAgent: true,
keepAlive: false
},
{
destination: 'https://fakeaccount.snowflakecomputing.com/login/sessionId=something',
isNewAgent: true,
keepAlive: true
},
{
destination: 'https://fakeaccount.snowflakecomputing.com/other/request',
isNewAgent: false,
keepAlive: true
},
{
destination: 'http://fakeaccount.snowflakecomputing.com/another/request',
isNewAgent: true,
keepAlive: false
},
];

it('test http(s) agent cache', () => {
let numofAgent = 0;
testCases.forEach(({ destination, isNewAgent, keepAlive }) => {
GlobalConfig.setKeepAlive(keepAlive);
getProxyAgent(mockProxy, fakeAccessUrl, destination);
if (isNewAgent) {
numofAgent++;
}
assert.strictEqual(getAgentCacheSize(), numofAgent);
});
});
});

0 comments on commit 6731437

Please sign in to comment.