From 00751da2f926065c2c2572105174aa0a031f6514 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 15 Jun 2022 17:09:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8Dapi=E5=AE=9A=E4=B9=89=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=89=87=E6=AE=B5=E6=97=B6post=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E4=B8=AD=E6=B2=A1=E6=9C=89=E6=8A=8Axml=E3=80=81raw?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=8A=A0=E5=85=A5=E8=BF=87=E6=9D=A5=E7=9A=84?= =?UTF-8?q?=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/1182989 --- .../project/menu/function/ScriptNavMenu.vue | 19 +++++++++++++-- .../project/menu/function/custom-function.js | 23 +++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue b/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue index 6e527cc350..230fbf7e70 100644 --- a/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue +++ b/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue @@ -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() { 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 f265ba24fa..73c6054d20 100644 --- a/frontend/src/business/components/project/menu/function/custom-function.js +++ b/frontend/src/business/components/project/menu/function/custom-function.js @@ -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)));`; } }