diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockScriptNavMenu.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockScriptNavMenu.vue index 6336ab0f00..9938531d62 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockScriptNavMenu.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockScriptNavMenu.vue @@ -107,6 +107,7 @@ export default { _parseRequestObj(data) { let requestHeaders = new Map(); let requestArguments = new Map(); + let requestRest = new Map(); let requestMethod = ""; let requestBody = ""; let requestPath = ""; @@ -115,6 +116,14 @@ export default { requestPath = request.path; requestMethod = request.method; let headers = request.headers; + let rest = request.rest; + if (rest && rest.length > 0) { + rest.forEach(r => { + if (r.enable) { + requestRest.set(r.name, r.value); + } + }) + } if (headers && headers.length > 0) { headers.forEach(header => { if (header.name) { @@ -134,7 +143,7 @@ export default { if (body.json) { requestBody = body.raw; } - return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments} + return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments, requestRest} }, apiClose() { diff --git a/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue b/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue index dc01b251ef..300f80ecbd 100644 --- a/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue +++ b/frontend/src/business/components/project/menu/function/ScriptNavMenu.vue @@ -114,6 +114,7 @@ export default { _parseRequestObj(data) { let requestHeaders = new Map(); let requestArguments = new Map(); + let requestRest = new Map(); let requestMethod = ""; let requestBody = ""; let requestPath = ""; @@ -122,12 +123,20 @@ export default { requestPath = request.path; requestMethod = request.method; let headers = request.headers; + let rest = request.rest; + if (rest && rest.length > 0) { + rest.forEach(r => { + if (r.enable) { + requestRest.set(r.name, r.value); + } + }) + } if (headers && headers.length > 0) { headers.forEach(header => { if (header.name) { requestHeaders.set(header.name, header.value); } - }) + }); } let args = request.arguments; if (args && args.length) { @@ -141,7 +150,7 @@ export default { if (body.json) { requestBody = body.raw; } - return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments} + return {requestPath, requestHeaders, requestMethod, requestBody, 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 a20b623d5f..01ed36881a 100644 --- a/frontend/src/business/components/project/menu/function/custom-function.js +++ b/frontend/src/business/components/project/menu/function/custom-function.js @@ -26,9 +26,10 @@ export function getCodeTemplate(language, requestObj) { function groovyCode(requestObj) { let {requestHeaders = new Map(), requestBody = "", requestPath = "", - requestMethod = "", host = "", protocol = "", requestArguments = new Map()} = requestObj; + requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()} = requestObj; let requestUrl = ""; requestPath = getRequestPath(requestArguments, requestPath); + requestPath = replaceRestParams(requestPath, requestRest); if (protocol && host && requestPath) { requestUrl = protocol + "://" + host + requestPath; } @@ -40,7 +41,7 @@ function groovyCode(requestObj) { function pythonCode(requestObj) { let {requestHeaders = new Map(), requestBody = "", requestPath = "/", - requestMethod = "", host = "", protocol = "http", requestArguments = new Map()} = requestObj; + requestMethod = "", host = "", protocol = "http", requestArguments = new Map(), requestRest = new Map()} = requestObj; let connType = "HTTPConnection"; if (protocol === 'https') { connType = "HTTPSConnection"; @@ -48,6 +49,7 @@ function pythonCode(requestObj) { let headers = getHeaders(requestHeaders); requestBody = requestBody ? JSON.stringify(requestBody) : "{}"; requestPath = getRequestPath(requestArguments, requestPath); + requestPath = replaceRestParams(requestPath, requestRest); let obj = {requestBody, headers, host, requestPath, requestMethod, connType}; return _pythonCodeTemplate(obj); } @@ -142,7 +144,8 @@ log.info(conn.content.text) function _beanshellTemplate(obj) { let {requestHeaders = new Map(), requestBody = "", requestPath = "/", - requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = ""} = obj; + requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj; + requestPath = replaceRestParams(requestPath, requestRest); let uri = `new URIBuilder() .setScheme("${protocol}") .setHost("${domain}") @@ -211,8 +214,9 @@ if (response.getStatusLine().getStatusCode() == 200) { function _jsTemplate(obj) { let {requestHeaders = new Map(), requestBody = "", requestPath = "/", - requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = ""} = obj; + requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj; let url = ""; + requestPath = replaceRestParams(requestPath, requestRest); if (protocol && domain && port) { url = protocol + "://" + domain + ":" + port + requestPath; } @@ -251,3 +255,25 @@ while((lines = reader.readLine()) !== null) { log.info(res); `; } + +function replaceRestParams(path, restMap) { + if (!path) { + return path; + } + let arr = path.match(/{([\w]+)}/g); + if (Array.isArray(arr) && arr.length > 0) { + arr = Array.from(new Set(arr)); + for (let str of arr) { + try { + let temp = str.substr(1); + let param = temp.substring(0, temp.length - 1); + if (str && restMap.has(param)) { + path = path.replace(new RegExp(str, 'g'), restMap.get(param)); + } + } catch (e) { + // nothing + } + } + } + return path; +}