feat: Swagger2 导入接口测试

This commit is contained in:
chenjianxing 2020-07-15 15:35:36 +08:00
parent cf9cffb114
commit 685da4ccef
16 changed files with 302 additions and 143 deletions

View File

@ -190,6 +190,13 @@
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<!-- swagger 解析 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.51</version>
</dependency>
</dependencies>
<build>

View File

@ -14,4 +14,5 @@ public class SwaggerApi {
private List<String> schemes;
private List<SwaggerTag> tags;
private JSONObject paths;
private JSONObject definitions;
}

View File

@ -12,5 +12,5 @@ public class SwaggerRequest {
private String operationId;
private List<String> consumes;
private List<String> produces;
private SwaggerParameter parameters;
private List<SwaggerParameter> parameters;
}

View File

@ -6,6 +6,7 @@ import lombok.Data;
public class KeyValue {
private String name;
private String value;
private String description;
public KeyValue() {
}
@ -14,4 +15,10 @@ public class KeyValue {
this.name = name;
this.value = value;
}
public KeyValue(String name, String value, String description) {
this.name = name;
this.value = value;
this.description = description;
}
}

View File

@ -1,12 +1,19 @@
package io.metersphere.api.parse;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.Request;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpHeader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public abstract class ApiImportAbstractParser implements ApiImportParser {
@ -34,4 +41,36 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
return testStr.toString();
}
protected void addContentType(Request request, String contentType) {
addHeader(request, HttpHeader.CONTENT_TYPE.toString(), contentType);
}
protected void addCookie(Request request, String key, String value) {
List<KeyValue> headers = Optional.ofNullable(request.getHeaders()).orElse(new ArrayList<>());
boolean hasCookie = false;
for (KeyValue header : headers) {
if (StringUtils.equalsIgnoreCase(HttpHeader.COOKIE.name(), header.getName())) {
hasCookie = true;
String cookies = Optional.ofNullable(header.getValue()).orElse("");
header.setValue(cookies + key + "=" + value + ";");
}
}
if (!hasCookie) {
addHeader(request, HttpHeader.COOKIE.name(), key + "=" + value + ";");
}
}
protected void addHeader(Request request, String key, String value) {
List<KeyValue> headers = Optional.ofNullable(request.getHeaders()).orElse(new ArrayList<>());
boolean hasContentType = false;
for (KeyValue header : headers) {
if (StringUtils.equalsIgnoreCase(header.getName(), key)) {
hasContentType = true;
}
}
if (!hasContentType) {
headers.add(new KeyValue(key, value));
}
request.setHeaders(headers);
}
}

View File

@ -12,6 +12,8 @@ public class ApiImportParserFactory {
return new MsParser();
} else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) {
return new PostmanParser();
} else if (StringUtils.equals(ApiImportPlatform.Swagger2.name(), platform)) {
return new Swagger2Parser();
}
return null;
}

View File

@ -14,6 +14,7 @@ import io.metersphere.commons.constants.PostmanRequestBodyMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpHeader;
import java.io.BufferedReader;
import java.io.IOException;
@ -64,32 +65,27 @@ public class PostmanParser extends ApiImportAbstractParser {
request.setMethod(requestDesc.getMethod());
request.setHeaders(parseKeyValue(requestDesc.getHeader()));
request.setParameters(parseKeyValue(url.getQuery()));
Body body = new Body();
JSONObject postmanBody = requestDesc.getBody();
String bodyMode = postmanBody.getString("mode");
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
body.setRaw(postmanBody.getString(bodyMode));
body.setType(MsRequestBodyType.RAW.value());
String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language");
List<KeyValue> headers = request.getHeaders();
boolean hasContentType = false;
for (KeyValue header : headers) {
if (StringUtils.equalsIgnoreCase(header.getName(), "Content-Type")) {
hasContentType = true;
}
}
if (!hasContentType) {
headers.add(new KeyValue("Content-Type", contentType));
}
} else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) {
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
body.setType(MsRequestBodyType.KV.value());
body.setKvs(parseKeyValue(postmanKeyValues));
}
request.setBody(body);
request.setBody(parseBody(requestDesc, request));
requests.add(request);
}
return requests;
}
private Body parseBody(PostmanRequest requestDesc, Request request) {
Body body = new Body();
JSONObject postmanBody = requestDesc.getBody();
String bodyMode = postmanBody.getString("mode");
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
body.setRaw(postmanBody.getString(bodyMode));
body.setType(MsRequestBodyType.RAW.value());
String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language");
addContentType(request, contentType);
} else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) {
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
body.setType(MsRequestBodyType.KV.value());
body.setKvs(parseKeyValue(postmanKeyValues));
}
return body;
}
}

View File

