fix(项目设置): 修复,创建代码片段,从API定义导入,Groovy、javaScript没有自动填充请求体参数的问题

--bug=1013992 --user=宋天阳
【接口测试】GitHub#14178,创建代码片段,从API定义导入,导入的代码没有自动填充请求体参数
https://www.tapd.cn/55049933/s/1181759
This commit is contained in:
song-tianyang 2022-06-14 18:31:38 +08:00 committed by f2c-ci-robot[bot]
parent e9f77455e9
commit baf6a97ef6
1 changed files with 45 additions and 2 deletions

View File

@ -27,7 +27,9 @@ export function getCodeTemplate(language, requestObj) {
function groovyCode(requestObj) { function groovyCode(requestObj) {
let { let {
requestHeaders = new Map(), requestBody = "", requestPath = "", requestHeaders = new Map(), requestBody = "", requestPath = "",
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map() requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map(),
requestBodyKvs = new Map(),
bodyType
} = requestObj; } = requestObj;
let requestUrl = ""; let requestUrl = "";
requestPath = getRequestPath(requestArguments, requestPath); requestPath = getRequestPath(requestArguments, requestPath);
@ -35,8 +37,20 @@ function groovyCode(requestObj) {
if (protocol && host && requestPath) { if (protocol && host && requestPath) {
requestUrl = protocol + "://" + host + requestPath; requestUrl = protocol + "://" + host + requestPath;
} }
let body = JSON.stringify(requestBody); let body = JSON.stringify(requestBody);
let headers = getHeaders(requestHeaders); if (requestMethod === 'POST' && bodyType === 'kvs') {
body = "\"";
for (let [k, v] of requestBodyKvs) {
if (body !== "\"") {
body += "&";
}
body += k + "=" + v;
}
body += "\"";
}
let headers = getGroovyHeaders(requestHeaders);
let obj = {requestUrl, requestMethod, headers, body}; let obj = {requestUrl, requestMethod, headers, body};
return _groovyCodeTemplate(obj); return _groovyCodeTemplate(obj);
} }
@ -104,6 +118,21 @@ function getHeaders(requestHeaders) {
return headers; return headers;
} }
function getGroovyHeaders(requestHeaders) {
let headers = "[";
let index = 1;
for (let [k, v] of requestHeaders) {
if (index !== 1) {
headers += ",";
}
// 拼装
headers += `'${k}':'${v}'`;
index++;
}
headers = headers + "]"
return headers;
}
function _pythonCodeTemplate(obj) { function _pythonCodeTemplate(obj) {
let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj; let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj;
let reqBody = obj.requestBody; let reqBody = obj.requestBody;
@ -286,6 +315,8 @@ function _jsTemplate(obj) {
requestArguments = new Map(), requestArguments = new Map(),
domain = "", domain = "",
port = "", port = "",
requestBodyKvs = new Map(),
bodyType = "",
requestRest = new Map() requestRest = new Map()
} = obj; } = obj;
let url = ""; let url = "";
@ -305,6 +336,18 @@ function _jsTemplate(obj) {
connStr += `conn.setRequestProperty("${k}","${v}");` + '\n'; connStr += `conn.setRequestProperty("${k}","${v}");` + '\n';
} }
if (requestMethod === 'POST' && bodyType === 'kvs') {
requestBody = "\"";
for (let [k, v] of requestBodyKvs) {
if (requestBody !== "\"") {
requestBody += "&";
}
requestBody += k + "=" + v;
}
requestBody += "\"";
}
return `var urlStr = "${url}"; // 请求地址 return `var urlStr = "${url}"; // 请求地址
var requestMethod = "${requestMethod}"; // 请求类型 var requestMethod = "${requestMethod}"; // 请求类型
var parameterData = ${requestBody}; // 请求参数 var parameterData = ${requestBody}; // 请求参数