Spaces:
Sleeping
Sleeping
| /** | |
| * test/unit-proxy-agent.mjs | |
| * | |
| * ๅๅ ๆต่ฏ๏ผproxy-agent ไปฃ็ๆจกๅ | |
| * ่ฟ่กๆนๅผ๏ผnode test/unit-proxy-agent.mjs | |
| * | |
| * ๆต่ฏ้ป่พๅไธบ็บฏๅ ่ๅฎ็ฐ๏ผไธไพ่ต dist ็ผ่ฏไบง็ฉใ | |
| * ้ช่ฏ๏ผ | |
| * 1. ๆ ไปฃ็ๆถ getProxyFetchOptions ่ฟๅ็ฉบๅฏน่ฑก | |
| * 2. ๆไปฃ็ๆถ่ฟๅๅซ dispatcher ็ๅฏน่ฑก | |
| * 3. ProxyAgent ็ผๅญ๏ผๅไพ๏ผ | |
| * 4. ๅ็งไปฃ็ URL ๆ ผๅผๆฏๆ | |
| */ | |
| // โโโ ๆต่ฏๆกๆถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| 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}`); | |
| } | |
| // โโโ ๅ ่ mock ๅฎ็ฐ๏ผๆจกๆ proxy-agent.ts ๆ ธๅฟ้ป่พ๏ผไธไพ่ต dist๏ผโโโโโโ | |
| // ๆจกๆ config | |
| let mockConfig = {}; | |
| function getConfig() { | |
| return mockConfig; | |
| } | |
| // ๆจกๆ ProxyAgent๏ผ่ฝป้็บง๏ผ | |
| class MockProxyAgent { | |
| constructor(url) { | |
| this.url = url; | |
| this.type = 'ProxyAgent'; | |
| } | |
| } | |
| // ๅ ่ไธ src/proxy-agent.ts ๅ้ป่พ็ๅฎ็ฐ | |
| 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 } : {}; | |
| } | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| // 1. ๆ ไปฃ็้ ็ฝฎ โ ่ฟๅ็ฉบๅฏน่ฑก | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| 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); | |
| }); | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| // 2. ๆไปฃ็้ ็ฝฎ โ ่ฟๅๅซ dispatcher ็ๅฏน่ฑก | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| 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 ็ผ็ ็็นๆฎๅญ็ฌฆ'); | |
| }); | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| // 3. ็ผๅญ๏ผๅไพ๏ผ่กไธบ | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| 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'); | |
| }); | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| // 4. fetch options ๅฑๅผ่ฏญไน้ช่ฏ | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| 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'); | |
| }); | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| // 5. config.ts ้ๆ้ช่ฏ๏ผ็ฏๅขๅ้ไผๅ ็บง๏ผ | |
| // โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log('\n๐ฆ [5] config ็ฏๅขๅ้้ๆ้ช่ฏ\n'); | |
| test('PROXY ็ฏๅขๅ้ๅบ่ฆ็ config.yaml๏ผ้ป่พ้ช่ฏ๏ผ', () => { | |
| // ๆจกๆ config.ts ็่ฆ็้ป่พ๏ผenv > yaml | |
| let config = { proxy: 'http://yaml-proxy:1234' }; | |
| const envProxy = 'http://env-proxy:5678'; | |
| // ๆจกๆ config.ts ็ฌฌ 49 ่ก้ป่พ | |
| 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); | |