test-defaults.js
1.41 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
// 测试 Defaults 类的功能
const { Defaults } = require('./dist/index');
// 创建默认配置实例
const defaultConfig = new Defaults();
console.log('默认配置:', defaultConfig);
// 创建自定义配置实例
const customConfig = new Defaults({
baseURL: 'https://api.example.com',
timeout: 10000,
headers: {
'Authorization': 'Bearer token123',
'X-Custom-Header': 'CustomValue'
},
appId: 'test-app-id',
appSecret: 'test-app-secret',
retries: 3,
customField: 'customValue' // 测试自定义字段
});
console.log('自定义配置:', customConfig);
// 测试合并配置
const mergedConfig = customConfig.merge({
timeout: 15000,
headers: {
'X-Another-Header': 'AnotherValue'
},
params: {
page: 1,
limit: 10
}
});
console.log('合并后的配置:', mergedConfig);
// 验证合并是否正确保留了原始值和新值
console.log('验证合并结果:');
console.log('baseURL 保持不变:', mergedConfig.baseURL === customConfig.baseURL);
console.log('timeout 已更新:', mergedConfig.timeout === 15000);
console.log('原始 headers 保留:', mergedConfig.headers['Authorization'] === 'Bearer token123');
console.log('新 headers 添加:', mergedConfig.headers['X-Another-Header'] === 'AnotherValue');
console.log('params 已添加:', mergedConfig.params.page === 1 && mergedConfig.params.limit === 10);
console.log('自定义字段保留:', mergedConfig.customField === 'customValue');