Skip to content

Commit b94ceb0

Browse files
committed
参数注入:新增支持 - Header: ORDER_IN('a', 'bc') 设置请求头生成规则,可以和 Request Body 等参数同时调整
1 parent 878c50d commit b94ceb0

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public String gainDBUri() {
243243
// return "jdbc:mysql://47.122.25.116:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
244244
// 以下是 MySQL 5.7 及以下
245245
return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
246+
// return "jdbc:mysql://apijson.cn:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
246247
}
247248
if (isPostgreSQL()) { // PG JDBC 必须在 URI 传 catalog
248249
return "jdbc:postgresql://localhost:5432/postgres?stringtype=unspecified"; //TODO 改成你自己的

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/StringUtil.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,30 @@ var StringUtil = {
654654
},
655655
CATEGORY_MAP: { // from TYPE_CATEGORY_KEYS
656656
// 'count': 'integer'
657+
},
658+
659+
toRandom: function (s) {
660+
return StringUtil.toForm(s, true)
661+
},
662+
toHeader: function (s) {
663+
return StringUtil.toForm(s)
664+
},
665+
toForm: function (s, quote) {
666+
if (s == null) {
667+
return '';
668+
}
669+
670+
var json = parseJSON(s);
671+
var newStr = '';
672+
for (var k in json) {
673+
var v = json[k];
674+
if (v instanceof Object || v instanceof Array) {
675+
v = JSON.stringify(v);
676+
}
677+
newStr += '\n' + k + ': ' + (quote && typeof v == 'string' ? "'" + v.replaceAll("'", "\\'") + "'" : StringUtil.get(v));
678+
}
679+
680+
return StringUtil.trim(newStr);
657681
}
658682

659683
};

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/js/main.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3398,11 +3398,11 @@ https://github.com/Tencent/APIJSON/issues
33983398
};
33993399

34003400
if (btnIndex == 1) {
3401-
// this.parseRandom(inputObj, config, null, true, true, false, callback)
3402-
callback(null, null, inputObj)
3401+
// this.parseRandom(inputObj, header, config, null, true, true, false, callback)
3402+
callback(null, null, null, inputObj)
34033403
}
34043404
else {
3405-
callback(null, null, inputObj)
3405+
callback(null, null, null, inputObj)
34063406
}
34073407

34083408
}
@@ -6496,15 +6496,15 @@ https://github.com/Tencent/APIJSON/issues
64966496

64976497
this.scripts = newDefaultScript()
64986498

