Skip to content

Commit 7556b72

Browse files
committed
新增支持参数注入(随机与顺序测试)
1 parent a21358c commit 7556b72

1 file changed

Lines changed: 53 additions & 77 deletions

File tree

js/main.js

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,50 +3805,31 @@
38053805
return
38063806
}
38073807

3808-
var sql = StringUtil.trim(vInput.value)
3809-
var keys = Object.keys(header)
3810-
var keySize = keys == null ? 0 : keys.length
3811-
3812-
if (keySize > 0) {
3813-
var code = '';
3814-
for (var i = 0; i < keySize; i++) {
3815-
var k = keys[i];
3816-
code += 'var ' + k + ' = "?";\n'
3817-
header[k] = eval(header[k])
3818-
}
3819-
code += 'var sql = `' + sql.replaceAll('`', '\\`') + '`;\nsql;'
3820-
sql = eval(code)
3821-
}
3822-
3823-
// var req = this.getRequest(vInput.value, {})
3824-
var req = {
3825-
uri: url,
3826-
sql: sql,
3827-
arg: Object.values(header)
3828-
}
3829-
3830-
vOutput.value = "requesting... \nURL = " + url
3831-
this.view = 'output';
3832-
3833-
this.setBaseUrl()
3834-
this.request(isAdminOperation, this.type, this.server + "/execute", req, isAdminOperation ? {} : header, callback)
3835-
3836-
this.locals = this.locals || []
3837-
if (this.locals.length >= 1000) { //最多1000条,太多会很卡
3838-
this.locals.splice(999, this.locals.length - 999)
3839-
}
3840-
var method = this.getMethod()
3841-
this.locals.unshift({
3842-
'Document': {
3843-
'userId': this.User.id,
3844-
'name': this.formatDateTime() + ' ' + (this.urlComment || StringUtil.trim(req.tag)),
3845-
'type': this.type,
3846-
'url': '/' + method,
3847-
'request': JSON.stringify(req, null, ' '),
3848-
'header': vHeader.value
3849-
}
3808+
this.parseRandom(vInput.value, vHeader.value, -1, true, false, false, function (randomName, constConfig, constJson) {
3809+
vOutput.value = "requesting... \nURL = " + url
3810+
App.view = 'output';
3811+
var req = constJson;
3812+
3813+
App.setBaseUrl()
3814+
App.request(isAdminOperation, App.type, App.server + "/execute", req, isAdminOperation ? {} : header, callback)
3815+
3816+
App.locals = App.locals || []
3817+
if (App.locals.length >= 1000) { //最多1000条,太多会很卡
3818+
App.locals.splice(999, App.locals.length - 999)
3819+
}
3820+
var method = App.getMethod()
3821+
App.locals.unshift({
3822+
'Document': {
3823+
'userId': App.User.id,
3824+
'name': App.formatDateTime() + ' ' + (App.urlComment || StringUtil.trim(req.tag)),
3825+
'type': App.type,
3826+
'url': '/' + method,
3827+
'request': JSON.stringify(req, null, ' '),
3828+
'header': vHeader.value
3829+
}
3830+
})
3831+
App.saveCache('', 'locals', App.locals)
38503832
})
3851-
this.saveCache('', 'locals', this.locals)
38523833
},
38533834

38543835
//请求
@@ -5108,7 +5089,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
51085089
this.resetCount(this.currentRandomItem)
51095090
}
51105091

5111-
var json = this.getRequest(vInput.value, {})
5092+
var json = vInput.value; // this.getRequest(vInput.value, {})
51125093
var url = this.getUrl()
51135094
var header = this.getHeader(vHeader.value)
51145095

