File size: 9,867 Bytes
097fb32 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | /**
* 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);
|