File size: 3,046 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { PacProxyAgent } from 'pac-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { ProxyAgent } from 'undici';
import { afterEach, describe, expect, it, vi } from 'vitest';

const loadProxy = async (env: Record<string, string>) => {
    vi.resetModules();
    for (const [key, value] of Object.entries(env)) {
        vi.stubEnv(key, value);
    }
    return (await import('@/utils/proxy')).default;
};

describe('proxy', () => {
    afterEach(() => {
        vi.clearAllTimers();
        vi.useRealTimers();
        vi.unstubAllEnvs();
    });

    it('uses PAC proxy when PAC_URI is set', async () => {
        const proxy = await loadProxy({
            PAC_URI: 'http://example.com/proxy.pac',
            PROXY_URIS: '',
            PROXY_URI: '',
        });

        expect(proxy.agent).toBeInstanceOf(PacProxyAgent);
        expect(proxy.dispatcher).toBeNull();
        expect(proxy.proxyUri).toBe('http://example.com/proxy.pac');

        const current = proxy.getCurrentProxy();
        expect(current?.uri).toBe('http://example.com/proxy.pac');
    });

    it('handles multi-proxy selection and updates after failures', async () => {
        const proxy = await loadProxy({
            PROXY_URIS: 'http://proxy1.local:8080,http://proxy2.local:8081',
            PAC_URI: '',
            PROXY_URI: '',
        });

        expect(proxy.multiProxy).toBeDefined();
        const current = proxy.getCurrentProxy();
        expect(current).not.toBeNull();
        expect(proxy.getDispatcherForProxy(current!)).toBeInstanceOf(ProxyAgent);
        expect(proxy.getAgentForProxy({ uri: 'socks5://proxy.local:1080' } as any)).toBeInstanceOf(SocksProxyAgent);

        proxy.markProxyFailed(current!.uri);
        const next = proxy.getCurrentProxy();
        expect(next).not.toBeNull();
    });

    it('clears proxy when multi-proxy has no valid entries', async () => {
        const proxy = await loadProxy({
            PROXY_URIS: 'http://inv lid.test',
            PAC_URI: '',
            PROXY_URI: '',
        });

        expect(proxy.getCurrentProxy()).toBeNull();
        proxy.markProxyFailed('http://inv lid.test');
        expect(proxy.agent).toBeNull();
        expect(proxy.dispatcher).toBeNull();
        expect(proxy.proxyUri).toBeUndefined();
    });

    it('creates a socks proxy agent for single proxy settings', async () => {
        const proxy = await loadProxy({
            PROXY_URI: 'socks5://proxy.local:1080',
            PROXY_URIS: '',
            PAC_URI: '',
        });

        expect(proxy.agent).toBeInstanceOf(SocksProxyAgent);
        expect(proxy.dispatcher).toBeNull();
        expect(proxy.getCurrentProxy()?.uri).toBe('socks5://proxy.local:1080');
    });

    it('returns null agent for unsupported proxy protocol', async () => {
        const proxy = await loadProxy({
            PROXY_URI: '',
            PROXY_URIS: '',
            PAC_URI: '',
        });

        expect(proxy.getAgentForProxy({ uri: 'ftp://proxy.local:21' } as any)).toBeNull();
    });
});