refactor(项目设置): 修复api定义导入代码片段时post请求中没有把xml、raw参数加入过来的问题

--bug=1013992 --user=宋天阳
【接口测试】GitHub#14178,创建代码片段,从API定义导入,导入的代码没有自动填充请求体参数
https://www.tapd.cn/55049933/s/1182989
This commit is contained in:
song-tianyang 2022-06-15 17:09:41 +08:00 committed by 刘瑞斌
parent 8fdf91aab0
commit 00751da2f9
2 changed files with 28 additions and 14 deletions

View File

@ -152,7 +152,13 @@ export default {
})
}
let body = request.body;
if (body.json) {
if (body.type === 'XML') {
requestBody = body.raw;
bodyType = "xml";
} else if (body.type === 'Raw') {
requestBody = body.raw;
bodyType = "raw";
} else if (body.json) {
requestBody = body.raw;
bodyType = "json";
} else if (body.kvs) {
@ -163,7 +169,16 @@ export default {
}
})
}
return {requestPath, requestHeaders, requestMethod, requestBody, requestBodyKvs, bodyType, requestArguments, requestRest}
return {
requestPath,
requestHeaders,
requestMethod,
requestBody,
requestBodyKvs,
bodyType,
requestArguments,
requestRest
}
},
apiClose() {

View File

@ -136,7 +136,7 @@ function getGroovyHeaders(requestHeaders) {
function _pythonCodeTemplate(obj) {
let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj;
let reqBody = obj.requestBody;
if (obj.bodyType !== 'json' && obj.requestBodyKvs) {
if (requestMethod === 'Post' && obj.bodyType !== 'json' && obj.requestBodyKvs) {
reqBody = 'urllib.urlencode({';
// 设置post参数
for (let [k, v] of requestBodyKvs) {
@ -222,18 +222,21 @@ function _beanshellTemplate(obj) {
for (let [k, v] of requestArguments) {
uri = uri + `.setParameter("${k}", "${v}")`;
}
if (method === "Get" && requestBodyKvs) {
for (let [k, v] of requestBodyKvs) {
uri = uri + `.setParameter("${k}", "${v}")`;
}
}
let postKvsParam = "";
if (method === 'Post') {
// 设置post参数
for (let [k, v] of requestBodyKvs) {
postKvsParam += `
nameValueList.add(new BasicNameValuePair("${k}", "${v}"));\r\n`;
postKvsParam += `nameValueList.add(new BasicNameValuePair("${k}", "${v}"));\r\n`;
}
if (postKvsParam !== "") {
postKvsParam = `
List nameValueList = new ArrayList();\r\n` + postKvsParam;
postKvsParam = `List nameValueList = new ArrayList();\r\n` + postKvsParam;
}
}
@ -258,14 +261,10 @@ function _beanshellTemplate(obj) {
}
let postMethodCode = "";
if (requestMethod === "POST") {
if (bodyType === "json") {
postMethodCode = `
request.setEntity(new StringEntity(StringEscapeUtils.unescapeJava(payload)));
`;
if (bodyType === "kvs") {
postMethodCode = postKvsParam + "\r\n" + `request.setEntity(new UrlEncodedFormEntity(nameValueList, "UTF-8"));`;
} else {
postMethodCode = postKvsParam + "\r\n" + `
request.setEntity(new UrlEncodedFormEntity(nameValueList, "UTF-8"));
`;
postMethodCode = `request.setEntity(new StringEntity(StringEscapeUtils.unescapeJava(payload)));`;
}
}