fix(接口自动化): idea插件导入支持json-schema

This commit is contained in:
zhangdahai112 2022-05-10 21:54:54 +08:00 committed by zhangdahai112
parent aa7f59c3fd
commit 2d8a543e95
6 changed files with 78 additions and 17 deletions

View File

@ -80,7 +80,7 @@ public class HarParser extends HarAbstractParser {
for (HarEntry entry : harEntryList) {
HarRequest harRequest = entry.request;
String url = harRequest.url;
if(url == null){
if (url == null) {
continue;
}
@ -89,12 +89,12 @@ public class HarParser extends HarAbstractParser {
if (url.contains("?")) {
url = url.split("\\?")[0];
}
}catch (Exception e){
} catch (Exception e) {
}
if(savedUrl.contains(harRequest.url)){
if (savedUrl.contains(harRequest.url)) {
continue;
}else {
} else {
savedUrl.add(harRequest.url);
}
@ -106,7 +106,7 @@ public class HarParser extends HarAbstractParser {
}
if (harRequest != null) {
MsHTTPSamplerProxy request = super.buildRequest(reqName, url, harRequest.method);
MsHTTPSamplerProxy request = super.buildRequest(reqName, url, harRequest.method, null);
ApiDefinitionWithBLOBs apiDefinition = super.buildApiDefinition(request.getId(), reqName, url, harRequest.method, importRequest);
parseParameters(harRequest, request);
parseRequestBody(harRequest, request.getBody());
@ -156,7 +156,7 @@ public class HarParser extends HarAbstractParser {
}
private void parseHeaderParameters(HarHeader harHeader, List<KeyValue> headers) {
addHeader(headers, harHeader.name, harHeader.value,harHeader.comment, "", false);
addHeader(headers, harHeader.name, harHeader.value, harHeader.comment, "", false);
}
private HttpResponse parseResponse(HarResponse response) {
@ -206,15 +206,15 @@ public class HarParser extends HarAbstractParser {
contentType = org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
List<HarPostParam> postParams = content.params;
for (HarPostParam postParam : postParams) {
KeyValue kv = new KeyValue(postParam.name,postParam.value);
KeyValue kv = new KeyValue(postParam.name, postParam.value);
body.getKvs().add(kv);
}
} else if (contentType.startsWith(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
contentType = org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
List<HarPostParam> postParams = content.params;
if(CollectionUtils.isNotEmpty(postParams)){
if (CollectionUtils.isNotEmpty(postParams)) {
for (HarPostParam postParam : postParams) {
KeyValue kv = new KeyValue(postParam.name,postParam.value);
KeyValue kv = new KeyValue(postParam.name, postParam.value);
body.getKvs().add(kv);
}
}
@ -228,7 +228,7 @@ public class HarParser extends HarAbstractParser {
contentType = org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
List<HarPostParam> postParams = content.params;
for (HarPostParam postParam : postParams) {
KeyValue kv = new KeyValue(postParam.name,postParam.value);
KeyValue kv = new KeyValue(postParam.name, postParam.value);
body.getKvs().add(kv);
}
} else {
@ -243,7 +243,7 @@ public class HarParser extends HarAbstractParser {
return;
}
String contentType = content.mimeType;
if(body != null){
if (body != null) {
body.setType(getBodyType(contentType));
body.setRaw(content.text);
}

View File

@ -66,13 +66,15 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
if (childItems != null) {
ApiModule module = null;
module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);
parseItem(childItems, variables, results, module, path + "/" + module.getName(), cases, repeatMap, repeatable);
parseItem(childItems, variables, results, module, path + "/" + module.getName(), cases, repeatMap, repeatable);
} else {
MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item);
MsHTTPSamplerProxy response = parsePostmanResponse(item);
ApiDefinitionWithBLOBs request = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(),
msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), new ApiTestImportRequest());
request.setPath(msHTTPSamplerProxy.getPath());
request.setRequest(JSON.toJSONString(msHTTPSamplerProxy));
request.setResponse(JSON.toJSONString(response));
if (parentModule != null) {
request.setModuleId(parentModule.getId());
if (StringUtils.isNotBlank(this.selectModulePath)) {
@ -84,7 +86,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
if (request != null) {
if (repeatMap.keySet().contains(request.getMethod() + request.getPath())
&& (repeatable == null || repeatable == false)) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
BeanUtils.copyBean(apiTestCase, request);
apiTestCase.setApiDefinitionId(repeatMap.get(request.getMethod() + request.getPath()));
apiTestCase.setPriority("P0");

View File

@ -9,5 +9,6 @@ public class PostmanItem {
private String name;
private List<PostmanEvent> event;
private PostmanRequest request;
private List<PostmanResponse> response;
private List<PostmanItem> item;
}

View File

@ -0,0 +1,20 @@
package io.metersphere.api.dto.parse.postman;
import lombok.Data;
import java.util.List;
@Data
public class PostmanResponse {
private Integer code;
private Integer responseTime;
private String name;
private PostmanRequest originalRequest;
private String status;
private List<PostmanKeyValue> header;
private String body;
private String jsonSchema;
private PostmanUrl url;
private String description;
}

View File

@ -1,6 +1,7 @@
package io.metersphere.api.parse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.MsScenario;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -167,6 +168,15 @@ public abstract class ApiImportAbstractParser<T> implements ApiImportParser<T> {
return request;
}
protected MsHTTPSamplerProxy buildRequest(String name, String path, String method, String jsonSchema) {
MsHTTPSamplerProxy request = buildRequest(name, path, method);
if (StringUtils.isNotBlank(jsonSchema)) {
request.getBody().setJsonSchema(JSONObject.parseObject(jsonSchema));
request.getBody().setFormat("JSON-SCHEMA");
}
return request;
}
protected void addContentType(List<KeyValue> headers, String contentType) {
addHeader(headers, "Content-Type", contentType);
}

View File

@ -2,7 +2,6 @@ package io.metersphere.api.parse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.parse.postman.*;
@ -11,6 +10,7 @@ import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.MsRequestBodyType;
import io.metersphere.commons.constants.PostmanRequestBodyMode;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.plugin.core.MsTestElement;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -30,7 +30,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
}
requestDesc.getAuth(); // todo 认证方式等待优化
PostmanUrl url = requestDesc.getUrl();
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod());
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod(), requestDesc.getBody().getString("jsonSchema"));
request.setRest(parseKeyValue(requestDesc.getUrl().getVariable()));
if (StringUtils.isNotBlank(request.getPath())) {
String path = request.getPath().split("\\?")[0];
@ -48,10 +48,38 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
return request;
}
protected MsHTTPSamplerProxy parsePostmanResponse(PostmanItem requestItem) {
List<PostmanResponse> requestList = requestItem.getResponse();
if (CollectionUtils.isEmpty(requestList)) {
return new MsHTTPSamplerProxy();
}
PostmanResponse requestDesc = requestItem.getResponse().get(0);
if (requestDesc == null) {
return null;
}
PostmanUrl url = requestDesc.getOriginalRequest().getUrl();
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), null, null, requestDesc.getJsonSchema());
request.setRest(parseKeyValue(requestDesc.getOriginalRequest().getUrl().getVariable()));
if (StringUtils.isNotBlank(request.getPath())) {
String path = request.getPath().split("\\?")[0];
path = parseVariable(path);
request.setPath(path);
} else {
request.setPath("/");
}
//todo response 后续支持更多类型导入
request.getBody().setType(Body.JSON_STR);
request.setArguments(parseKeyValue(url == null ? new ArrayList<>() : url.getQuery()));
request.setHeaders(parseKeyValue(requestDesc.getHeader()));
addBodyHeader(request);
return request;
}
/**
* 将postman的变量转换成ms变量
* {{xxx}} -> ${xxx}
* 将postman的变量转换成ms变量
* {{xxx}} -> ${xxx}
*
* @param value
* @return
*/