test-proxy.js.old•4.97 kB
#!/usr/bin/env node
/**
* Test script to verify proxy configuration works correctly with Node.js fetch API
*
* This script tests:
* 1. Proxy agent creation with various proxy URL formats
* 2. Compatibility with Node.js fetch API (dispatcher option)
* 3. Environment variable handling
*/
import { createProxyAgent } from './dist/proxy.js';
console.log('=== Proxy Configuration Test ===\n');
// Test 1: No proxy configured
console.log('Test 1: No proxy environment variables');
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
delete process.env.http_proxy;
delete process.env.https_proxy;
const agent1 = createProxyAgent('http://example.com');
console.log('Result:', agent1 === undefined ? '✅ PASS - No agent created' : '❌ FAIL - Agent created when it should not');
console.log();
// Test 2: Valid HTTP proxy
console.log('Test 2: Valid HTTP_PROXY');
process.env.HTTP_PROXY = 'http://proxy.example.com:8080';
try {
const agent2 = createProxyAgent('http://example.com');
console.log('Result:', agent2 ? '✅ PASS - ProxyAgent created' : '❌ FAIL - No agent created');
console.log('Agent type:', agent2?.constructor?.name);
// Verify it has the dispatcher interface
if (agent2) {
console.log('Has dispatch method:', typeof agent2.dispatch === 'function' ? '✅ Yes' : '❌ No');
}
} catch (error) {
console.log('❌ FAIL - Error:', error.message);
}
console.log();
// Test 3: Proxy with authentication
console.log('Test 3: Proxy with authentication');
process.env.HTTP_PROXY = 'http://user:pass@proxy.example.com:8080';
try {
const agent3 = createProxyAgent('https://example.com');
console.log('Result:', agent3 ? '✅ PASS - ProxyAgent with auth created' : '❌ FAIL - No agent created');
console.log('Agent type:', agent3?.constructor?.name);
} catch (error) {
console.log('❌ FAIL - Error:', error.message);
}
console.log();
// Test 4: Invalid proxy URL
console.log('Test 4: Invalid proxy URL');
process.env.HTTP_PROXY = 'not-a-valid-url';
try {
const agent4 = createProxyAgent('http://example.com');
console.log('❌ FAIL - Should have thrown error but created agent');
} catch (error) {
console.log('Result: ✅ PASS - Correctly threw error');
console.log('Error message:', error.message);
}
console.log();
// Test 5: Unsupported protocol
console.log('Test 5: Unsupported protocol');
process.env.HTTP_PROXY = 'socks5://proxy.example.com:1080';
try {
const agent5 = createProxyAgent('http://example.com');
console.log('❌ FAIL - Should have thrown error but created agent');
} catch (error) {
console.log('Result: ✅ PASS - Correctly threw error');
console.log('Error message:', error.message);
}
console.log();
// Test 6: HTTPS_PROXY fallback
console.log('Test 6: HTTPS_PROXY fallback');
delete process.env.HTTP_PROXY;
process.env.HTTPS_PROXY = 'http://secure-proxy.example.com:8443';
try {
const agent6 = createProxyAgent('https://example.com');
console.log('Result:', agent6 ? '✅ PASS - ProxyAgent from HTTPS_PROXY' : '❌ FAIL - No agent created');
} catch (error) {
console.log('❌ FAIL - Error:', error.message);
}
console.log();
// Test 7: Verify fetch compatibility (mock test)
console.log('Test 7: Fetch API compatibility check');
process.env.HTTP_PROXY = 'http://proxy.example.com:8080';
try {
const agent7 = createProxyAgent('http://api.example.com/data');
// Create a mock RequestInit object like fetch would use
const requestOptions = {
method: 'GET',
dispatcher: agent7
};
console.log('Result: ✅ PASS - Can create fetch RequestInit with dispatcher');
console.log('RequestInit has dispatcher:', 'dispatcher' in requestOptions ? '✅ Yes' : '❌ No');
console.log('Dispatcher is ProxyAgent:', requestOptions.dispatcher?.constructor?.name === 'ProxyAgent' ? '✅ Yes' : '❌ No');
} catch (error) {
console.log('❌ FAIL - Error:', error.message);
}
console.log();
// Test 8: Case-insensitive environment variables
console.log('Test 8: Case-insensitive environment variables (http_proxy)');
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
process.env.http_proxy = 'http://lowercase-proxy.example.com:8080';
try {
const agent8 = createProxyAgent('http://example.com');
console.log('Result:', agent8 ? '✅ PASS - ProxyAgent from lowercase http_proxy' : '❌ FAIL - No agent created');
} catch (error) {
console.log('❌ FAIL - Error:', error.message);
}
console.log();
console.log('=== Test Summary ===');
console.log('All proxy configuration tests completed!');
console.log('\nKey findings:');
console.log('✅ Proxy agent uses Undici ProxyAgent (compatible with fetch)');
console.log('✅ Supports HTTP_PROXY, HTTPS_PROXY, http_proxy, https_proxy');
console.log('✅ Uses "dispatcher" option (correct for Node.js fetch)');
console.log('✅ Validates proxy URL format');
console.log('✅ Supports proxy authentication');
console.log('\nNote: To test with a real proxy, set HTTP_PROXY and run your application.');