1616
1717import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
1818
19- import { buildUpstreamProxyAgent } from '../src/proxy/routes' ;
19+ import { buildUpstreamProxyAgent , getOrCreateProxyAgent } from '../src/proxy/routes' ;
2020import * as config from '../src/config' ;
2121
2222vi . mock ( '../src/config' , async ( importOriginal ) => {
@@ -27,6 +27,42 @@ vi.mock('../src/config', async (importOriginal) => {
2727 } ;
2828} ) ;
2929
30+ describe ( 'getOrCreateProxyAgent' , ( ) => {
31+ it ( 'accepts http:// URLs' , ( ) => {
32+ expect ( ( ) => getOrCreateProxyAgent ( 'http://proxy.example.com:8080' ) ) . not . toThrow ( ) ;
33+ } ) ;
34+
35+ it ( 'accepts https:// URLs' , ( ) => {
36+ expect ( ( ) => getOrCreateProxyAgent ( 'https://proxy.example.com:8080' ) ) . not . toThrow ( ) ;
37+ } ) ;
38+
39+ it ( 'rejects socks5:// URLs with a descriptive error' , ( ) => {
40+ expect ( ( ) => getOrCreateProxyAgent ( 'socks5://proxy.example.com:1080' ) ) . toThrow (
41+ / u n s u p p o r t e d .* s c h e m e .* s o c k s 5 / i,
42+ ) ;
43+ } ) ;
44+
45+ it ( 'rejects ftp:// URLs with a descriptive error' , ( ) => {
46+ expect ( ( ) => getOrCreateProxyAgent ( 'ftp://proxy.example.com:21' ) ) . toThrow (
47+ / u n s u p p o r t e d .* s c h e m e .* f t p / i,
48+ ) ;
49+ } ) ;
50+
51+ it ( 'rejects URLs without a protocol (no scheme)' , ( ) => {
52+ expect ( ( ) => getOrCreateProxyAgent ( 'localhost:8081' ) ) . toThrow (
53+ / U n s u p p o r t e d u p s t r e a m p r o x y U R L s c h e m e / i,
54+ ) ;
55+ } ) ;
56+
57+ it ( 'rejects URLs with an empty hostname' , ( ) => {
58+ expect ( ( ) => getOrCreateProxyAgent ( 'http://:8080' ) ) . toThrow ( / i n v a l i d u p s t r e a m p r o x y u r l / i) ;
59+ } ) ;
60+
61+ it ( 'rejects completely invalid URL strings' , ( ) => {
62+ expect ( ( ) => getOrCreateProxyAgent ( 'not a url at all' ) ) . toThrow ( / i n v a l i d u p s t r e a m p r o x y u r l / i) ;
63+ } ) ;
64+ } ) ;
65+
3066describe ( 'buildUpstreamProxyAgent' , ( ) => {
3167 const originalEnv = process . env ;
3268
@@ -69,7 +105,7 @@ describe('buildUpstreamProxyAgent', () => {
69105
70106 it ( 'creates an agent when only HTTPS_PROXY is set and config is empty' , ( ) => {
71107 process . env . HTTPS_PROXY = 'http://env-proxy.example.com:8080' ;
72- vi . mocked ( config . getUpstreamProxyConfig ) . mockReturnValue ( { } ) ;
108+ vi . mocked ( config . getUpstreamProxyConfig ) . mockReturnValue ( { enabled : true } ) ;
73109
74110 const agent = buildUpstreamProxyAgent ( { host : 'github.com' , headers : { } } ) ;
75111 expect ( agent ) . toBeDefined ( ) ;
0 commit comments