6499-
this.parseRandom(loginReq, loginRandom, 0, true, false, false, function(randomName, constConfig, constJson) {
6500-
App.request(isAdminOperation, loginMethod, loginType, baseUrl + loginUrl, constJson, loginHeader, function (url, res, err) {
6499+
this.parseRandom(loginReq, loginHeader, loginRandom, 0, true, false, false, function(randomName, constConfig, constJson, constHeader) {
6500+
App.request(isAdminOperation, loginMethod, loginType, baseUrl + loginUrl, constJson, constHeader, function (url, res, err) {
65016501
if (App.isEnvCompareEnabled != true) {
6502-
loginCallback(url, res, err, null, loginMethod, loginType, loginUrl, constJson, loginHeader)
6502+
loginCallback(url, res, err, null, loginMethod, loginType, loginUrl, constJson, constHeader)
65036503
return
65046504
}
65056505

65066506
App.request(isAdminOperation, loginMethod, loginType, App.getBaseUrl(App.otherEnv) + loginUrl
6507-
, loginReq, loginHeader, function(url_, res_, err_) {
6507+
, loginReq, constHeader, function(url_, res_, err_) {
65086508
var data = res_.data
65096509
var user = data.user || data.userObj || data.userObject || data.userRsp || data.userResp || data.userBean || data.userData || data.data || data.User || data.Data
65106510
if (user != null) {
@@ -6521,7 +6521,7 @@ https://github.com/Tencent/APIJSON/issues
65216521
}
65226522

65236523
App.onResponse(url_, res_, err_);
6524-
App.onLoginResponse(isAdminOperation, req, url, res, err, loginMethod, loginType, loginUrl, constJson, loginRandom, loginHeader)
6524+
App.onLoginResponse(isAdminOperation, req, url, res, err, loginMethod, loginType, loginUrl, constJson, loginRandom, constHeader)
65256525
}, App.scripts)
65266526
})
65276527
})
@@ -7380,7 +7380,7 @@ https://github.com/Tencent/APIJSON/issues
73807380
const onHttpResponse = function (res) {
73817381
App.currentHttpResponse = res
73827382
clearTimeout(errHandler)
7383-
var postEvalResult = evalPostScript(url, res, null)
7383+
var postEvalResult = evalPostScript(method, type, url, req, header, callback, res, null)
73847384
if (postEvalResult == BREAK_ALL) {
73857385
return
73867386
}
@@ -7452,7 +7452,7 @@ https://github.com/Tencent/APIJSON/issues
74527452
var res = {status: errObj.status || (errObj.response || {}).status, request: {url: url, headers: header, data: req}, data: (errObj.response || {}).data}
74537453
App.currentHttpResponse = res
74547454

7455-
var postEvalResult = evalPostScript(url, res, err)
7455+
var postEvalResult = evalPostScript(method, type, url, req, header, callback, res, err)
74567456
if (postEvalResult == BREAK_ALL) {
74577457
return
74587458
}
@@ -7578,7 +7578,8 @@ https://github.com/Tencent/APIJSON/issues
75787578
.catch(onHttpCatch)
75797579
}
75807580

7581-
var evalScript = isAdminOperation || caseScript_ == null ? function () {} : function (isPre, code, res, err) {
7581+
var evalScript = isAdminOperation || caseScript_ == null ? function () {}
7582+
: function (isPre, code, method, type, url, req, header, callback, res, err) {
75827583
var logger = console.log
75837584
console.log = function(msg) {
75847585
logger(msg)
@@ -7667,12 +7668,13 @@ https://github.com/Tencent/APIJSON/issues
76677668

76687669
var preEvalResult = null;
76697670
if (StringUtil.isNotEmpty(preScript, true)) {
7670-
preEvalResult = evalScript(true, preScript)
7671+
preEvalResult = evalScript(true, preScript, method, type, url, req, header, callback)
76717672
}
76727673

76737674
// }
76747675

7675-
evalPostScript = isAdminOperation || caseScript_ == null ? function () {} : function (url, res, err) {
7676+
evalPostScript = isAdminOperation || caseScript_ == null ? function () {}
7677+
: function (method, type, url, req, header, callback, res, err) {
76767678
var postScript = ''
76777679

76787680
var casePostScript = StringUtil.trim((caseScript.post || {}).script)
@@ -7695,7 +7697,7 @@ https://github.com/Tencent/APIJSON/issues
76957697
postScript = preScript + '\n\n// request >>>>>>>>>>>>>>>>>>>>>>>>>> response \n\n' + postScript
76967698
}
76977699

7698-
return evalScript(false, postScript, res, err)
7700+
return evalScript(false, postScript, method, type, url, req, header, callback, res, err)
76997701
}
77007702

77017703
return null;
@@ -8015,16 +8017,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
80158017
}
80168018
else if (target == vHeader || target == vRandom) { // { "key": value } 转 key: value
80178019
try {
8018-
var json = JSON5.parse(paste);
8019-
var newStr = '';
8020-
for (var k in json) {
8021-
var v = json[k];
8022-
if (v instanceof Object || v instanceof Array) {
8023-
v = JSON.stringify(v);
8024-
}
8025-
newStr += '\n' + k + ': ' + (target != vHeader && typeof v == 'string' ? "'" + v.replaceAll("'", "\\'") + "'" : StringUtil.get(v));
8026-
}
8027-
target.value = StringUtil.trim(newStr);
8020+
target.value = StringUtil.toHeader(paste, target == vRandom);
80288021
event.preventDefault();
80298022
}
80308023
catch (e) {
@@ -10465,9 +10458,9 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1046510458

1046610459
try {
1046710460
this.parseRandom(
10468-
parseJSON(JSON.stringify(json)), rawConfig, random.id
10461+
parseJSON(JSON.stringify(json)), parseJSON(JSON.stringify(header)), rawConfig, random.id
1046910462
, ! testSubList, testSubList && i >= existCount, testSubList && i >= existCount
10470-
, function (randomName, constConfig, constJson) {
10463+
, function (randomName, constConfig, constJson, constHeader) {
1047110464

1047210465
respCount ++;
1047310466

@@ -10503,10 +10496,11 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1050310496
else {
1050410497
if (show == true) {
1050510498
vInput.value = JSON.stringify(constJson, null, ' ');
10499+
vHeader.value = StringUtil.toHeader(constHeader);
1050610500
App.send(false, cb, caseScript);
1050710501
}
1050810502
else {
10509-
App.request(false, method, type, url, constJson, header, cb, caseScript);
10503+
App.request(false, method, type, url, constJson, constHeader, cb, caseScript);
1051010504
}
1051110505
}
1051210506

@@ -10747,14 +10741,15 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1074710741
* @param show
1074810742
* @param callback
1074910743
*/
10750-
parseRandom: function (json, config, randomId, generateJSON, generateConfig, generateName, callback, preScript, ctx) {
10744+
parseRandom: function (json, head, config, randomId, generateJSON, generateConfig, generateName, callback, preScript, ctx) {
1075110745
var lines = config == null ? null : config.trim().split('\n')
1075210746
if (lines == null || lines.length <= 0) {
1075310747
// return null;
10754-
callback('', '', json);
10748+
callback('', '', json, head);
1075510749
return
1075610750
}
1075710751
json = json || {};
10752+
head = head || {};
1075810753

1075910754
baseUrl = this.getBaseUrl();
1076010755

@@ -10782,7 +10777,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1078210777
if (cn.length > 50) {
1078310778
cn = cn.substring(0, 30) + ' ..' + randomNameKeys.length + '.. ' + cn.substring(cn.length - 12)
1078410779
}
10785-
callback(cn, constConfigLines.join('\n'), json);
10780+
callback(cn, constConfigLines.join('\n'), json, head);
1078610781
}
1078710782
continue;
1078810783
}
@@ -10854,7 +10849,9 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1085410849
if (generateJSON) {
1085510850
//先按照单行简单实现
1085610851
//替换 JSON 里的键值对 key: value
10857-
var parent = json;
10852+
var isHead = key.startsWith('- ');
10853+
var targetObj = isHead ? head : json;
10854+
var parent = targetObj;
1085810855
var current = null;
1085910856
for (var j = 0; j < pathKeys.length - 1; j ++) {
1086010857
current = parent[pathKeys[j]]
@@ -10869,15 +10866,16 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1086910866
}
1087010867

1087110868
if (current == null) {
10872-
current = json;
10869+
current = targetObj;
1087310870
}
1087410871
// alert('< current = ' + JSON.stringify(current, null, ' '))
1087510872

10873+
// FIXME 还需要吗?之前的替换字段功能都废弃了,这个导致顺序变化
1087610874
if (key != lastKeyInPath || current.hasOwnProperty(key) == false) {
1087710875
delete current[lastKeyInPath];
1087810876
}
1087910877

10880-
current[key] = val;
10878+
current[isHead ? key.substring(2) : key] = val;
1088110879
}
1088210880

1088310881
}
@@ -10891,7 +10889,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1089110889
if (cn.length > 50) {
1089210890
cn = cn.substring(0, 30) + ' ..' + randomNameKeys.length + '.. ' + cn.substring(cn.length - 12)
1089310891
}
10894-
callback(cn, constConfigLines.join('\n'), json);
10892+
callback(cn, constConfigLines.join('\n'), json, head);
1089510893
}
1089610894
};
1089710895

@@ -11155,8 +11153,8 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1115511153
var method = null;
1115611154
var type = null;
1115711155
var url = null;
11158-
var req = null;
11159-
var header = null;
11156+
var req = json;
11157+
var header = head;
1116011158
var res = {};
1116111159
var data = res.data;
1116211160
var err = null;
@@ -11438,15 +11436,15 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1143811436
}
1143911437
var header = cur.header = doc.header
1144011438
//
11441-
// this.parseRandom(json, rawConfig, random.id, true, false, function (randomName, constConfig, constJson) {
11439+
// this.parseRandom(json, header, rawConfig, random.id, true, false, function (randomName, constConfig, constJson) {
1144211440
this.startTestSingle(list, allCount, index, item, isRandom, accountIndex, isCross, callback
1144311441
, function(res, allCount, list, index, response, cmp, isRandom, accountIndex, justRecoverTest, isCross) {
1144411442

1144511443
res = res || {}
1144611444
var config = res.config || {}
1144711445
var p = config.data || config.params
1144811446
try {
11449-
cur.arg = App.getRequest(config.data || config.params, {})
11447+
cur.arg = App.getRequest(p, {})
1145011448
} catch (e) {
1145111449
if (typeof p != 'string' || p.indexOf('=') <= 0) {
1145211450
throw e
@@ -11581,9 +11579,9 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1158111579
// const ctx = item.ctx = item.ctx || {}
1158211580

1158311581
var random = item.Random || {}
11584-
this.parseRandom(req, random.config, random.id, true, false, false, function(randomName, constConfig, constJson) {
11582+
this.parseRandom(req, header, random.config, random.id, true, false, false, function(randomName, constConfig, constJson, constHeader) {
1158511583
App.currentAccountIndex = accountIndex
11586-
App.request(false, method, type, isEnvCompare ? otherEnvUrl : curEnvUrl, constJson, header, function (url, res, err) {
11584+
App.request(false, method, type, isEnvCompare ? otherEnvUrl : curEnvUrl, constJson, constHeader, function (url, res, err) {
1158711585
try {
1158811586
App.onResponse(url, res, err)
1158911587
if (DEBUG) {
@@ -11609,7 +11607,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1160911607
item.TestRecord = tr
1161011608

1161111609
App.currentAccountIndex = accountIndex
11612-
App.request(false, method, type, curEnvUrl, constJson, header, function (url, res, err) {
11610+
App.request(false, method, type, curEnvUrl, constJson, constHeader, function (url, res, err) {
1161311611
try {
1161411612
App.onResponse(url, res, err)
1161511613
if (DEBUG) {

0 commit comments

Comments
 (0)