-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtest-unified-proxy-simple.js
More file actions
187 lines (162 loc) · 5.57 KB
/
test-unified-proxy-simple.js
File metadata and controls
187 lines (162 loc) · 5.57 KB
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
#!/usr/bin/env node
/**
* 简单的统一代理测试脚本
* 直接测试统一代理的核心功能,不依赖于完整的服务器启动
*/
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// 模拟Fastify实例和请求/响应对象
class MockFastify {
constructor() {
this.routes = new Map();
}
get(path, handler) {
this.routes.set(`GET:${path}`, handler);
}
post(path, handler) {
this.routes.set(`POST:${path}`, handler);
}
route(options) {
const method = options.method || 'GET';
const path = options.url || options.path;
const handler = options.handler;
this.routes.set(`${method}:${path}`, handler);
}
async inject(options) {
const key = `${options.method}:${options.url.split('?')[0]}`;
const handler = this.routes.get(key);
if (!handler) {
return {
statusCode: 404,
body: JSON.stringify({ error: 'Route not found' })
};
}
const mockRequest = {
method: options.method,
url: options.url,
query: new URLSearchParams(options.url.split('?')[1] || ''),
headers: options.headers || {},
body: options.payload
};
const mockReply = {
statusCode: 200,
headers: {},
body: null,
code(status) {
this.statusCode = status;
return this;
},
header(name, value) {
this.headers[name] = value;
return this;
},
send(data) {
this.body = data;
return this;
},
type(contentType) {
this.headers['content-type'] = contentType;
return this;
}
};
try {
await handler(mockRequest, mockReply);
return {
statusCode: mockReply.statusCode,
headers: mockReply.headers,
body: mockReply.body
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
}
}
// 导入统一代理控制器
async function loadUnifiedProxy() {
try {
const unifiedProxyPath = join(__dirname, '..', 'controllers', 'unified-proxy.js');
const fileUrl = `file:///${unifiedProxyPath.replace(/\\/g, '/')}`;
const { default: unifiedProxyController } = await import(fileUrl);
return unifiedProxyController;
} catch (error) {
console.error('❌ 无法加载统一代理控制器:', error.message);
return null;
}
}
// 测试函数
async function testUnifiedProxy() {
console.log('🚀 开始简单统一代理测试...\n');
// 加载统一代理控制器
const unifiedProxyController = await loadUnifiedProxy();
if (!unifiedProxyController) {
console.log('❌ 测试失败:无法加载统一代理控制器');
return;
}
// 创建模拟Fastify实例
const mockFastify = new MockFastify();
// 注册统一代理路由
try {
await unifiedProxyController(mockFastify, {}, () => {});
console.log('✅ 统一代理控制器加载成功');
} catch (error) {
console.error('❌ 统一代理控制器注册失败:', error.message);
return;
}
// 测试状态接口
console.log('\n📊 测试状态接口...');
try {
const statusResponse = await mockFastify.inject({
method: 'GET',
url: '/unified-proxy/status'
});
if (statusResponse.statusCode === 200) {
console.log('✅ 状态接口测试成功');
console.log('📄 响应内容:', statusResponse.body);
} else {
console.log('❌ 状态接口测试失败,状态码:', statusResponse.statusCode);
}
} catch (error) {
console.error('❌ 状态接口测试异常:', error.message);
}
// 测试代理接口(无效URL)
console.log('\n🔗 测试代理接口(无效URL)...');
try {
const proxyResponse = await mockFastify.inject({
method: 'GET',
url: '/unified-proxy/proxy?url=invalid-url&auth=drpy'
});
if (proxyResponse.statusCode >= 400) {
console.log('✅ 无效URL测试成功,正确返回错误状态码:', proxyResponse.statusCode);
} else {
console.log('❌ 无效URL测试失败,应该返回错误状态码');
}
} catch (error) {
console.error('❌ 无效URL测试异常:', error.message);
}
// 测试代理接口(缺少认证)
console.log('\n🔐 测试代理接口(缺少认证)...');
try {
const authResponse = await mockFastify.inject({
method: 'GET',
url: '/unified-proxy/proxy?url=https://example.com/test.m3u8'
});
if (authResponse.statusCode === 401) {
console.log('✅ 认证测试成功,正确返回401状态码');
} else {
console.log('❌ 认证测试失败,状态码:', authResponse.statusCode);
}
} catch (error) {
console.error('❌ 认证测试异常:', error.message);
}
console.log('\n🎉 简单统一代理测试完成!');
}
// 运行测试
testUnifiedProxy().catch(error => {
console.error('❌ 测试运行失败:', error);
process.exit(1);
});