@ -0,0 +1,185 @@
package io.metersphere.api.parse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.parse.ApiImport;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.Request;
import io.metersphere.api.dto.scenario.Scenario;
import io.metersphere.commons.constants.MsRequestBodyType;
import io.metersphere.commons.constants.SwaggerParameterType;
import io.swagger.models.*;
import io.swagger.models.parameters.*;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.ObjectProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.parser.SwaggerParser;
import java.io.InputStream;
import java.util.*;
public class Swagger2Parser extends ApiImportAbstractParser {
@Override
public ApiImport parse(InputStream source) {
String testStr = getApiTestStr(source);
// Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");
Swagger swagger = new SwaggerParser().readWithInfo(testStr).getSwagger();
ApiImport apiImport = new ApiImport();
apiImport.setScenarios(parseRequests(swagger));
return apiImport;
}
private List<Scenario> parseRequests(Swagger swagger) {
Map<String, Path> paths = swagger.getPaths();
Set<String> pathNames = paths.keySet();
Map<String, Scenario> scenarioMap = new HashMap<>();
List<Scenario> scenarios = new ArrayList<>();
for (String pathName : pathNames) {
Path path = paths.get(pathName);
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
Set<HttpMethod> httpMethods = operationMap.keySet();
for (HttpMethod method : httpMethods) {
Operation operation = operationMap.get(method);
Request request = new Request();
request.setName(operation.getOperationId());
request.setPath(pathName);
request.setUseEnvironment(true);
request.setMethod(method.name());
parseParameters(operation, swagger.getDefinitions(), request);
List<String> tags = operation.getTags();
if (tags != null) {
tags.forEach(tag -> {
Scenario scenario = Optional.ofNullable(scenarioMap.get(tag)).orElse(new Scenario());
List<Request> requests = Optional.ofNullable(scenario.getRequests()).orElse(new ArrayList<>());
requests.add(request);
scenario.setRequests(requests);scenario.setName(tag);
scenarioMap.put(tag, scenario);
});
} else {
Scenario scenario = Optional.ofNullable(scenarioMap.get("default")).orElse(new Scenario());
List<Request> requests = Optional.ofNullable(scenario.getRequests()).orElse(new ArrayList<>());
requests.add(request);
scenario.setRequests(requests);
scenarioMap.put("default", scenario);
}
}
}
scenarioMap.values().forEach(scenario -> {
scenarios.add(scenario);
});
return scenarios;
}
private void parseParameters(Operation operation, Map<String, Model> definitions, Request request) {
List<Parameter> parameters = operation.getParameters();
for (Parameter parameter : parameters) {
switch (parameter.getIn()){
// case SwaggerParameterType.PATH:
// parsePathParameters(parameter, request);
// break;
case SwaggerParameterType.QUERY:
parseQueryParameters(parameter, request);
break;
case SwaggerParameterType.FORM_DATA:
parseFormDataParameters(parameter, request);
break;
case SwaggerParameterType.BODY:
parseBodyParameters(parameter, request, definitions);
break;
case SwaggerParameterType.HEADER:
parseHeaderParameters(parameter, request);
break;
case SwaggerParameterType.COOKIE:
parseCookieParameters(parameter, request);
break;
// case SwaggerParameterType.FILE:
// parsePathParameters(parameter, request);
// break;
}
}
}
private void parseCookieParameters(Parameter parameter, Request request) {
CookieParameter cookieParameter = (CookieParameter) parameter;
addCookie(request, cookieParameter.getName(), cookieParameter.getDescription());
}
private void parseHeaderParameters(Parameter parameter, Request request) {
HeaderParameter headerParameter = (HeaderParameter) parameter;
addHeader(request, headerParameter.getName(), headerParameter.getDescription());
}
private void parseBodyParameters(Parameter parameter, Request request, Map<String, Model> definitions) {
BodyParameter bodyParameter = (BodyParameter) parameter;
Body body = Optional.ofNullable(request.getBody()).orElse(new Body());
body.setType(MsRequestBodyType.RAW.value());
Model schema = bodyParameter.getSchema();
if (schema instanceof RefModel) {
RefModel refModel = (RefModel) bodyParameter.getSchema();
Model model = definitions.get(refModel.getSimpleRef());
JSONObject bodyParameters = getBodyJSONObjectParameters(model.getProperties(), definitions);
body.setRaw(bodyParameters.toJSONString());
} else if (schema instanceof ArrayModel) {
ArrayModel arrayModel = (ArrayModel) bodyParameter.getSchema();
Property items = arrayModel.getItems();
if (items instanceof RefProperty) {
RefProperty refProperty = (RefProperty) items;
Model model = definitions.get(refProperty.getSimpleRef());
JSONArray propertyList = new JSONArray();
propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions));
body.setRaw(propertyList.toString());
}
}
request.setBody(body);
addContentType(request, "application/json");
}
private JSONObject getBodyJSONObjectParameters(Map<String, Property> properties, Map<String, Model> definitions) {
JSONObject jsonObject = new JSONObject();
properties.forEach((key, value) -> {
if (value instanceof ObjectProperty) {
ObjectProperty objectProperty = (ObjectProperty) value;
jsonObject.put(key, getBodyJSONObjectParameters(objectProperty.getProperties(), definitions));
} else if (value instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) value;
Property items = arrayProperty.getItems();
if (items instanceof RefProperty) {
RefProperty refProperty = (RefProperty) items;
Model model = definitions.get(refProperty.getSimpleRef());
JSONArray propertyList = new JSONArray();
propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions));
jsonObject.put(key, propertyList);
} else {
jsonObject.put(key, new ArrayList<>());
}
} else {
jsonObject.put(key, Optional.ofNullable(value.getDescription()).orElse(""));
}
});
return jsonObject;
}
private void parseFormDataParameters(Parameter parameter, Request request) {
Body body = Optional.ofNullable(request.getBody()).orElse(new Body());
body.setType(MsRequestBodyType.FORM_DATA.value());
List<KeyValue> keyValues = Optional.ofNullable(body.getKvs()).orElse(new ArrayList<>());
keyValues.add(new KeyValue(parameter.getName(), "", parameter.getDescription()));
body.setKvs(keyValues);
request.setBody(body);
}
private void parseQueryParameters(Parameter parameter, Request request) {
QueryParameter queryParameter = (QueryParameter) parameter;
List<KeyValue> parameters = Optional.ofNullable(request.getParameters()).orElse(new ArrayList<>());
parameters.add(new KeyValue(queryParameter.getName(), "", queryParameter.getDescription()));
request.setParameters(parameters);
}
}

