| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| let passed = 0; |
| let failed = 0; |
|
|
| function test(name, fn) { |
| try { |
| fn(); |
| console.log(` โ
${name}`); |
| passed++; |
| } catch (e) { |
| console.error(` โ ${name}`); |
| console.error(` ${e.message}`); |
| failed++; |
| } |
| } |
|
|
| function assert(condition, msg) { |
| if (!condition) throw new Error(msg || 'Assertion failed'); |
| } |
|
|
| function assertEqual(a, b, msg) { |
| const as = JSON.stringify(a), bs = JSON.stringify(b); |
| if (as !== bs) throw new Error(msg || `Expected ${bs}, got ${as}`); |
| } |
|
|
| |
|
|
| |
| let mockConfig = {}; |
|
|
| function getConfig() { |
| return mockConfig; |
| } |
|
|
| |
| class MockProxyAgent { |
| constructor(url) { |
| this.url = url; |
| this.type = 'ProxyAgent'; |
| } |
| } |
|
|
| |
| let cachedAgent = undefined; |
|
|
| function resetCache() { |
| cachedAgent = undefined; |
| } |
|
|
| function getProxyDispatcher() { |
| const config = getConfig(); |
| const proxyUrl = config.proxy; |
|
|
| if (!proxyUrl) return undefined; |
|
|
| if (!cachedAgent) { |
| cachedAgent = new MockProxyAgent(proxyUrl); |
| } |
|
|
| return cachedAgent; |
| } |
|
|
| function getProxyFetchOptions() { |
| const dispatcher = getProxyDispatcher(); |
| return dispatcher ? { dispatcher } : {}; |
| } |
|
|
| |
| |
| |
| console.log('\n๐ฆ [1] ๆ ไปฃ็้
็ฝฎ\n'); |
|
|
| test('proxy ๆช่ฎพ็ฝฎๆถ่ฟๅ็ฉบๅฏน่ฑก', () => { |
| resetCache(); |
| mockConfig = {}; |
| const opts = getProxyFetchOptions(); |
| assertEqual(Object.keys(opts).length, 0, 'ๅบ่ฟๅ็ฉบๅฏน่ฑก'); |
| }); |
|
|
| test('proxy ไธบ undefined ๆถ่ฟๅ็ฉบๅฏน่ฑก', () => { |
| resetCache(); |
| mockConfig = { proxy: undefined }; |
| const opts = getProxyFetchOptions(); |
| assertEqual(Object.keys(opts).length, 0); |
| }); |
|
|
| test('proxy ไธบ็ฉบๅญ็ฌฆไธฒๆถ่ฟๅ็ฉบๅฏน่ฑก', () => { |
| resetCache(); |
| mockConfig = { proxy: '' }; |
| const opts = getProxyFetchOptions(); |
| assertEqual(Object.keys(opts).length, 0, '็ฉบๅญ็ฌฆไธฒไธๅบๅๅปบไปฃ็'); |
| }); |
|
|
| test('getProxyDispatcher ๆ ไปฃ็ๆถ่ฟๅ undefined', () => { |
| resetCache(); |
| mockConfig = {}; |
| const d = getProxyDispatcher(); |
| assertEqual(d, undefined); |
| }); |
|
|
| |
| |
| |
| console.log('\n๐ฆ [2] ๆไปฃ็้
็ฝฎ\n'); |
|
|
| test('่ฎพ็ฝฎ proxy ๅ่ฟๅๅซ dispatcher ็ๅฏน่ฑก', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const opts = getProxyFetchOptions(); |
| assert(opts.dispatcher !== undefined, 'ๅบๅ
ๅซ dispatcher'); |
| assert(opts.dispatcher instanceof MockProxyAgent, 'ๅบไธบ ProxyAgent ๅฎไพ'); |
| }); |
|
|
| test('dispatcher ๅ
ๅซๆญฃ็กฎ็ไปฃ็ URL', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const d = getProxyDispatcher(); |
| assertEqual(d.url, 'http://127.0.0.1:7890'); |
| }); |
|
|
| test('ๅธฆ่ฎค่ฏ็ไปฃ็ URL', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://user:pass@proxy.corp.com:8080' }; |
| const d = getProxyDispatcher(); |
| assertEqual(d.url, 'http://user:pass@proxy.corp.com:8080'); |
| }); |
|
|
| test('HTTPS ไปฃ็ URL', () => { |
| resetCache(); |
| mockConfig = { proxy: 'https://secure-proxy.corp.com:443' }; |
| const d = getProxyDispatcher(); |
| assertEqual(d.url, 'https://secure-proxy.corp.com:443'); |
| }); |
|
|
| test('ๅธฆ็นๆฎๅญ็ฌฆๅฏ็ ็ไปฃ็ URL', () => { |
| resetCache(); |
| const url = 'http://admin:p%40ssw0rd@proxy:8080'; |
| mockConfig = { proxy: url }; |
| const d = getProxyDispatcher(); |
| assertEqual(d.url, url, 'ๅบๅๆ ทไฟ็ URL ็ผ็ ็็นๆฎๅญ็ฌฆ'); |
| }); |
|
|
| |
| |
| |
| console.log('\n๐ฆ [3] ็ผๅญๅไพ่กไธบ\n'); |
|
|
| test('ๅคๆฌก่ฐ็จ่ฟๅๅไธ ProxyAgent ๅฎไพ', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const d1 = getProxyDispatcher(); |
| const d2 = getProxyDispatcher(); |
| assert(d1 === d2, 'ๅบ่ฟๅๅไธไธช็ผๅญๅฎไพ'); |
| }); |
|
|
| test('getProxyFetchOptions ๅคๆฌก่ฐ็จๅค็จๅไธ dispatcher', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const opts1 = getProxyFetchOptions(); |
| const opts2 = getProxyFetchOptions(); |
| assert(opts1.dispatcher === opts2.dispatcher, 'dispatcher ๅบไธบๅไธๅฎไพ'); |
| }); |
|
|
| test('้็ฝฎ็ผๅญๅๅๅปบๆฐๅฎไพ', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const d1 = getProxyDispatcher(); |
| resetCache(); |
| mockConfig = { proxy: 'http://10.0.0.1:3128' }; |
| const d2 = getProxyDispatcher(); |
| assert(d1 !== d2, '้็ฝฎๅๅบๅๅปบๆฐๅฎไพ'); |
| assertEqual(d2.url, 'http://10.0.0.1:3128'); |
| }); |
|
|
| |
| |
| |
| console.log('\n๐ฆ [4] fetch options ๅฑๅผ่ฏญไน\n'); |
|
|
| test('ๆ ไปฃ็ๆถๅฑๅผไธๅฝฑๅๅๅง options', () => { |
| resetCache(); |
| mockConfig = {}; |
| const original = { method: 'POST', headers: { 'Content-Type': 'application/json' } }; |
| const merged = { ...original, ...getProxyFetchOptions() }; |
| assertEqual(merged.method, 'POST'); |
| assertEqual(merged.headers['Content-Type'], 'application/json'); |
| assert(merged.dispatcher === undefined, 'ไธๅบๆทปๅ dispatcher'); |
| }); |
|
|
| test('ๆไปฃ็ๆถๅฑๅผๆๅ
ฅ dispatcher ไธไธ่ฆ็ๅ
ถไปๅญๆฎต', () => { |
| resetCache(); |
| mockConfig = { proxy: 'http://127.0.0.1:7890' }; |
| const original = { method: 'POST', body: '{}', signal: 'test-signal' }; |
| const merged = { ...original, ...getProxyFetchOptions() }; |
| assertEqual(merged.method, 'POST'); |
| assertEqual(merged.body, '{}'); |
| assertEqual(merged.signal, 'test-signal'); |
| assert(merged.dispatcher instanceof MockProxyAgent, 'ๅบๅ
ๅซ dispatcher'); |
| }); |
|
|
| |
| |
| |
| console.log('\n๐ฆ [5] config ็ฏๅขๅ้้ๆ้ช่ฏ\n'); |
|
|
| test('PROXY ็ฏๅขๅ้ๅบ่ฆ็ config.yaml๏ผ้ป่พ้ช่ฏ๏ผ', () => { |
| |
| let config = { proxy: 'http://yaml-proxy:1234' }; |
| const envProxy = 'http://env-proxy:5678'; |
| |
| if (envProxy) config.proxy = envProxy; |
| assertEqual(config.proxy, 'http://env-proxy:5678', 'PROXY ็ฏๅขๅ้ๅบ่ฆ็ yaml ้
็ฝฎ'); |
| }); |
|
|
| test('PROXY ็ฏๅขๅ้ๆช่ฎพ็ฝฎๆถไฟๆ yaml ๅผ๏ผ้ป่พ้ช่ฏ๏ผ', () => { |
| let config = { proxy: 'http://yaml-proxy:1234' }; |
| const envProxy = undefined; |
| if (envProxy) config.proxy = envProxy; |
| assertEqual(config.proxy, 'http://yaml-proxy:1234', 'ๅบไฟๆ yaml ้
็ฝฎไธๅ'); |
| }); |
|
|
| |
| |
| |
| console.log('\n' + 'โ'.repeat(55)); |
| console.log(` ็ปๆ: ${passed} ้่ฟ / ${failed} ๅคฑ่ดฅ / ${passed + failed} ๆป่ฎก`); |
| console.log('โ'.repeat(55) + '\n'); |
|
|
| if (failed > 0) process.exit(1); |
|
|