From ab38dab7a67eefd6b8bd4e2516e98fe147dcc2de Mon Sep 17 00:00:00 2001 From: James Gatz Date: Fri, 20 Dec 2024 13:14:59 +0100 Subject: [PATCH] feat(Inso): Add proxy settings to inso cli (#8257) * Use environment variables to set proxy settings on inso * move proxy settings to cli commands * cleanup * proxyEnabled settings --- packages/insomnia-inso/src/cli.ts | 58 ++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/insomnia-inso/src/cli.ts b/packages/insomnia-inso/src/cli.ts index f47a62ebe6b..7e56ea14b64 100644 --- a/packages/insomnia-inso/src/cli.ts +++ b/packages/insomnia-inso/src/cli.ts @@ -309,6 +309,25 @@ export const go = (args?: string[]) => { const program = new commander.Command(); const version = process.env.VERSION || packageJson.version; + const proxySettings: { + proxyEnabled: boolean; + httpProxy: string; + httpsProxy: string; + noProxy: string; + } = { + proxyEnabled: false, + httpProxy: '', + httpsProxy: '', + noProxy: '', + }; + + if (process.env.HTTP_PROXY || process.env.HTTPS_PROXY) { + proxySettings.proxyEnabled = true; + proxySettings.httpProxy = process.env.HTTP_PROXY || ''; + proxySettings.httpsProxy = process.env.HTTPS_PROXY || ''; + proxySettings.noProxy = process.env.NO_PROXY || ''; + } + // export and lint logic // Provide a path to a file which looks like an insomnia db // it may contain multiple workspaces, and specs. @@ -354,7 +373,10 @@ export const go = (args?: string[]) => { .option('-b, --bail', 'abort ("bail") after first test failure', false) .option('--keepFile', 'do not delete the generated test file', false) .option('-k, --disableCertValidation', 'disable certificate validation for requests with SSL', false) - .action(async (identifier, cmd: { env: string; testNamePattern: string; reporter: TestReporter; bail: boolean; keepFile: boolean; disableCertValidation: boolean; ci: boolean }) => { + .option('--httpsProxy ', 'proxy server for https requests', proxySettings.httpsProxy) + .option('--httpProxy ', 'proxy server for http requests', proxySettings.httpProxy) + .option('--noProxy ', 'proxy server for no proxy requests', proxySettings.noProxy) + .action(async (identifier, cmd: { env: string; testNamePattern: string; reporter: TestReporter; bail: boolean; keepFile: boolean; disableCertValidation: boolean; ci: boolean; httpsProxy?: string; httpProxy?: string; noProxy?: string }) => { const globals: GlobalOptions = program.optsWithGlobals(); const commandOptions = { ...globals, ...cmd }; const __configFile = await tryToReadInsoConfigFile(commandOptions.config, commandOptions.workingDir); @@ -413,8 +435,20 @@ export const go = (args?: string[]) => { data: {}, }; + const proxyOptions: { + proxyEnabled: boolean; + httpProxy?: string; + httpsProxy?: string; + noProxy?: string; + } = { + proxyEnabled: Boolean(options.httpProxy || options.httpsProxy), + httpProxy: options.httpProxy, + httpsProxy: options.httpsProxy, + noProxy: options.noProxy, + }; + try { - const sendRequest = await getSendRequestCallbackMemDb(environment._id, db, transientVariables, { validateSSL: !options.disableCertValidation }); + const sendRequest = await getSendRequestCallbackMemDb(environment._id, db, transientVariables, { validateSSL: !options.disableCertValidation, ...proxyOptions }); // Generate test file const testFileContents = generate(suites.map(suite => ({ name: suite.name, @@ -454,7 +488,10 @@ export const go = (args?: string[]) => { .option('-r, --reporter ', `reporter to use, options are [${reporterTypes.join(', ')}]`, defaultReporter) .option('-b, --bail', 'abort ("bail") after first non-200 response', false) .option('--disableCertValidation', 'disable certificate validation for requests with SSL', false) - .action(async (identifier, cmd: { env: string; globals: string; disableCertValidation: boolean; requestNamePattern: string; bail: boolean; item: string[]; delayRequest: string; iterationCount: string; iterationData: string; envVar: string[] }) => { + .option('--httpsProxy ', 'proxy server for https requests', proxySettings.httpsProxy) + .option('--httpProxy ', 'proxy server for http requests', proxySettings.httpProxy) + .option('--noProxy ', 'proxy server for no proxy requests', proxySettings.noProxy) + .action(async (identifier, cmd: { env: string; globals: string; disableCertValidation: boolean; requestNamePattern: string; bail: boolean; item: string[]; delayRequest: string; iterationCount: string; iterationData: string; envVar: string[]; httpsProxy?: string; httpProxy?: string; noProxy?: string }) => { const globals: { config: string; workingDir: string; exportFile: string; ci: boolean; printOptions: boolean; verbose: boolean } = program.optsWithGlobals(); const commandOptions = { ...globals, ...cmd }; @@ -603,7 +640,20 @@ export const go = (args?: string[]) => { name: 'Transient Variables', data: {}, }; - const sendRequest = await getSendRequestCallbackMemDb(environment._id, db, transientVariables, { validateSSL: !options.disableCertValidation }, iterationData, iterationCount); + + const proxyOptions: { + proxyEnabled: boolean; + httpProxy?: string; + httpsProxy?: string; + noProxy?: string; + } = { + proxyEnabled: Boolean(options.httpProxy || options.httpsProxy), + httpProxy: options.httpProxy, + httpsProxy: options.httpsProxy, + noProxy: options.noProxy, + }; + + const sendRequest = await getSendRequestCallbackMemDb(environment._id, db, transientVariables, { validateSSL: !options.disableCertValidation, ...proxyOptions }, iterationData, iterationCount); let success = true; for (let i = 0; i < iterationCount; i++) { let reqIndex = 0;