whitelist-simple-test.html
5.17 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>白名单URL简单测试</title>
<script src="../dist/index.umd.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
.card {
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
pre {
background-color: #f5f5f5;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
margin-bottom: 10px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>白名单URL简单测试</h1>
<div class="container">
<div class="card">
<h2>测试说明</h2>
<p>本页面测试在请求前检查method参数是否在白名单中,如果在白名单中,则在URL中添加sdctag=true参数并参与签名。</p>
<button id="testWhitelist">测试GET请求</button>
<button id="testNonWhitelist">测试POST请求</button>
<h3>结果:</h3>
<pre id="result">点击按钮开始测试...</pre>
</div>
</div>
<script>
// 等待页面加载完成
document.addEventListener('DOMContentLoaded', function () {
// 获取DOM元素
const resultElement = document.getElementById('result');
const testWhitelistButton = document.getElementById('testWhitelist');
const testNonWhitelistButton = document.getElementById('testNonWhitelist');
// 检查GlskFetch对象
console.log('GlskFetch:', window.GlskFetch);
// 获取Request构造函数
let RequestConstructor = null;
if (typeof window.GlskFetch === 'function') {
// 如果GlskFetch本身是构造函数
RequestConstructor = window.GlskFetch;
} else if (window.GlskFetch && typeof window.GlskFetch.default === 'function') {
// 如果GlskFetch.default是构造函数
RequestConstructor = window.GlskFetch.default;
}
if (!RequestConstructor) {
console.error('Request构造函数不存在');
resultElement.textContent = 'SDK加载失败:Request构造函数不存在';
return;
}
// 注意:Trace对象在UMD模块中可能不可直接访问
// 但Request类内部已经正确引入并使用了Trace对象
// 所以白名单功能应该能正常工作
console.log('SDK内部已集成Trace模块,将从服务器获取白名单URL');
// 创建请求实例
const request = new RequestConstructor({
baseURL: 'http://192.168.1.117:9898/facade',
appId: 'dj_zhixin',
appSecret: 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b',
sentryDsn: 'https://be4fb79a912b4c34a5d1c539a1618978@sentry.glsx.com.cn/1',
});
// 测试白名单内请求
testWhitelistButton.addEventListener('click', async function () {
resultElement.textContent = '请求中...';
try {
// 白名单URL列表由SDK内部处理
// 我们只需要在请求参数中添加method参数
console.log('发送请求,SDK将自动检查白名单并添加sdctag参数');
// 使用params包裹GET请求参数
const result = await request.get('/dict/sign/test', {
params:{
"id":6,
"objs": ["hehe", "haah"],
"dogs":[{"name":"haogou","age":1},{"name":"拉布拉多","age":2}],
"dogs":{"name":"haogou","age":1}
}
});
resultElement.textContent =
`请求成功:\n请求参数: ${JSON.stringify(result.config.data, null, 2)}\n\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
} catch (error) {
resultElement.textContent = `请求失败: ${error.message}`;
}
});
// 测试白名单外请求
testNonWhitelistButton.addEventListener('click', async function () {
resultElement.textContent = '请求中...';
try {
// 白名单URL列表由SDK内部处理
// 我们只需要在请求参数中添加method参数
console.log('发送请求,SDK将自动检查白名单并添加sdctag参数');
// POST请求使用data传递参数(放在请求体中)
const result = await request.post('/dict/sign/test1', {
"id": 6,
"objs": ["hehe", "haah"],
"dogs": [{"name": "haogou", "age": 1}, {"name": "拉布拉多", "age": 2}],
"dog": {"name": "haogou", "age": 1}
});
resultElement.textContent =
`请求成功:\n请求参数: ${JSON.stringify(result.config.params, null, 2)}\n\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
} catch (error) {
resultElement.textContent = `请求失败: ${error.message}`;
}
});
});
</script>
</body>
</html>