feat: 接口测试支持导入插件录制json格式

This commit is contained in:
chenjianxing 2020-07-16 13:10:52 +08:00
parent 97cb512ea9
commit 82cc66dbcd
2 changed files with 45 additions and 3 deletions

View File

@ -1,7 +1,12 @@
package io.metersphere.api.parse; package io.metersphere.api.parse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.parse.ApiImport; import io.metersphere.api.dto.parse.ApiImport;
import io.metersphere.commons.constants.MsRequestBodyType;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpMethod;
import java.io.InputStream; import java.io.InputStream;
@ -10,7 +15,44 @@ public class MsParser extends ApiImportAbstractParser {
@Override @Override
public ApiImport parse(InputStream source) { public ApiImport parse(InputStream source) {
String testStr = getApiTestStr(source); String testStr = getApiTestStr(source);
return JSON.parseObject(testStr, ApiImport.class); return JSON.parseObject(parsePluginFormat(testStr), ApiImport.class);
} }
private String parsePluginFormat(String testStr) {
JSONObject testObject = JSONObject.parseObject(testStr);
if (testObject.get("scenarios") != null) {
return testStr;
} else {
//插件格式
JSONArray scenarios = new JSONArray();
testObject.keySet().forEach(scenarioName -> {
JSONObject scenario = new JSONObject();
scenario.put("name", scenarioName);
JSONArray requestsObjects = new JSONArray();
JSONObject requestsObject = testObject.getJSONObject(scenarioName);
requestsObject.keySet().forEach(requestName -> {
JSONObject requestObject = requestsObject.getJSONObject(requestName);
requestObject.put("name", requestName);
JSONArray bodies = requestObject.getJSONArray("body");
if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) {
StringBuilder bodyStr = new StringBuilder();
for (int i = 0; i < bodies.size(); i++) {
String body = bodies.getString(i);
bodyStr.append(body);
}
JSONObject bodyObject = new JSONObject();
bodyObject.put("raw", bodyStr);
bodyObject.put("type", MsRequestBodyType.RAW.value());
requestObject.put("body", bodyObject);
}
requestsObjects.add(requestObject);
});
scenario.put("requests", requestsObjects);
scenarios.add(scenario);
});
JSONObject result = new JSONObject();
result.put("scenarios", scenarios);
return result.toJSONString();
}
}
} }

View File

@ -2,7 +2,7 @@
<el-form :model="request" :rules="rules" ref="request" label-width="100px"> <el-form :model="request" :rules="rules" ref="request" label-width="100px">
<el-form-item :label="$t('api_test.request.name')" prop="name"> <el-form-item :label="$t('api_test.request.name')" prop="name">
<el-input :disabled="isReadOnly" v-model="request.name" maxlength="100" show-word-limit/> <el-input :disabled="isReadOnly" v-model="request.name" maxlength="300" show-word-limit/>
</el-form-item> </el-form-item>
<el-form-item v-if="!request.useEnvironment" :label="$t('api_test.request.url')" prop="url" class="adjust-margin-bottom"> <el-form-item v-if="!request.useEnvironment" :label="$t('api_test.request.url')" prop="url" class="adjust-margin-bottom">
@ -91,7 +91,7 @@
activeName: "parameters", activeName: "parameters",
rules: { rules: {
name: [ name: [
{max: 100, message: this.$t('commons.input_limit', [1, 100]), trigger: 'blur'} {max: 300, message: this.$t('commons.input_limit', [1, 300]), trigger: 'blur'}
], ],
url: [ url: [
{max: 500, required: true, message: this.$t('commons.input_limit', [1, 500]), trigger: 'blur'}, {max: 500, required: true, message: this.$t('commons.input_limit', [1, 500]), trigger: 'blur'},