fix(Mock自定义脚本): 修复Mock的样式问题

修复Mock的样式问题
This commit is contained in:
song-tianyang 2021-10-21 14:31:14 +08:00 committed by BugKing
parent 2bb5cbdc93
commit 9c079688f2
6 changed files with 91 additions and 55 deletions

View File

@ -351,10 +351,13 @@ public class MockApiUtils {
String script = scriptObj.getString("script");
String scriptLanguage =scriptObj.getString("scriptLanguage");
returnStr = parseScript(script,url,headerMap,requestMockParams);
Map<String,String> scrpitParseMap = parseScript(script,url,headerMap,requestMockParams);
if(scrpitParseMap.containsKey("returnMsg")){
returnStr = scrpitParseMap.get("returnMsg");
}
if(scrpitParseMap.containsKey("script")){
script = scrpitParseMap.get("script");
}
runScript(script,scriptLanguage);
}
}
@ -364,54 +367,65 @@ public class MockApiUtils {
}
}
private static String parseScript(String script,String url,Map<String,String> headerMap,RequestMockParams requestMockParams) {
private static Map<String,String> parseScript(String script,String url,Map<String,String> headerMap,RequestMockParams requestMockParams) {
Map<String,String> returnMap = new HashMap<>();
String returnMsg = "";
String newScript = "";
if(StringUtils.isNotEmpty(script)){
String [] scriptRowArr = StringUtils.split(script,"\n");
for (String scriptRow : scriptRowArr) {
scriptRow = scriptRow.trim();
if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){
scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim();
if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){
returnMsg += url;
}else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(10,scriptRow.length()-2);
if(headerMap.containsKey(paramName)){
returnMsg += headerMap.get(paramName);
}
}else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey(paramName)){
returnMsg += String.valueOf(bodyParamObj.get(paramName));
for (String scriptItemRows : scriptRowArr) {
String [] scriptItemArr = scriptItemRows.split(";");
for (String scriptRow :scriptItemArr) {
scriptRow = scriptRow.trim();
if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){
scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim();
if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){
returnMsg += url;
}else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(10,scriptRow.length()-2);
if(headerMap.containsKey(paramName)){
returnMsg += headerMap.get(paramName);
}
}
}else if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey("raw")){
returnMsg += String.valueOf(bodyParamObj.get("raw"));
}else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey(paramName)){
returnMsg += String.valueOf(bodyParamObj.get(paramName));
}
}
}
}else if(StringUtils.startsWith(scriptRow,"@query(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(9,scriptRow.length()-2);
if(requestMockParams.getQueryParamsObj() != null && requestMockParams.getQueryParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getQueryParamsObj().get(paramName));
}
}else if(StringUtils.startsWith(scriptRow,"@rest(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getRestParamsObj() != null && requestMockParams.getRestParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getRestParamsObj().get(paramName));
}else if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey("raw")){
returnMsg += String.valueOf(bodyParamObj.get("raw"));
}
}
}else if(StringUtils.startsWith(scriptRow,"@query(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(9,scriptRow.length()-2);
if(requestMockParams.getQueryParamsObj() != null && requestMockParams.getQueryParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getQueryParamsObj().get(paramName));
}
}else if(StringUtils.startsWith(scriptRow,"@rest(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getRestParamsObj() != null && requestMockParams.getRestParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getRestParamsObj().get(paramName));
}
}else {
returnMsg += scriptRow;
}
}else {
returnMsg += scriptRow;
newScript += scriptRow +";";
}
}
if(StringUtils.isNotEmpty(newScript)){
newScript += "\n";
}
}
}
return returnMsg;
returnMap.put("script",newScript);
returnMap.put("returnMsg",returnMsg);
return returnMap;
}
private static void runScript(String script, String scriptLanguage) {

View File

@ -651,7 +651,11 @@ public class MockConfigService {
returnStr = MockApiUtils.getResultByResponseResult(responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams);
}
if (responseJsonObj.containsKey("httpCode")) {
response.setStatus(Integer.parseInt(responseJsonObj.getString("httpCode")));
int httpCodeNum = 500;
try {
httpCodeNum = Integer.parseInt(responseJsonObj.getString("httpCode"));
}catch (Exception e){}
response.setStatus(httpCodeNum);
}
if (responseJsonObj.containsKey("delayed")) {
try {

View File

@ -1,6 +1,6 @@
<template>
<div>
<el-row type="flex" :gutter="10">
<el-row type="flex">
<el-col :span="codeSpan" class="script-content">
<ms-code-edit v-if="isCodeEditAlive" :mode="codeEditModeMap[jsr223ProcessorData.scriptLanguage]"
:read-only="isReadOnly"
@ -15,10 +15,10 @@
@click="switchMenu"></i>
</div>
</div>
<el-col :span="menuSpan" class="script-index">
<el-col :span="menuSpan" style="width: 200px" class="script-index">
<ms-dropdown :default-command="jsr223ProcessorData.scriptLanguage" :commands="languages" style="margin-bottom: 5px;margin-left: 15px;"
@command="languageChange"/>
<mock-script-nav-menu ref="scriptNavMenu" :language="jsr223ProcessorData.scriptLanguage" :menus="baseCodeTemplates"
<mock-script-nav-menu ref="scriptNavMenu" style="width: 90%" :language="jsr223ProcessorData.scriptLanguage" :menus="baseCodeTemplates"
@handleCode="handleCodeTemplate"/>
</el-col>
</el-row>
@ -46,27 +46,27 @@ export default {
children: [
{
title: this.$t('api_test.request.address'),
value: '\nreturnMsg.add(@address)\n',
value: 'returnMsg.add(@address);',
},
{
title: "Header "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@header(${param}))\n',
value: 'returnMsg.add(@header(${param}));',
},
{
title: this.$t('api_test.request.body')+this.$t('api_test.variable'),
value: '\nreturnMsg.add(@body(${param}))\n',
value: 'returnMsg.add(@body(${param}));',
},
{
title: this.$t('api_test.request.body')+this.$t('api_test.variable')+" (Raw)",
value: '\nreturnMsg.add(@bodyRaw)\n',
value: 'returnMsg.add(@bodyRaw);',
},
{
title: "Query "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@query(${param}))\n',
value: 'returnMsg.add(@query(${param}));',
},
{
title: "Rest "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@rest(${param}))\n',
value: 'returnMsg.add(@rest(${param}));',
},
]

View File

@ -5,7 +5,7 @@
<el-collapse-transition>
<el-tabs v-model="activeName" v-show="isActive" style="margin: 20px">
<el-tab-pane v-if="!isTcp" :label="$t('api_test.definition.request.response_header')" name="headers" class="pane">
<ms-api-key-value :isShowEnable="false" :suggestions="headerSuggestions" :items="response.headers"/>
<ms-api-key-value style="width: 95%" :isShowEnable="false" :suggestions="headerSuggestions" :items="response.headers"/>
</el-tab-pane>
<el-tab-pane :label="$t('api_test.definition.request.response_body')" name="body" class="pane">
<mock-api-response-body :isReadOnly="false" :isShowEnable="false" :api-id="apiId" :body="response.body" :headers="response.headers"/>
@ -15,7 +15,7 @@
<el-row>
<el-col :span="2"/>
<el-col :span="20">
<el-input class="ms-http-input" size="small" v-model="response.httpCode"/>
<el-input size="small" style="width: 180px;margin-top: 10px" v-model="response.httpCode"/>
</el-col>
<el-col :span="2"/>
</el-row>
@ -105,6 +105,12 @@ export default {
if (!this.response || !this.response || !this.response.headers) {
return;
}
if(!this.response.httpCode || this.response.httpCode === ''){
this.$set(this.response,"httpCode","200");
}
if(!this.response.delayed){
this.response.delayed = 0;
}
if (Object.prototype.toString.call(this.response).match(/\[object (\w+)\]/)[1].toLowerCase() !== 'object') {
this.response = JSON.parse(this.response);
}

View File

@ -100,10 +100,10 @@ export default {
},
response: {
httpCode: "",
delayed: "",
httpHeads: [],
body: "",
responseResult:{
delayed: 0,
headers:[],
arguments:[],
rest:[],
@ -234,6 +234,18 @@ export default {
uploadMockExpectConfig(clearForm) {
let url = "/mockConfig/updateMockExpectConfig";
let param = this.mockExpectConfig;
if(!param.name || param.name === ''){
this.$error(this.$t('test_track.case.input_name'));
return false;
}else if(param.name.length > 100){
this.$error(this.$t('test_track.length_less_than')+100);
return false;
}
if(!param.response.responseResult.httpCode){
param.response.responseResult.httpCode = 200;
}
if(!param.request.params.id){
param.request.params.id = getUUID();
}

View File

@ -15,7 +15,7 @@
:screen-height="screenHeight"
@row-click="clickRow"
row-key="id"
operator-width="80px"
operator-width="120px"
ref="table"
>