core-functionality-test.html
18.5 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>核心功能综合测试</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
.success { color: green; }
.error { color: red; }
.info { color: blue; }
button { padding: 10px 15px; margin: 5px; cursor: pointer; }
pre { background: #f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto; }
.result { margin: 10px 0; padding: 10px; background: #f9f9f9; border-radius: 3px; }
</style>
</head>
<body>
<h1>glsk-fetch 核心功能综合测试</h1>
<div class="test-section">
<h2>1. 模块加载测试</h2>
<button onclick="testModuleLoading()">测试模块加载</button>
<div id="module-results"></div>
</div>
<div class="test-section">
<h2>2. 签名功能测试</h2>
<button onclick="testSignature()">测试签名生成和验证</button>
<div id="signature-results"></div>
</div>
<div class="test-section">
<h2>3. 请求配置测试</h2>
<button onclick="testRequestConfig()">测试请求配置</button>
<div id="config-results"></div>
</div>
<div class="test-section">
<h2>4. 错误处理测试</h2>
<button onclick="testErrorHandling()">测试错误处理</button>
<div id="error-results"></div>
</div>
<div class="test-section">
<h2>5. 小程序环境兼容性测试</h2>
<button onclick="testMiniappCompatibility()">测试小程序兼容性</button>
<div id="miniapp-results"></div>
</div>
<div class="test-section">
<h2>6. UniApp 环境兼容性测试</h2>
<button onclick="testUniappCompatibility()">测试UniApp兼容性</button>
<div id="uniapp-results"></div>
</div>
<div class="test-section">
<h2>7. 性能测试</h2>
<button onclick="testPerformance()">测试性能</button>
<div id="performance-results"></div>
</div>
<!-- 加载 UMD 格式的库 -->
<script src="../dist/index.umd.js"></script>
<script>
function logResult(containerId, message, type = 'info') {
const container = document.getElementById(containerId);
const div = document.createElement('div');
div.className = `result ${type}`;
div.innerHTML = message;
container.appendChild(div);
console.log(`[${type.toUpperCase()}] ${message}`);
}
function clearResults(containerId) {
document.getElementById(containerId).innerHTML = '';
}
// 1. 模块加载测试
function testModuleLoading() {
clearResults('module-results');
try {
// 测试全局对象
if (typeof window.GlskFetch === 'undefined') {
throw new Error('GlskFetch 全局对象不存在');
}
logResult('module-results', '✅ GlskFetch 全局对象存在', 'success');
// 测试解构赋值
const { Request, generateSignature, verifySignature } = window.GlskFetch;
logResult('module-results', '✅ 解构赋值成功', 'success');
// 测试类型
logResult('module-results', `Request 类型: ${typeof Request}`, 'info');
logResult('module-results', `generateSignature 类型: ${typeof generateSignature}`, 'info');
logResult('module-results', `verifySignature 类型: ${typeof verifySignature}`, 'info');
// 测试可用方法
const methods = Object.keys(window.GlskFetch);
logResult('module-results', `可用方法: ${methods.join(', ')}`, 'info');
} catch (error) {
logResult('module-results', `❌ 模块加载失败: ${error.message}`, 'error');
}
}
// 2. 签名功能测试
function testSignature() {
clearResults('signature-results');
try {
const { generateSignature, verifySignature } = window.GlskFetch;
// 基础签名测试
const basicOptions = {
appId: 'test-app-id',
appSecret: 'test-app-secret',
params: {
userId: 12345,
action: 'getData'
}
};
const signature1 = generateSignature(basicOptions);
logResult('signature-results', '✅ 基础签名生成成功', 'success');
logResult('signature-results', `签名: ${signature1.signature}`, 'info');
logResult('signature-results', `时间戳: ${signature1.timestamp}`, 'info');
logResult('signature-results', `随机数: ${signature1.nonce}`, 'info');
// 验证签名
const isValid1 = verifySignature({
...basicOptions,
signature: signature1.signature,
timestamp: signature1.timestamp,
nonce: signature1.nonce
});
logResult('signature-results', `✅ 签名验证结果: ${isValid1}`, isValid1 ? 'success' : 'error');
// 复杂参数签名测试
const complexOptions = {
appId: 'complex-test',
appSecret: 'complex-secret',
params: {
user: {
id: 67890,
name: 'testUser',
roles: ['admin', 'user']
},
data: {
nested: {
value: 'test',
array: [1, 2, 3]
}
},
timestamp: Date.now()
}
};
const signature2 = generateSignature(complexOptions);
logResult('signature-results', '✅ 复杂参数签名生成成功', 'success');
const isValid2 = verifySignature({
...complexOptions,
signature: signature2.signature,
timestamp: signature2.timestamp,
nonce: signature2.nonce
});
logResult('signature-results', `✅ 复杂参数签名验证结果: ${isValid2}`, isValid2 ? 'success' : 'error');
// 错误签名测试
const isInvalid = verifySignature({
...basicOptions,
signature: 'invalid-signature',
timestamp: signature1.timestamp,
nonce: signature1.nonce
});
logResult('signature-results', `✅ 错误签名验证结果: ${isInvalid} (应为false)`, !isInvalid ? 'success' : 'error');
} catch (error) {
logResult('signature-results', `❌ 签名功能测试失败: ${error.message}`, 'error');
}
}
// 3. 请求配置测试
function testRequestConfig() {
clearResults('config-results');
try {
const { Request } = window.GlskFetch;
// 基础配置测试
const basicRequest = new Request({
baseURL: 'https://api.example.com',
timeout: 5000
});
logResult('config-results', '✅ 基础配置实例化成功', 'success');
// 完整配置测试
const fullRequest = new Request({
baseURL: 'https://api.example.com',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer test-token',
'User-Agent': 'glsk-fetch-test/1.0.0'
},
retries: 3,
retryDelay: 1000
});
logResult('config-results', '✅ 完整配置实例化成功', 'success');
// 测试实例方法
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(fullRequest));
logResult('config-results', `实例方法: ${methods.join(', ')}`, 'info');
// 测试配置继承
const childRequest = new Request({
baseURL: 'https://child.api.com'
});
logResult('config-results', '✅ 配置继承测试成功', 'success');
} catch (error) {
logResult('config-results', `❌ 请求配置测试失败: ${error.message}`, 'error');
}
}
// 4. 错误处理测试
function testErrorHandling() {
clearResults('error-results');
try {
const { Request, generateSignature } = window.GlskFetch;
// 测试无效配置
try {
new Request(null);
logResult('error-results', '❌ 应该抛出错误但没有', 'error');
} catch (error) {
logResult('error-results', '✅ 无效配置正确抛出错误', 'success');
}
// 测试签名错误处理
try {
generateSignature({});
logResult('error-results', '❌ 签名参数不完整应该抛出错误', 'error');
} catch (error) {
logResult('error-results', '✅ 签名参数验证正确', 'success');
}
// 测试超时处理
const timeoutRequest = new Request({
baseURL: 'https://api.example.com',
timeout: 1
});
logResult('error-results', '✅ 超时配置设置成功', 'success');
logResult('error-results', '✅ 错误处理测试完成', 'success');
} catch (error) {
logResult('error-results', `❌ 错误处理测试失败: ${error.message}`, 'error');
}
}
// 5. 小程序环境兼容性测试
function testMiniappCompatibility() {
clearResults('miniapp-results');
try {
// 模拟小程序环境
window.wx = {
request: function(options) {
console.log('模拟 wx.request:', options);
setTimeout(() => {
if (options.success) {
options.success({
data: { message: '小程序请求成功' },
statusCode: 200
});
}
}, 100);
},
getSystemInfo: function(options) {
if (options.success) {
options.success({
platform: 'miniapp',
system: 'iOS 14.0',
version: '8.0.0'
});
}
}
};
const { Request } = window.GlskFetch;
// 小程序环境请求测试
const miniappRequest = new Request({
baseURL: 'https://api.miniapp.com',
timeout: 8000,
headers: {
'Content-Type': 'application/json'
}
});
logResult('miniapp-results', '✅ 小程序环境 Request 创建成功', 'success');
logResult('miniapp-results', '✅ wx 对象模拟成功', 'success');
// 测试小程序特定配置
wx.getSystemInfo({
success: function(res) {
logResult('miniapp-results', `✅ 系统信息获取成功: ${res.platform}`, 'success');
}
});
logResult('miniapp-results', '✅ 小程序兼容性测试完成', 'success');
} catch (error) {
logResult('miniapp-results', `❌ 小程序兼容性测试失败: ${error.message}`, 'error');
}
}
// 6. UniApp 环境兼容性测试
function testUniappCompatibility() {
clearResults('uniapp-results');
try {
// 模拟 UniApp 环境
window.uni = {
request: function(options) {
console.log('模拟 uni.request:', options);
return new Promise((resolve) => {
setTimeout(() => {
resolve({
data: { message: 'UniApp 请求成功' },
statusCode: 200
});
}, 100);
});
},
getSystemInfo: function() {
return {
platform: 'uniapp',
system: 'Android 11',
uniPlatform: 'app-plus'
};
}
};
const { Request } = window.GlskFetch;
// UniApp 环境请求测试
const uniappRequest = new Request({
baseURL: 'https://api.uniapp.com',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
'User-Agent': 'UniApp/1.0.0'
}
});
logResult('uniapp-results', '✅ UniApp 环境 Request 创建成功', 'success');
logResult('uniapp-results', '✅ uni 对象模拟成功', 'success');
// 测试 UniApp 特定功能
const systemInfo = uni.getSystemInfo();
logResult('uniapp-results', `✅ 系统信息: ${systemInfo.platform} - ${systemInfo.system}`, 'success');
// 测试异步请求
uni.request({
url: 'https://api.uniapp.com/test',
method: 'GET'
}).then(response => {
logResult('uniapp-results', '✅ UniApp 异步请求测试成功', 'success');
});
logResult('uniapp-results', '✅ UniApp 兼容性测试完成', 'success');
} catch (error) {
logResult('uniapp-results', `❌ UniApp 兼容性测试失败: ${error.message}`, 'error');
}
}
// 7. 性能测试
function testPerformance() {
clearResults('performance-results');
try {
const { Request, generateSignature } = window.GlskFetch;
// 实例化性能测试
const startTime1 = performance.now();
for (let i = 0; i < 1000; i++) {
new Request({
baseURL: 'https://api.example.com',
timeout: 5000
});
}
const endTime1 = performance.now();
logResult('performance-results', `✅ 1000次实例化耗时: ${(endTime1 - startTime1).toFixed(2)}ms`, 'success');
// 签名生成性能测试
const signatureOptions = {
appId: 'perf-test',
appSecret: 'perf-secret',
params: {
data: 'performance test data',
timestamp: Date.now()
}
};
const startTime2 = performance.now();
for (let i = 0; i < 1000; i++) {
generateSignature(signatureOptions);
}
const endTime2 = performance.now();
logResult('performance-results', `✅ 1000次签名生成耗时: ${(endTime2 - startTime2).toFixed(2)}ms`, 'success');
// 内存使用测试
const memoryBefore = performance.memory ? performance.memory.usedJSHeapSize : 0;
const requests = [];
for (let i = 0; i < 100; i++) {
requests.push(new Request({
baseURL: `https://api${i}.example.com`,
timeout: 5000
}));
}
const memoryAfter = performance.memory ? performance.memory.usedJSHeapSize : 0;
if (performance.memory) {
const memoryUsed = ((memoryAfter - memoryBefore) / 1024).toFixed(2);
logResult('performance-results', `✅ 100个实例内存使用: ${memoryUsed}KB`, 'success');
} else {
logResult('performance-results', '⚠️ 浏览器不支持内存监控', 'info');
}
logResult('performance-results', '✅ 性能测试完成', 'success');
} catch (error) {
logResult('performance-results', `❌ 性能测试失败: ${error.message}`, 'error');
}
}
// 页面加载完成后自动运行基础测试
window.addEventListener('load', function() {
console.log('=== glsk-fetch 核心功能综合测试页面加载完成 ===');
testModuleLoading();
});
</script>
</body>
</html>