@@ -5148,7 +5129,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
51485129
* @param show
51495130
* @param callback
51505131
*/
5151-
testRandomSingle: function (show, testList, testSubList, item, type, url, json, header, callback) {
5132+
testRandomSingle: function (show, testList, testSubList, item, type, url, sql, header, callback) {
51525133
item = item || {}
51535134
var random = item.Random = item.Random || {}
51545135
var subs = item['[]'] || []
@@ -5165,8 +5146,8 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
51655146
const which = i;
51665147
var rawConfig = testSubList && i < existCount ? ((subs[i] || {}).Random || {}).config : random.config
51675148
this.parseRandom(
5168-
JSON.parse(JSON.stringify(json)), rawConfig, random.id
5169-
, ! testSubList, testSubList && i >= existCount, testSubList && i >= existCount
5149+
sql, rawConfig, random.id
5150+
, ! testSubList, count == 1 || (testSubList && i >= existCount), testSubList && i >= existCount
51705151
, function (randomName, constConfig, constJson) {
51715152

51725153
respCount ++;
@@ -5211,7 +5192,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
52115192
};
52125193

52135194
if (show == true) {
5214-
vInput.value = JSON.stringify(constJson, null, ' ');
5195+
vHeader.value = constConfig;
52155196
App.send(false, cb);
52165197
}
52175198
else {
@@ -5294,14 +5275,21 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
52945275
* @param show
52955276
* @param callback
52965277
*/
5297-
parseRandom: function (json, config, randomId, generateJSON, generateConfig, generateName, callback) {
5278+
parseRandom: function (sql, config, randomId, generateJSON, generateConfig, generateName, callback) {
52985279
var lines = config == null ? null : config.trim().split('\n')
52995280
if (lines == null || lines.length <= 0) {
53005281
// return null;
53015282
callback(null, null, null);
53025283
return
53035284
}
5304-
json = json || {};
5285+
5286+
sql = StringUtil.trim(sql)
5287+
5288+
var code = ''
5289+
var arg = []
5290+
// TODO 新增一个默认参数面板,自动将 SQL 里的变量生成 hint 到
5291+
5292+
// var req = this.getRequest(vInput.value, {})
53055293

53065294
baseUrl = this.getBaseUrl();
53075295

@@ -5312,7 +5300,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
53125300
var randomNameKeys = []
53135301
var constConfigLines = [] //TODO 改为 [{ "rawPath": "User/id", "replacePath": "User/id@", "replaceValue": "RANDOM_INT(1, 10)", "isExpression": true }] ?
53145302

5315-
// alert('< json = ' + JSON.stringify(json, null, ' '))
5303+
// alert('< sql = ' + JSON.stringify(json, null, ' '))
53165304

53175305
for (let i = 0; i < reqCount; i ++) {
53185306
const which = i;
@@ -5325,6 +5313,10 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
53255313
if (line.length <= 0) {
53265314
respCount ++;
53275315
if (i >= lines.length - 1 && respCount >= reqCount) {
5316+
var json = {
5317+
sql: sql,
5318+
arg: arg
5319+
}
53285320
callback(randomNameKeys.join(', '), constConfigLines.join('\n'), json);
53295321
}
53305322
continue;
@@ -5336,7 +5328,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
53365328
const bi = -1; //没必要支持,用 before: undefined, after: .. 同样支持替换,反而这样导致不兼容包含空格的 key p_k.indexOf(' ');
53375329
const path = bi < 0 ? p_k : p_k.substring(0, bi); // User/id
53385330

5339-
const pathKeys = path.split('/')
5331+
const pathKeys = [path]; // path.split('/')
53405332
if (pathKeys == null || pathKeys.length <= 0) {
53415333
throw new Error('参数注入 第 ' + (i + 1) + ' 行格式错误!\n字符 ' + path + ' 不符合 JSON 路径的格式 key0/key1/../targetKey !' +
53425334
'\n每个随机变量配置都必须按照\n key0/key1/../targetKey replaceKey: value // 注释\n的格式!' +
@@ -5399,39 +5391,23 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
53995391
if (generateJSON) {
54005392
//先按照单行简单实现
54015393
//替换 JSON 里的键值对 key: value
5402-
var parent = json;
5403-
var current = null;
5404-
for (var j = 0; j < pathKeys.length - 1; j ++) {
5405-
current = parent[pathKeys[j]]
5406-
if (current == null) {
5407-
current = parent[pathKeys[j]] = {}
5408-
}
5409-
if (parent instanceof Object == false) {
5410-
throw new Error('参数注入 第 ' + (i + 1) + ' 行格式错误!路径 ' + path + ' 中' +
5411-
' pathKeys[' + j + '] = ' + pathKeys[j] + ' 在实际请求 JSON 内对应的值不是对象 {} 或 数组 [] !');
5412-
}
5413-
parent = current;
5414-
}
5415-
5416-
if (current == null) {
5417-
current = json;
5418-
}
5419-
// alert('< current = ' + JSON.stringify(current, null, ' '))
5420-
5421-
if (key != lastKeyInPath || current.hasOwnProperty(key) == false) {
5422-
delete current[lastKeyInPath];
5423-
}
5424-
5425-
current[key] = val;
5394+
code += 'var ' + key + ' = "?";\n';
5395+
arg.push(val);
54265396
}
5427-
54285397
}
54295398
catch (e) {
54305399
throw new Error('第 ' + (which + 1) + ' 行随机配置 key: value 后的 value 不合法! \nerr: ' + e.message)
54315400
}
54325401

54335402
respCount ++;
54345403
if (respCount >= reqCount) {
5404+
code += 'var sql = `' + sql.replaceAll('`', '\\`') + '`;\nsql;'
5405+
sql = eval(code)
5406+
var json = {
5407+
sql: sql,
5408+
arg: arg // Object.values(header)
5409+
}
5410+
54355411
callback(randomNameKeys.join(', '), constConfigLines.join('\n'), json);
54365412
}
54375413
};

0 commit comments

Comments
 (0)