From baf6a97ef6daddb3615aecd101c4a3b5a1f5e5d5 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 14 Jun 2022 18:31:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=8C=E5=88=9B=E5=BB=BA=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=89=87=E6=AE=B5=EF=BC=8C=E4=BB=8EAPI=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=EF=BC=8CGroovy=E3=80=81javaScript=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E5=8F=82=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013992 --user=宋天阳 【接口测试】GitHub#14178,创建代码片段,从API定义导入,导入的代码没有自动填充请求体参数 https://www.tapd.cn/55049933/s/1181759 --- .../project/menu/function/custom-function.js | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/frontend/src/business/components/project/menu/function/custom-function.js b/frontend/src/business/components/project/menu/function/custom-function.js index e3f271d1de..f845d565ac 100644 --- a/frontend/src/business/components/project/menu/function/custom-function.js +++ b/frontend/src/business/components/project/menu/function/custom-function.js @@ -27,7 +27,9 @@ export function getCodeTemplate(language, requestObj) { function groovyCode(requestObj) { let { 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; let requestUrl = ""; requestPath = getRequestPath(requestArguments, requestPath); @@ -35,8 +37,20 @@ function groovyCode(requestObj) { if (protocol && host && requestPath) { requestUrl = protocol + "://" + host + requestPath; } + 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}; return _groovyCodeTemplate(obj); } @@ -104,6 +118,21 @@ function getHeaders(requestHeaders) { 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) { let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj; let reqBody = obj.requestBody; @@ -286,6 +315,8 @@ function _jsTemplate(obj) { requestArguments = new Map(), domain = "", port = "", + requestBodyKvs = new Map(), + bodyType = "", requestRest = new Map() } = obj; let url = ""; @@ -305,6 +336,18 @@ function _jsTemplate(obj) { 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}"; // 请求地址 var requestMethod = "${requestMethod}"; // 请求类型 var parameterData = ${requestBody}; // 请求参数