whistle.mockbubu 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/server-entry/response-handler.js +47 -2
- package/package.json +1 -1
- package/public/js/app.js +395 -95
- package/public/js/app.js.map +1 -1
|
@@ -124,7 +124,7 @@ async function matchByConditions(params) {
|
|
|
124
124
|
|
|
125
125
|
const allMatch = rule.conditions.every((cond) => {
|
|
126
126
|
const sourceParams = cond.source === 'query' ? queryParams : payloadParams
|
|
127
|
-
return
|
|
127
|
+
return matchCondition(sourceParams, cond)
|
|
128
128
|
})
|
|
129
129
|
|
|
130
130
|
if (allMatch) {
|
|
@@ -133,7 +133,7 @@ async function matchByConditions(params) {
|
|
|
133
133
|
content: rule.content ?? {},
|
|
134
134
|
ruleName: rule.name,
|
|
135
135
|
paramsSummary: rule.conditions
|
|
136
|
-
.map(c => `${c.source}.${c.key}
|
|
136
|
+
.map(c => `${c.source}.${c.key}[${c.operator || 'eq'}]${c.value ?? ''}`)
|
|
137
137
|
.join(', '),
|
|
138
138
|
}
|
|
139
139
|
}
|
|
@@ -152,6 +152,51 @@ async function matchByConditions(params) {
|
|
|
152
152
|
return { matched: false }
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
/**
|
|
156
|
+
* 单条件匹配:根据 operator 执行对应的比较
|
|
157
|
+
* operator 缺省时向后兼容旧数据(等同 eq)
|
|
158
|
+
*
|
|
159
|
+
* @param {Object} sourceParams - 请求参数 key-value 对
|
|
160
|
+
* @param {{ key: string, operator?: string, value?: string }} cond
|
|
161
|
+
* @returns {boolean}
|
|
162
|
+
*/
|
|
163
|
+
function matchCondition(sourceParams, cond) {
|
|
164
|
+
const { key, value = '' } = cond
|
|
165
|
+
const operator = cond.operator || 'eq'
|
|
166
|
+
|
|
167
|
+
// 条件不完整时跳过(视为满足),避免空值产生意外的匹配行为
|
|
168
|
+
if (!key) return true
|
|
169
|
+
const VALUELESS_OPS = new Set(['exists', 'not_exists'])
|
|
170
|
+
if (!VALUELESS_OPS.has(operator) && !value) return true
|
|
171
|
+
|
|
172
|
+
const exists = Object.prototype.hasOwnProperty.call(sourceParams, key)
|
|
173
|
+
const actual = exists ? String(sourceParams[key]) : undefined
|
|
174
|
+
const expected = String(value)
|
|
175
|
+
|
|
176
|
+
switch (operator) {
|
|
177
|
+
case 'eq':
|
|
178
|
+
return actual === expected
|
|
179
|
+
case 'neq':
|
|
180
|
+
return actual !== expected
|
|
181
|
+
case 'contains':
|
|
182
|
+
return actual !== undefined && actual.includes(expected)
|
|
183
|
+
case 'regex': {
|
|
184
|
+
if (!actual) return false
|
|
185
|
+
try {
|
|
186
|
+
return new RegExp(expected).test(actual)
|
|
187
|
+
} catch {
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
case 'exists':
|
|
192
|
+
return exists
|
|
193
|
+
case 'not_exists':
|
|
194
|
+
return !exists
|
|
195
|
+
default:
|
|
196
|
+
return actual === expected
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
155
200
|
/**
|
|
156
201
|
* 从原始请求中提取 query 参数
|
|
157
202
|
* 来源:req.originalReq.url,fallback 到 x-whistle-full-url header
|