fix(项目设置): 修复自定义代码片段从api定义导入不会补充请求体参数的问题
--bug=1013992 --user=宋天阳 【接口测试】GitHub#14178,创建代码片段,从API定义导入,导入的代码没有自动填充请求体参数 https://www.tapd.cn/55049933/s/1180685
This commit is contained in:
parent
d7105773dd
commit
1361365201
|
@ -110,6 +110,8 @@ export default {
|
|||
let requestRest = new Map();
|
||||
let requestMethod = "";
|
||||
let requestBody = "";
|
||||
let requestBodyKvs = new Map();
|
||||
let bodyType = "";
|
||||
let requestPath = "";
|
||||
let request = JSON.parse(data.request);
|
||||
// 拼接发送请求需要的参数
|
||||
|
@ -142,8 +144,16 @@ export default {
|
|||
let body = request.body;
|
||||
if (body.json) {
|
||||
requestBody = body.raw;
|
||||
bodyType = "json";
|
||||
} else if (body.kvs) {
|
||||
bodyType = "kvs";
|
||||
body.kvs.forEach(arg => {
|
||||
if (arg.name) {
|
||||
requestBodyKvs.set(arg.name, arg.value);
|
||||
}
|
||||
})
|
||||
}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments, requestRest}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody, requestBodyKvs, bodyType, requestArguments, requestRest}
|
||||
},
|
||||
apiClose() {
|
||||
|
||||
|
|
|
@ -11,20 +11,23 @@
|
|||
<span class="link-type" v-if="!menu.hideScript">
|
||||
<i class="icon el-icon-arrow-right" style="font-weight: bold; margin-right: 2px;"
|
||||
@click="active(menu)" :class="{'is-active': menu.open}"></i>
|
||||
<span @click="active(menu)" class="nav-menu-title nav-font">{{menu.title}}</span>
|
||||
<span @click="active(menu)" class="nav-menu-title nav-font">{{ menu.title }}</span>
|
||||
</span>
|
||||
|
||||
<el-collapse-transition>
|
||||
<div v-if="menu.open">
|
||||
<div v-for="(child, key) in menu.children" :key="key" class="func-div">
|
||||
<el-link :disabled="child.disabled" @click="handleClick(child)" class="func-link nav-font">{{child.title}}</el-link>
|
||||
<el-link :disabled="child.disabled" @click="handleClick(child)" class="func-link nav-font">
|
||||
{{ child.title }}
|
||||
</el-link>
|
||||
</div>
|
||||
</div>
|
||||
</el-collapse-transition>
|
||||
</div>
|
||||
<custom-function-relate ref="customFunctionRelate" @addCustomFuncScript="handleCodeTemplate"/>
|
||||
<!--接口列表-->
|
||||
<api-func-relevance @save="apiSave" :is-test-plan="false" :is-script="true" @close="apiClose" ref="apiFuncRelevance"/>
|
||||
<api-func-relevance @save="apiSave" :is-test-plan="false" :is-script="true" @close="apiClose"
|
||||
ref="apiFuncRelevance"/>
|
||||
</div>
|
||||
|
||||
</template>
|
||||
|
@ -117,6 +120,8 @@ export default {
|
|||
let requestRest = new Map();
|
||||
let requestMethod = "";
|
||||
let requestBody = "";
|
||||
let requestBodyKvs = new Map();
|
||||
let bodyType = "";
|
||||
let requestPath = "";
|
||||
let request = JSON.parse(data.request);
|
||||
// 拼接发送请求需要的参数
|
||||
|
@ -149,8 +154,16 @@ export default {
|
|||
let body = request.body;
|
||||
if (body.json) {
|
||||
requestBody = body.raw;
|
||||
bodyType = "json";
|
||||
} else if (body.kvs) {
|
||||
bodyType = "kvs";
|
||||
body.kvs.forEach(arg => {
|
||||
if (arg.name) {
|
||||
requestBodyKvs.set(arg.name, arg.value);
|
||||
}
|
||||
})
|
||||
}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments, requestRest}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody, requestBodyKvs, bodyType, requestArguments, requestRest}
|
||||
},
|
||||
apiClose() {
|
||||
|
||||
|
|
|
@ -25,8 +25,10 @@ export function getCodeTemplate(language, requestObj) {
|
|||
}
|
||||
|
||||
function groovyCode(requestObj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "",
|
||||
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()} = requestObj;
|
||||
let {
|
||||
requestHeaders = new Map(), requestBody = "", requestPath = "",
|
||||
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()
|
||||
} = requestObj;
|
||||
let requestUrl = "";
|
||||
requestPath = getRequestPath(requestArguments, requestPath);
|
||||
requestPath = replaceRestParams(requestPath, requestRest);
|
||||
|
@ -40,8 +42,18 @@ function groovyCode(requestObj) {
|
|||
}
|
||||
|
||||
function pythonCode(requestObj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
||||
requestMethod = "", host = "", protocol = "http", requestArguments = new Map(), requestRest = new Map()} = requestObj;
|
||||
let {
|
||||
requestHeaders = new Map(),
|
||||
requestBody = "",
|
||||
requestPath = "/",
|
||||
requestMethod = "",
|
||||
host = "",
|
||||
protocol = "http",
|
||||
requestArguments = new Map(),
|
||||
requestBodyKvs = new Map(),
|
||||
bodyType,
|
||||
requestRest = new Map()
|
||||
} = requestObj;
|
||||
let connType = "HTTPConnection";
|
||||
if (protocol === 'https') {
|
||||
connType = "HTTPSConnection";
|
||||
|
@ -50,7 +62,7 @@ function pythonCode(requestObj) {
|
|||
requestBody = requestBody ? JSON.stringify(requestBody) : "{}";
|
||||
requestPath = getRequestPath(requestArguments, requestPath);
|
||||
requestPath = replaceRestParams(requestPath, requestRest);
|
||||
let obj = {requestBody, headers, host, requestPath, requestMethod, connType};
|
||||
let obj = {requestBody, headers, host, requestPath, requestMethod, requestBodyKvs, bodyType, connType};
|
||||
return _pythonCodeTemplate(obj);
|
||||
}
|
||||
|
||||
|
@ -86,17 +98,27 @@ function getHeaders(requestHeaders) {
|
|||
}
|
||||
// 拼装
|
||||
headers += `'${k}':'${v}'`;
|
||||
index ++;
|
||||
index++;
|
||||
}
|
||||
headers = headers + "}"
|
||||
return headers;
|
||||
}
|
||||
|
||||
function _pythonCodeTemplate(obj) {
|
||||
let {requestBody, headers, host, requestPath, requestMethod, connType} = obj;
|
||||
return `import httplib
|
||||
params = ${requestBody} #例 {'username':'test'}
|
||||
headers = ${headers} #例 {'Content-Type':'application/json'}
|
||||
let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj;
|
||||
let reqBody = obj.requestBody;
|
||||
if (obj.bodyType !== 'json' && obj.requestBodyKvs) {
|
||||
reqBody = 'urllib.urlencode({';
|
||||
// 设置post参数
|
||||
for (let [k, v] of requestBodyKvs) {
|
||||
reqBody += `\'${k}\':\'${v}\'`;
|
||||
}
|
||||
reqBody += `})`;
|
||||
}
|
||||
|
||||
return `import httplib,urllib
|
||||
params = ${reqBody} #例 {'username':'test'}
|
||||
headers = ${headers} #例 {'Content-Type':'application/json'} 或 {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}
|
||||
host = '${host}'
|
||||
path = '${requestPath}'
|
||||
method = '${requestMethod}' # POST/GET
|
||||
|
@ -111,7 +133,7 @@ log.info(data)
|
|||
|
||||
function _groovyCodeTemplate(obj) {
|
||||
let {requestUrl, requestMethod, headers, body} = obj;
|
||||
let params = `[
|
||||
let params = `[
|
||||
'url': '${requestUrl}',
|
||||
'method': '${requestMethod}', // POST/GET
|
||||
'headers': ${headers}, // 请求headers 例:{'Content-type':'application/json'}
|
||||
|
@ -143,8 +165,20 @@ log.info(conn.content.text)
|
|||
}
|
||||
|
||||
function _beanshellTemplate(obj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
||||
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
|
||||
let {
|
||||
requestHeaders = new Map(),
|
||||
requestBody = "",
|
||||
requestBodyKvs = new Map(),
|
||||
bodyType = "",
|
||||
requestPath = "/",
|
||||
requestMethod = "GET",
|
||||
protocol = "http",
|
||||
requestArguments = new Map(),
|
||||
domain = "",
|
||||
port = "",
|
||||
requestRest = new Map()
|
||||
} = obj;
|
||||
|
||||
requestPath = replaceRestParams(requestPath, requestRest);
|
||||
let uri = `new URIBuilder()
|
||||
.setScheme("${protocol}")
|
||||
|
@ -158,6 +192,21 @@ function _beanshellTemplate(obj) {
|
|||
for (let [k, v] of requestArguments) {
|
||||
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`;
|
||||
}
|
||||
if (postKvsParam !== "") {
|
||||
postKvsParam = `
|
||||
List nameValueList = new ArrayList();\r\n` + postKvsParam;
|
||||
}
|
||||
}
|
||||
|
||||
if (port) {
|
||||
uri += `.setPort(${port}) // int类型端口
|
||||
`;
|
||||
|
@ -170,16 +219,26 @@ function _beanshellTemplate(obj) {
|
|||
// 设置请求头
|
||||
let setHeader = "";
|
||||
for (let [k, v] of requestHeaders) {
|
||||
setHeader = setHeader + `request.setHeader("${k}", "${v}");` +'\n';
|
||||
setHeader = setHeader + `request.setHeader("${k}", "${v}");` + '\n';
|
||||
}
|
||||
try {
|
||||
requestBody = JSON.stringify(requestBody);
|
||||
} catch (e) {
|
||||
requestBody = "";
|
||||
}
|
||||
let postMethodCode = "";
|
||||
if (requestMethod === "POST") {
|
||||
if (bodyType === "json") {
|
||||
postMethodCode = `
|
||||
request.setEntity(new StringEntity(StringEscapeUtils.unescapeJava(payload)));
|
||||
`;
|
||||
} else {
|
||||
postMethodCode = postKvsParam + "\r\n" + `
|
||||
request.setEntity(new UrlEncodedFormEntity(nameValueList, "UTF-8"));
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
let postMethodCode = requestMethod === "POST" ?
|
||||
`request.setEntity(new StringEntity(StringEscapeUtils.unescapeJava(payload)));` : "";
|
||||
return `import java.net.URI;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.*;
|
||||
|
@ -190,6 +249,11 @@ import org.apache.http.impl.client.HttpClients;
|
|||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
|
||||
import java.util. *;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
|
||||
// 创建Httpclient对象
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
// 参数
|
||||
|
@ -213,8 +277,17 @@ if (response.getStatusLine().getStatusCode() == 200) {
|
|||
}
|
||||
|
||||
function _jsTemplate(obj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
||||
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
|
||||
let {
|
||||
requestHeaders = new Map(),
|
||||
requestBody = "",
|
||||
requestPath = "/",
|
||||
requestMethod = "GET",
|
||||
protocol = "http",
|
||||
requestArguments = new Map(),
|
||||
domain = "",
|
||||
port = "",
|
||||
requestRest = new Map()
|
||||
} = obj;
|
||||
let url = "";
|
||||
requestPath = replaceRestParams(requestPath, requestRest);
|
||||
if (protocol && domain && port) {
|
||||
|
|
Loading…
Reference in New Issue