View File

@ -1,106 +0,0 @@
package io.metersphere.api.parse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.parse.ApiImport;
import io.metersphere.api.dto.parse.postman.PostmanItem;
import io.metersphere.api.dto.parse.postman.PostmanRequest;
import io.metersphere.api.dto.parse.postman.PostmanUrl;
import io.metersphere.api.dto.parse.swagger.SwaggerApi;
import io.metersphere.api.dto.parse.swagger.SwaggerInfo;
import io.metersphere.api.dto.parse.swagger.SwaggerRequest;
import io.metersphere.api.dto.scenario.Request;
import io.metersphere.api.dto.scenario.Scenario;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class SwaggerParser extends ApiImportAbstractParser {
@Override
public ApiImport parse(InputStream source) {
String testStr = getApiTestStr(source);
SwaggerApi swaggerApi = JSON.parseObject(testStr.toString(), SwaggerApi.class);
SwaggerInfo info = swaggerApi.getInfo();
String title = info.getTitle();
// List<Request> requests = parseRequests(swaggerApi);
// ApiImport apiImport = new ApiImport();
// List<Scenario> scenarios = new ArrayList<>();
// Scenario scenario = new Scenario();
// scenario.setRequests(requests);
// scenario.setName(info.getName());
// scenarios.add(scenario);
// apiImport.setScenarios(scenarios);
// return apiImport;
return null;
}
// private List<Request> parseRequests(SwaggerApi swaggerApi) {
// JSONObject paths = swaggerApi.getPaths();
//
// Set<String> pathNames = paths.keySet();
//
// for (String path : pathNames) {
// JSONObject pathObject = paths.getJSONObject(path);
// Set<String> methods = pathObject.keySet();
// for (String method : methods) {
// SwaggerRequest swaggerRequest = JSON.parseObject(pathObject.getJSONObject(method).toJSONString(), SwaggerRequest.class);
// Request request = new Request();
// request.setName(swaggerRequest.getOperationId());
// request.setUrl(url.getRaw());
// request.setPath(.getRaw());
// request.setUseEnvironment(false);
// request.setMethod(requestDesc.getMethod());
// request.setHeaders(parseKeyValue(requestDesc.getHeader()));
// request.setParameters(parseKeyValue(url.getQuery()));
//
// }
// }
//
// List<PostmanItem> item = postmanCollection.getItem();
// List<Request> requests = new ArrayList<>();
// for (PostmanItem requestItem : item) {
// Request request = new Request();
// PostmanRequest requestDesc = requestItem.getRequest();
// PostmanUrl url = requestDesc.getUrl();
// request.setName(requestItem.getName());
// request.setUrl(url.getRaw());
// request.setUseEnvironment(false);
// request.setMethod(requestDesc.getMethod());
// request.setHeaders(parseKeyValue(requestDesc.getHeader()));
// request.setParameters(parseKeyValue(url.getQuery()));
// Body body = new Body();
// JSONObject postmanBody = requestDesc.getBody();
// String bodyMode = postmanBody.getString("mode");
// if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
// body.setRaw(postmanBody.getString(bodyMode));
// body.setType(MsRequestBodyType.RAW.value());
// String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language");
// List<KeyValue> headers = request.getHeaders();
// boolean hasContentType = false;
// for (KeyValue header : headers) {
// if (StringUtils.equalsIgnoreCase(header.getName(), "Content-Type")) {
// hasContentType = true;
// }
// }
// if (!hasContentType) {
// headers.add(new KeyValue("Content-Type", contentType));
// }
// } else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) {
// List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
// body.setType(MsRequestBodyType.KV.value());
// body.setKvs(parseKeyValue(postmanKeyValues));
// }
// request.setBody(body);
// requests.add(request);
// }
// return requests;
// }
}

