fix(项目设置): 代码片段引入接口定义后生成的环境信息不全

--bug=1014112 --user=李玉号 【项目设置】自定义代码片段,引用接口定义的时候,选择mock环境,环境信息不全
https://www.tapd.cn/55049933/s/1183467
This commit is contained in:
shiziyuan9527 2022-06-16 10:44:37 +08:00 committed by f2c-ci-robot[bot]
parent 2c96843ce8
commit cba71ddc40
1 changed files with 29 additions and 9 deletions

View File

@ -26,16 +26,17 @@ export function getCodeTemplate(language, requestObj) {
function groovyCode(requestObj) { function groovyCode(requestObj) {
let { let {
requestHeaders = new Map(), requestBody = "", requestPath = "", requestHeaders = new Map(), requestBody = "", requestPath = "", domain = "", port = "",
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map(), requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map(),
requestBodyKvs = new Map(), requestBodyKvs = new Map(),
bodyType bodyType
} = requestObj; } = requestObj;
let requestUrl = ""; let requestUrl = "";
requestPath = getRequestPath(requestArguments, requestPath); requestPath = getRequestPath(requestArguments, requestPath);
requestPath = replaceRestParams(requestPath, requestRest); let path = getMockPath(domain, port, host);
requestPath = path + replaceRestParams(requestPath, requestRest);
if (protocol && host && requestPath) { if (protocol && host && requestPath) {
requestUrl = protocol + "://" + host + requestPath; requestUrl = protocol + "://" + domain + (port ? ":" + port : "") + requestPath;
} }
let body = JSON.stringify(requestBody); let body = JSON.stringify(requestBody);
@ -62,6 +63,8 @@ function pythonCode(requestObj) {
requestPath = "/", requestPath = "/",
requestMethod = "", requestMethod = "",
host = "", host = "",
domain = "",
port = "",
protocol = "http", protocol = "http",
requestArguments = new Map(), requestArguments = new Map(),
requestBodyKvs = new Map(), requestBodyKvs = new Map(),
@ -75,8 +78,9 @@ function pythonCode(requestObj) {
let headers = getHeaders(requestHeaders); let headers = getHeaders(requestHeaders);
requestBody = requestBody ? JSON.stringify(requestBody) : "{}"; requestBody = requestBody ? JSON.stringify(requestBody) : "{}";
requestPath = getRequestPath(requestArguments, requestPath); requestPath = getRequestPath(requestArguments, requestPath);
requestPath = replaceRestParams(requestPath, requestRest); let path = getMockPath(domain, port, host);
let obj = {requestBody, headers, host, requestPath, requestMethod, requestBodyKvs, bodyType, connType}; requestPath = path + replaceRestParams(requestPath, requestRest);
let obj = {requestBody, headers, requestPath, requestMethod, requestBodyKvs, bodyType, connType, domain, port};
return _pythonCodeTemplate(obj); return _pythonCodeTemplate(obj);
} }
@ -134,7 +138,7 @@ function getGroovyHeaders(requestHeaders) {
} }
function _pythonCodeTemplate(obj) { function _pythonCodeTemplate(obj) {
let {requestBody, requestBodyKvs, bodyType, headers, host, requestPath, requestMethod, connType} = obj; let {requestBody, requestBodyKvs, bodyType, headers, requestPath, requestMethod, connType, domain, port} = obj;
let reqBody = obj.requestBody; let reqBody = obj.requestBody;
if (requestMethod === 'Post' && obj.bodyType !== 'json' && obj.requestBodyKvs) { if (requestMethod === 'Post' && obj.bodyType !== 'json' && obj.requestBodyKvs) {
reqBody = 'urllib.urlencode({'; reqBody = 'urllib.urlencode({';
@ -145,6 +149,8 @@ function _pythonCodeTemplate(obj) {
reqBody += `})`; reqBody += `})`;
} }
let host = domain + (port ? ":" + port : "");
return `import httplib,urllib return `import httplib,urllib
params = ${reqBody} # {'username':'test'} params = ${reqBody} # {'username':'test'}
headers = ${headers} # {'Content-Type':'application/json'} {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} headers = ${headers} # {'Content-Type':'application/json'} {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}
@ -209,10 +215,11 @@ function _beanshellTemplate(obj) {
requestRest = new Map() requestRest = new Map()
} = obj; } = obj;
requestPath = replaceRestParams(requestPath, requestRest); let path = getMockPath(domain, port, host);
requestPath = path + replaceRestParams(requestPath, requestRest);
let uri = `new URIBuilder() let uri = `new URIBuilder()
.setScheme("${protocol}") .setScheme("${protocol}")
.setHost("${host}") .setHost("${domain}")
.setPath("${requestPath}") .setPath("${requestPath}")
`; `;
// http 请求类型 // http 请求类型
@ -323,7 +330,11 @@ function _jsTemplate(obj) {
let url = ""; let url = "";
requestPath = replaceRestParams(requestPath, requestRest); requestPath = replaceRestParams(requestPath, requestRest);
if (protocol && domain && port) { if (protocol && domain && port) {
url = protocol + "://" + host + requestPath; let path = getMockPath(domain, port, host);
requestPath = path + requestPath;
url = protocol + "://" + domain + (port ? ":" + port : "") + requestPath;
} else if (protocol && domain) {
url = protocol + "://" + domain + requestPath;
} }
url = getRequestPath(requestArguments, url); url = getRequestPath(requestArguments, url);
try { try {
@ -394,3 +405,12 @@ function replaceRestParams(path, restMap) {
} }
return path; return path;
} }
function getMockPath(domain, port, socket) {
if (domain === socket || !port) {
return "";
}
let str = domain + ":" + port;
// 获取socket之后的路径
return socket.substring(str.length);
}