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 requestRest = new Map();
|
||||||
let requestMethod = "";
|
let requestMethod = "";
|
||||||
let requestBody = "";
|
let requestBody = "";
|
||||||
|
let requestBodyKvs = new Map();
|
||||||
|
let bodyType = "";
|
||||||
let requestPath = "";
|
let requestPath = "";
|
||||||
let request = JSON.parse(data.request);
|
let request = JSON.parse(data.request);
|
||||||
// 拼接发送请求需要的参数
|
// 拼接发送请求需要的参数
|
||||||
|
@ -142,8 +144,16 @@ export default {
|
||||||
let body = request.body;
|
let body = request.body;
|
||||||
if (body.json) {
|
if (body.json) {
|
||||||
requestBody = body.raw;
|
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() {
|
apiClose() {
|
||||||
|
|
||||||
|
|
|
@ -11,20 +11,23 @@
|
||||||
<span class="link-type" v-if="!menu.hideScript">
|
<span class="link-type" v-if="!menu.hideScript">
|
||||||
<i class="icon el-icon-arrow-right" style="font-weight: bold; margin-right: 2px;"
|
<i class="icon el-icon-arrow-right" style="font-weight: bold; margin-right: 2px;"
|
||||||
@click="active(menu)" :class="{'is-active': menu.open}"></i>
|
@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>
|
</span>
|
||||||
|
|
||||||
<el-collapse-transition>
|
<el-collapse-transition>
|
||||||
<div v-if="menu.open">
|
<div v-if="menu.open">
|
||||||
<div v-for="(child, key) in menu.children" :key="key" class="func-div">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
</el-collapse-transition>
|
</el-collapse-transition>
|
||||||
</div>
|
</div>
|
||||||
<custom-function-relate ref="customFunctionRelate" @addCustomFuncScript="handleCodeTemplate"/>
|
<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>
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
@ -117,6 +120,8 @@ export default {
|
||||||
let requestRest = new Map();
|
let requestRest = new Map();
|
||||||
let requestMethod = "";
|
let requestMethod = "";
|
||||||
let requestBody = "";
|
let requestBody = "";
|
||||||
|
let requestBodyKvs = new Map();
|
||||||
|
let bodyType = "";
|
||||||
let requestPath = "";
|
let requestPath = "";
|
||||||
let request = JSON.parse(data.request);
|
let request = JSON.parse(data.request);
|
||||||
// 拼接发送请求需要的参数
|
// 拼接发送请求需要的参数
|
||||||
|
@ -149,8 +154,16 @@ export default {
|
||||||
let body = request.body;
|
let body = request.body;
|
||||||
if (body.json) {
|
if (body.json) {
|
||||||
requestBody = body.raw;
|
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() {
|
apiClose() {
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,10 @@ export function getCodeTemplate(language, requestObj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function groovyCode(requestObj) {
|
function groovyCode(requestObj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "",
|
let {
|
||||||
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()} = requestObj;
|
requestHeaders = new Map(), requestBody = "", requestPath = "",
|
||||||
|
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()
|
||||||
|
} = requestObj;
|
||||||
let requestUrl = "";
|
let requestUrl = "";
|
||||||
requestPath = getRequestPath(requestArguments, requestPath);
|
requestPath = getRequestPath(requestArguments, requestPath);
|
||||||
requestPath = replaceRestParams(requestPath, requestRest);
|
requestPath = replaceRestParams(requestPath, requestRest);
|
||||||
|
@ -40,8 +42,18 @@ function groovyCode(requestObj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function pythonCode(requestObj) {
|
function pythonCode(requestObj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
let {
|
||||||
requestMethod = "", host = "", protocol = "http", requestArguments = new Map(), requestRest = new Map()} = requestObj;
|
requestHeaders = new Map(),
|
||||||
|
requestBody = "",
|
||||||
|
requestPath = "/",
|
||||||
|
requestMethod = "",
|
||||||
|
host = "",
|
||||||
|
protocol = "http",
|
||||||
|
requestArguments = new Map(),
|
||||||
|
requestBodyKvs = new Map(),
|
||||||
|
bodyType,
|
||||||
|
requestRest = new Map()
|
||||||
|
} = requestObj;
|
||||||
let connType = "HTTPConnection";
|
let connType = "HTTPConnection";
|
||||||
if (protocol === 'https') {
|
if (protocol === 'https') {
|
||||||
connType = "HTTPSConnection";
|
connType = "HTTPSConnection";
|
||||||
|
@ -50,7 +62,7 @@ function pythonCode(requestObj) {
|
||||||
requestBody = requestBody ? JSON.stringify(requestBody) : "{}";
|
requestBody = requestBody ? JSON.stringify(requestBody) : "{}";
|
||||||
requestPath = getRequestPath(requestArguments, requestPath);
|
requestPath = getRequestPath(requestArguments, requestPath);
|
||||||
requestPath = replaceRestParams(requestPath, requestRest);
|
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);
|
return _pythonCodeTemplate(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,17 +98,27 @@ function getHeaders(requestHeaders) {
|
||||||
}
|
}
|
||||||
// 拼装
|
// 拼装
|
||||||
headers += `'${k}':'${v}'`;
|
headers += `'${k}':'${v}'`;
|
||||||
index ++;
|
index++;
|
||||||
}
|
}
|
||||||
headers = headers + "}"
|
headers = headers + "}"
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _pythonCodeTemplate(obj) {
|
function _pythonCodeTemplate(obj) {
|
||||||
let {requestBody, headers, host, requestPath, requestMethod, connType} = obj;
|
let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj;
|
||||||
return `import httplib
|
let reqBody = obj.requestBody;
|
||||||
params = ${requestBody} #例 {'username':'test'}
|
if (obj.bodyType !== 'json' && obj.requestBodyKvs) {
|
||||||
headers = ${headers} #例 {'Content-Type':'application/json'}
|
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}'
|
host = '${host}'
|
||||||
path = '${requestPath}'
|
path = '${requestPath}'
|
||||||
method = '${requestMethod}' # POST/GET
|
method = '${requestMethod}' # POST/GET
|
||||||
|
@ -111,7 +133,7 @@ log.info(data)
|
||||||
|
|
||||||
function _groovyCodeTemplate(obj) {
|
function _groovyCodeTemplate(obj) {
|
||||||
let {requestUrl, requestMethod, headers, body} = obj;
|
let {requestUrl, requestMethod, headers, body} = obj;
|
||||||
let params = `[
|
let params = `[
|
||||||
'url': '${requestUrl}',
|
'url': '${requestUrl}',
|
||||||
'method': '${requestMethod}', // POST/GET
|
'method': '${requestMethod}', // POST/GET
|
||||||
'headers': ${headers}, // 请求headers 例:{'Content-type':'application/json'}
|
'headers': ${headers}, // 请求headers 例:{'Content-type':'application/json'}
|
||||||
|
@ -143,8 +165,20 @@ log.info(conn.content.text)
|
||||||
}
|
}
|
||||||
|
|
||||||
function _beanshellTemplate(obj) {
|
function _beanshellTemplate(obj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
let {
|
||||||
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
|
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);
|
requestPath = replaceRestParams(requestPath, requestRest);
|
||||||
let uri = `new URIBuilder()
|
let uri = `new URIBuilder()
|
||||||
.setScheme("${protocol}")
|
.setScheme("${protocol}")
|
||||||
|
@ -158,6 +192,21 @@ function _beanshellTemplate(obj) {
|
||||||
for (let [k, v] of requestArguments) {
|
for (let [k, v] of requestArguments) {
|
||||||
uri = uri + `.setParameter("${k}", "${v}")`;
|
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) {
|
if (port) {
|
||||||
uri += `.setPort(${port}) // int类型端口
|
uri += `.setPort(${port}) // int类型端口
|
||||||
`;
|
`;
|
||||||
|
@ -170,16 +219,26 @@ function _beanshellTemplate(obj) {
|
||||||
// 设置请求头
|
// 设置请求头
|
||||||
let setHeader = "";
|
let setHeader = "";
|
||||||
for (let [k, v] of requestHeaders) {
|
for (let [k, v] of requestHeaders) {
|
||||||
setHeader = setHeader + `request.setHeader("${k}", "${v}");` +'\n';
|
setHeader = setHeader + `request.setHeader("${k}", "${v}");` + '\n';
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
requestBody = JSON.stringify(requestBody);
|
requestBody = JSON.stringify(requestBody);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
requestBody = "";
|
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;
|
return `import java.net.URI;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.*;
|
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.util.EntityUtils;
|
||||||
import org.apache.http.entity.StringEntity;
|
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对象
|
// 创建Httpclient对象
|
||||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||||
// 参数
|
// 参数
|
||||||
|
@ -213,8 +277,17 @@ if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _jsTemplate(obj) {
|
function _jsTemplate(obj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
let {
|
||||||
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
|
requestHeaders = new Map(),
|
||||||
|
requestBody = "",
|
||||||
|
requestPath = "/",
|
||||||
|
requestMethod = "GET",
|
||||||
|
protocol = "http",
|
||||||
|
requestArguments = new Map(),
|
||||||
|
domain = "",
|
||||||
|
port = "",
|
||||||
|
requestRest = new Map()
|
||||||
|
} = obj;
|
||||||
let url = "";
|
let url = "";
|
||||||
requestPath = replaceRestParams(requestPath, requestRest);
|
requestPath = replaceRestParams(requestPath, requestRest);
|
||||||
if (protocol && domain && port) {
|
if (protocol && domain && port) {
|
||||||
|
|
Loading…
Reference in New Issue