1+ import http from 'http' ;
2+
3+ // 测试配置
4+ const config = {
5+ proxyHost : 'localhost' ,
6+ proxyPort : 3001 ,
7+ authCode : 'drpys' ,
8+ wrongAuthCode : 'wrong_auth' ,
9+ testUrl : 'https://httpbin.org/json'
10+ } ;
11+
12+ // 发送 HTTP 请求的辅助函数
13+ function makeRequest ( options ) {
14+ return new Promise ( ( resolve , reject ) => {
15+ const req = http . request ( options , ( res ) => {
16+ let data = '' ;
17+ res . on ( 'data' , chunk => data += chunk ) ;
18+ res . on ( 'end' , ( ) => {
19+ resolve ( {
20+ statusCode : res . statusCode ,
21+ headers : res . headers ,
22+ data : data
23+ } ) ;
24+ } ) ;
25+ } ) ;
26+
27+ req . on ( 'error' , reject ) ;
28+ req . end ( ) ;
29+ } ) ;
30+ }
31+
32+ // 测试 1: 无身份验证参数
33+ async function testNoAuth ( ) {
34+ console . log ( '\n=== 测试 1: 无身份验证参数 ===' ) ;
35+
36+ try {
37+ const options = {
38+ hostname : config . proxyHost ,
39+ port : config . proxyPort ,
40+ path : `/file-proxy/proxy?url=${ encodeURIComponent ( config . testUrl ) } ` ,
41+ method : 'GET'
42+ } ;
43+
44+ console . log ( `请求路径: ${ options . path } ` ) ;
45+ const response = await makeRequest ( options ) ;
46+
47+ console . log ( `状态码: ${ response . statusCode } ` ) ;
48+ console . log ( `响应数据: ${ response . data . substring ( 0 , 200 ) } ` ) ;
49+
50+ if ( response . statusCode === 401 ) {
51+ console . log ( '✅ 无身份验证参数测试通过 - 正确返回 401' ) ;
52+ } else {
53+ console . log ( '❌ 无身份验证参数测试失败 - 应该返回 401' ) ;
54+ }
55+ } catch ( error ) {
56+ console . log ( `❌ 无身份验证参数测试出错: ${ error . message } ` ) ;
57+ }
58+ }
59+
60+ // 测试 2: 错误的身份验证参数
61+ async function testWrongAuth ( ) {
62+ console . log ( '\n=== 测试 2: 错误的身份验证参数 ===' ) ;
63+
64+ try {
65+ const options = {
66+ hostname : config . proxyHost ,
67+ port : config . proxyPort ,
68+ path : `/file-proxy/proxy?url=${ encodeURIComponent ( config . testUrl ) } &auth=${ config . wrongAuthCode } ` ,
69+ method : 'GET'
70+ } ;
71+
72+ console . log ( `请求路径: ${ options . path } ` ) ;
73+ const response = await makeRequest ( options ) ;
74+
75+ console . log ( `状态码: ${ response . statusCode } ` ) ;
76+ console . log ( `响应数据: ${ response . data . substring ( 0 , 200 ) } ` ) ;
77+
78+ if ( response . statusCode === 401 ) {
79+ console . log ( '✅ 错误身份验证参数测试通过 - 正确返回 401' ) ;
80+ } else {
81+ console . log ( '❌ 错误身份验证参数测试失败 - 应该返回 401' ) ;
82+ }
83+ } catch ( error ) {
84+ console . log ( `❌ 错误身份验证参数测试出错: ${ error . message } ` ) ;
85+ }
86+ }
87+
88+ // 测试 3: 正确的身份验证参数
89+ async function testCorrectAuth ( ) {
90+ console . log ( '\n=== 测试 3: 正确的身份验证参数 ===' ) ;
91+
92+ try {
93+ const options = {
94+ hostname : config . proxyHost ,
95+ port : config . proxyPort ,
96+ path : `/file-proxy/proxy?url=${ encodeURIComponent ( config . testUrl ) } &auth=${ config . authCode } ` ,
97+ method : 'GET'
98+ } ;
99+
100+ console . log ( `请求路径: ${ options . path } ` ) ;
101+ const response = await makeRequest ( options ) ;
102+
103+ console . log ( `状态码: ${ response . statusCode } ` ) ;
104+ console . log ( `响应数据: ${ response . data . substring ( 0 , 200 ) } ...` ) ;
105+
106+ if ( response . statusCode === 200 ) {
107+ console . log ( '✅ 正确身份验证参数测试通过 - 正确返回 200' ) ;
108+ } else {
109+ console . log ( '❌ 正确身份验证参数测试失败 - 应该返回 200' ) ;
110+ }
111+ } catch ( error ) {
112+ console . log ( `❌ 正确身份验证参数测试出错: ${ error . message } ` ) ;
113+ }
114+ }
115+
116+ // 测试 4: info 接口身份验证
117+ async function testInfoAuth ( ) {
118+ console . log ( '\n=== 测试 4: info 接口身份验证 ===' ) ;
119+
120+ try {
121+ // 无 auth 参数
122+ const optionsNoAuth = {
123+ hostname : config . proxyHost ,
124+ port : config . proxyPort ,
125+ path : `/file-proxy/info?url=${ encodeURIComponent ( config . testUrl ) } ` ,
126+ method : 'GET'
127+ } ;
128+
129+ console . log ( `无 auth 请求路径: ${ optionsNoAuth . path } ` ) ;
130+ const responseNoAuth = await makeRequest ( optionsNoAuth ) ;
131+ console . log ( `无 auth 状态码: ${ responseNoAuth . statusCode } ` ) ;
132+
133+ // 有 auth 参数
134+ const optionsWithAuth = {
135+ hostname : config . proxyHost ,
136+ port : config . proxyPort ,
137+ path : `/file-proxy/info?url=${ encodeURIComponent ( config . testUrl ) } &auth=${ config . authCode } ` ,
138+ method : 'GET'
139+ } ;
140+
141+ console . log ( `有 auth 请求路径: ${ optionsWithAuth . path } ` ) ;
142+ const responseWithAuth = await makeRequest ( optionsWithAuth ) ;
143+ console . log ( `有 auth 状态码: ${ responseWithAuth . statusCode } ` ) ;
144+
145+ if ( responseNoAuth . statusCode === 401 && responseWithAuth . statusCode === 200 ) {
146+ console . log ( '✅ info 接口身份验证测试通过' ) ;
147+ } else {
148+ console . log ( '❌ info 接口身份验证测试失败' ) ;
149+ }
150+ } catch ( error ) {
151+ console . log ( `❌ info 接口身份验证测试出错: ${ error . message } ` ) ;
152+ }
153+ }
154+
155+ // 测试 5: cache 接口身份验证
156+ async function testCacheAuth ( ) {
157+ console . log ( '\n=== 测试 5: cache 接口身份验证 ===' ) ;
158+
159+ try {
160+ // 无 auth 参数
161+ const optionsNoAuth = {
162+ hostname : config . proxyHost ,
163+ port : config . proxyPort ,
164+ path : `/file-proxy/cache` ,
165+ method : 'DELETE'
166+ } ;
167+
168+ console . log ( `无 auth 请求路径: ${ optionsNoAuth . path } ` ) ;
169+ const responseNoAuth = await makeRequest ( optionsNoAuth ) ;
170+ console . log ( `无 auth 状态码: ${ responseNoAuth . statusCode } ` ) ;
171+
172+ // 有 auth 参数
173+ const optionsWithAuth = {
174+ hostname : config . proxyHost ,
175+ port : config . proxyPort ,
176+ path : `/file-proxy/cache?auth=${ config . authCode } ` ,
177+ method : 'DELETE'
178+ } ;
179+
180+ console . log ( `有 auth 请求路径: ${ optionsWithAuth . path } ` ) ;
181+ const responseWithAuth = await makeRequest ( optionsWithAuth ) ;
182+ console . log ( `有 auth 状态码: ${ responseWithAuth . statusCode } ` ) ;
183+
184+ if ( responseNoAuth . statusCode === 401 && responseWithAuth . statusCode === 200 ) {
185+ console . log ( '✅ cache 接口身份验证测试通过' ) ;
186+ } else {
187+ console . log ( '❌ cache 接口身份验证测试失败' ) ;
188+ }
189+ } catch ( error ) {
190+ console . log ( `❌ cache 接口身份验证测试出错: ${ error . message } ` ) ;
191+ }
192+ }
193+
194+ // 运行所有身份验证测试
195+ async function runAuthTests ( ) {
196+ console . log ( '开始运行文件代理身份验证测试...' ) ;
197+
198+ await testNoAuth ( ) ;
199+ await testWrongAuth ( ) ;
200+ await testCorrectAuth ( ) ;
201+ await testInfoAuth ( ) ;
202+ await testCacheAuth ( ) ;
203+
204+ console . log ( '\n身份验证测试完成!' ) ;
205+ }
206+
207+ runAuthTests ( ) . catch ( console . error ) ;
208+
209+ export {
210+ runAuthTests ,
211+ testNoAuth ,
212+ testWrongAuth ,
213+ testCorrectAuth ,
214+ testInfoAuth ,
215+ testCacheAuth
216+ } ;
0 commit comments