View File

@ -1,5 +1,5 @@
package io.metersphere.commons.constants;
public enum ApiImportPlatform {
Metersphere, Postman
Metersphere, Postman, Swagger2
}

View File

@ -0,0 +1,12 @@
package io.metersphere.commons.constants;
public class SwaggerParameterType {
public static final String PATH = "path";
public static final String FORM_DATA = "formData";
public static final String FILE = "file";
public static final String HEADER = "header";
public static final String BODY = "body";
public static final String COOKIE = "cookie";
public static final String QUERY = "query";
}

View File

@ -56,7 +56,14 @@
name: 'Postman',
value: 'Postman',
tip: this.$t('api_test.api_import.postman_tip'),
exportTip: this.$t('api_test.api_import.post_man_export_tip'),
exportTip: this.$t('api_test.api_import.post_export_tip'),
suffixes: new Set(['json'])
},
{
name: 'Swagger',
value: 'Swagger2',
tip: this.$t('api_test.api_import.swagger_tip'),
exportTip: this.$t('api_test.api_import.swagger_export_tip'),
suffixes: new Set(['json'])
}
],

View File

@ -394,16 +394,19 @@ const JMX_ASSERTION_CONDITION = {
class JMXRequest {
constructor(request) {
if (request && request instanceof Request && request.url) {
let url = new URL(request.url);
this.method = request.method;
this.hostname = decodeURIComponent(url.hostname);
this.pathname = decodeURIComponent(url.pathname);
this.path = decodeURIComponent(request.path);
if (request && request instanceof Request && (request.url || request.path)) {
this.useEnvironment = request.useEnvironment;
this.environment = request.environment;
this.port = url.port;
this.protocol = url.protocol.split(":")[0];
this.path = decodeURIComponent(request.path);
this.method = request.method;
if (!request.useEnvironment) {
let url = new URL(request.url);
this.hostname = decodeURIComponent(url.hostname);
this.pathname = decodeURIComponent(url.pathname);
this.port = url.port;
this.protocol = url.protocol.split(":")[0];
}
if (this.method.toUpperCase() !== "GET") {
// this.pathname += url.search.replace('&', '&amp;');
this.pathname += '?';

View File

@ -374,8 +374,10 @@ export default {
export_tip: "Export Tip",
ms_tip: "Support for Metersphere JSON format",
ms_export_tip: "Export jSON-formatted files via Metersphere website or browser plug-ins",
swagger_tip: "Only Swagger2.x json files are supported",
postman_tip: "Only Postman Collection V2.1 json files are supported",
post_man_export_tip: "Export the test collection by Postman",
postman_export_tip: "Export the test collection by Postman",
swagger_export_tip: "Export jSON-formatted files via Swagger website",
suffixFormatErr: "The file format does not meet the requirements",
}
},

View File

@ -373,7 +373,9 @@ export default {
ms_tip: "支持 Metersphere json 格式",
ms_export_tip: "通过 Metersphere Api 测试页面或者浏览器插件导出 json 格式文件",
postman_tip: "只支持 Postman Collection v2.1 格式的 json 文件",
post_man_export_tip: "通过 Postman 导出测试集合",
swagger_tip: "只支持 Swagger2.x 版本的 json 文件",
post_export_tip: "通过 Postman 导出测试集合",
swagger_export_tip: "通过 Swagger 页面导出",
suffixFormatErr: "文件格式不符合要求",
}
},

View File

@ -373,7 +373,9 @@ export default {
ms_tip: "支持 Metersphere json 格式",
ms_export_tip: "通過 Metersphere Api 測試頁面或者瀏覽器插件導出 json 格式文件",
postman_tip: "只支持 Postman Collection v2.1 格式的 json 文件",
post_man_export_tip: "通過 Postman 導出測試集合",
swagger_tip: "只支持 Swagger2.x 版本的 json 文件",
post_export_tip: "通過 Postman 導出測試集合",
swagger_export_tip: "通過 Swagger 頁面導出",
suffixFormatErr: "文件格式不符合要求",
}
},