File size: 5,574 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
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
import { describe, expect, it } from 'vitest';

import unifyProxy, { unifyProxies } from '@/utils/proxy/unify-proxy';

const emptyProxyObj = {
    protocol: undefined,
    host: undefined,
    port: undefined,
    auth: undefined,
    url_regex: '.*',
};

const effectiveExpect = ({ proxyUri, proxyObj }, expectUri, expectObj) => {
    expect(proxyUri).toBe(expectUri);
    expect(proxyObj).toEqual(expectObj);
};

describe('unify-proxy', () => {
    const nullExpect = (unified) => effectiveExpect(unified, undefined, emptyProxyObj);
    it('proxy empty', () => {
        nullExpect(unifyProxy('', emptyProxyObj));
    });
    it('proxy-uri invalid', () => {
        nullExpect(unifyProxy('http://inv lid.test', emptyProxyObj));
    });
    it('proxy-uri invalid protocol', () => {
        nullExpect(unifyProxy('ftp://rsshub.proxy', emptyProxyObj));
    });
    it('proxy-obj no host', () => {
        nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'http', port: '2333' }));
    });
    it('proxy-obj invalid host', () => {
        nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'http', host: 'inv lid.test', port: '2333' }));
    });
    it('proxy-obj invalid protocol', () => {
        nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'ftp', host: 'rsshub.proxy', port: '2333' }));
    });

    const httpNoPortUri = 'http://rsshub.proxy';
    const httpNoPortObj = { ...emptyProxyObj, protocol: 'http', host: 'rsshub.proxy' };
    const httpNoPortExpect = (unified) => effectiveExpect(unified, httpNoPortUri, httpNoPortObj);
    it('proxy-uri http no port', () => {
        httpNoPortExpect(unifyProxy(httpNoPortUri, emptyProxyObj));
    });
    it('proxy-obj http no port', () => {
        httpNoPortExpect(unifyProxy('', httpNoPortObj));
    });

    const httpUri = 'http://rsshub.proxy:2333';
    const httpObj = { ...httpNoPortObj, port: '2333' };
    const httpExpect = (unified) => effectiveExpect(unified, httpUri, httpObj);
    it('proxy-uri http', () => {
        httpExpect(unifyProxy(httpUri, emptyProxyObj));
    });
    it('proxy-obj http', () => {
        httpExpect(unifyProxy('', httpObj));
    });

    const httpsUri = 'https://rsshub.proxy:2333';
    const httpsObj = { ...httpObj, protocol: 'https' };
    const httpsExpect = (unified) => effectiveExpect(unified, httpsUri, httpsObj);
    it('proxy-uri https', () => {
        httpsExpect(unifyProxy(httpsUri, emptyProxyObj));
    });
    it('proxy-obj https', () => {
        httpsExpect(unifyProxy('', httpsObj));
    });

    const socks5Uri = 'socks5://rsshub.proxy:2333';
    const socks5Obj = { ...httpObj, protocol: 'socks5' };
    const socks5Expect = (unified) => effectiveExpect(unified, socks5Uri, socks5Obj);
    it('proxy-uri socks5', () => {
        socks5Expect(unifyProxy(socks5Uri, emptyProxyObj));
    });
    it('proxy-obj socks5', () => {
        socks5Expect(unifyProxy('', socks5Obj));
    });

    const overrideObj = { ...emptyProxyObj, protocol: 'http', host: 'over.ride', port: '6666' };
    it('proxy-uri override proxy-obj {PROTOCAL,HOST,PORT}', () => {
        socks5Expect(unifyProxy(socks5Uri, overrideObj));
    });

    const noProtocolUri = 'rsshub.proxy:2333';
    it('proxy-uri no protocol', () => {
        httpExpect(unifyProxy(noProtocolUri, emptyProxyObj));
    });

    const noProtocolObj = { ...httpObj, protocol: undefined };
    it('proxy-obj no protocol', () => {
        httpExpect(unifyProxy('', noProtocolObj));
    });

    const protocolInHostObj = { ...httpObj, host: httpNoPortUri, protocol: undefined };
    it('proxy-obj protocol in host', () => {
        httpExpect(unifyProxy('', protocolInHostObj));
    });

    const portInHostObj = { ...httpNoPortObj, host: httpUri };
    it('proxy-obj port in host', () => {
        httpExpect(unifyProxy('', portInHostObj));
    });

    const everythingInHostObj = { ...emptyProxyObj, host: httpUri };
    it('proxy-obj everything in host', () => {
        httpExpect(unifyProxy('', everythingInHostObj));
    });

    const portBothObj = { ...portInHostObj, port: '6666' };
    it('proxy-obj port in host override proxy-obj port', () => {
        httpExpect(unifyProxy('', portBothObj));
    });

    const PortNaNObj = { ...httpNoPortObj, port: 'test' };
    it('proxy-obj port NaN', () => {
        httpNoPortExpect(unifyProxy('', PortNaNObj));
    });

    const httpsAuthUri = 'https://user:pass@rsshub.proxy:2333';
    it('proxy-uri https auth', () => {
        effectiveExpect(unifyProxy(httpsAuthUri, emptyProxyObj), httpsAuthUri, httpsObj);
    });

    const httpsAuthObj = { ...httpsObj, auth: 'testtest' };
    it('proxy-obj https auth', () => {
        effectiveExpect(unifyProxy('', httpsAuthObj), httpsUri, httpsAuthObj);
    });

    const socks5AuthUri = 'socks5://user:pass@rsshub.proxy:2333';
    it('proxy-uri socks5 auth', () => {
        effectiveExpect(unifyProxy(socks5AuthUri, emptyProxyObj), socks5AuthUri, socks5Obj);
    });

    const socks5AuthObj = { ...socks5Obj, auth: 'testtest' };
    it('proxy-obj socks5 auth (invalid)', () => {
        socks5Expect(unifyProxy('', socks5AuthObj));
    });

    it('proxy-uri user@pass override proxy-obj auth', () => {
        effectiveExpect(unifyProxy(httpsAuthUri, httpsAuthObj), httpsAuthUri, httpsObj);
    });

    it('unifyProxies filters invalid proxy uris', () => {
        const results = unifyProxies(['http://rsshub.proxy:2333', 'http://inv lid.test'], emptyProxyObj);
        expect(results).toHaveLength(1);
        expect(results[0].proxyUri).toBe('http://rsshub.proxy:2333');
    });
});