Make the tests both proxy cased and redo the import

This commit is contained in:
joelwizard 2026-04-03 15:55:05 -07:00
parent d4973cc41a
commit aef0e9ddf9
No known key found for this signature in database
GPG Key ID: 7DBA6660BE571D64

View File

@ -12,47 +12,53 @@ import {describe, test, expect, beforeEach, afterEach} from '@jest/globals'
import { import {
createPipelineRequest, createPipelineRequest,
proxyPolicy,
type PipelineRequest, type PipelineRequest,
type SendRequest type SendRequest
} from '@typespec/ts-http-runtime' } from '@typespec/ts-http-runtime'
import {proxyPolicy} from '@typespec/ts-http-runtime/internal/policies'
import {HttpsProxyAgent} from 'https-proxy-agent' import {HttpsProxyAgent} from 'https-proxy-agent'
import {HttpProxyAgent} from 'http-proxy-agent' import {HttpProxyAgent} from 'http-proxy-agent'
describe('proxyPolicy', () => { describe('proxyPolicy', () => {
const PROXY_URL = 'http://corporate-proxy.example.com:3128' const PROXY_URL = 'http://corporate-proxy.example.com:3128'
let savedHttpsProxy: string | undefined // The runtime checks both uppercase and lowercase proxy env vars, so we
let savedHttpProxy: string | undefined // must save/clear/restore both casings to keep tests hermetic.
let savedNoProxy: string | undefined const PROXY_ENV_KEYS = [
'HTTPS_PROXY',
'https_proxy',
'HTTP_PROXY',
'http_proxy',
'NO_PROXY',
'no_proxy'
] as const
let savedEnv: Record<string, string | undefined>
beforeEach(() => { beforeEach(() => {
// Save and set proxy env vars // Save all proxy env vars
savedHttpsProxy = process.env['HTTPS_PROXY'] savedEnv = {}
savedHttpProxy = process.env['HTTP_PROXY'] for (const key of PROXY_ENV_KEYS) {
savedNoProxy = process.env['NO_PROXY'] savedEnv[key] = process.env[key]
}
// Set uppercase, delete lowercase to avoid ambiguity
process.env['HTTPS_PROXY'] = PROXY_URL process.env['HTTPS_PROXY'] = PROXY_URL
process.env['HTTP_PROXY'] = PROXY_URL process.env['HTTP_PROXY'] = PROXY_URL
delete process.env['https_proxy']
delete process.env['http_proxy']
delete process.env['NO_PROXY'] delete process.env['NO_PROXY']
delete process.env['no_proxy']
}) })
afterEach(() => { afterEach(() => {
// Restore original env // Restore original env
if (savedHttpsProxy !== undefined) { for (const key of PROXY_ENV_KEYS) {
process.env['HTTPS_PROXY'] = savedHttpsProxy if (savedEnv[key] !== undefined) {
} else { process.env[key] = savedEnv[key]
delete process.env['HTTPS_PROXY'] } else {
} delete process.env[key]
if (savedHttpProxy !== undefined) { }
process.env['HTTP_PROXY'] = savedHttpProxy
} else {
delete process.env['HTTP_PROXY']
}
if (savedNoProxy !== undefined) {
process.env['NO_PROXY'] = savedNoProxy
} else {
delete process.env['NO_PROXY']
} }
}) })