fix(接口测试): har文件过滤css和js

--bug=1046387 --user=宋天阳 【接口测试】定义-导入HAR文件失败 https://www.tapd.cn/55049933/s/1578799
This commit is contained in:
Jianguo-Genius 2024-09-14 12:39:08 +08:00 committed by Craftsman
parent 4436083f8c
commit eb847d899c
5 changed files with 102837 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import io.metersphere.api.parser.api.har.model.*;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.api.utils.JSONUtil;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
@ -240,6 +241,10 @@ public class HarParserApiDefinition extends HttpApiDefinitionImportAbstractParse
for (HarEntry entry : harEntryList) {
HarRequest harRequest = entry.request;
if (harRequest != null) {
// css js 略过
if (StringUtils.equalsIgnoreCase(harRequest.method, HttpMethodConstants.GET.name()) && StringUtils.endsWithAny(harRequest.url, ".css", ".js")) {
continue;
}
String url = harRequest.url;
if (url == null) {
continue;
@ -358,7 +363,7 @@ public class HarParserApiDefinition extends HttpApiDefinitionImportAbstractParse
if (StringUtils.equalsIgnoreCase("GET", requestBody.method) || requestBody.postData == null) {
return;
}
String bodyType = content.mimeType;
String bodyType = Body.BodyType.NONE.name();
if (StringUtils.isEmpty(bodyType)) {
body.setRawBody(new RawBody() {{
this.setValue(content.text);
@ -427,12 +432,9 @@ public class HarParserApiDefinition extends HttpApiDefinitionImportAbstractParse
}});
} else if (bodyType.startsWith(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
bodyType = Body.BodyType.BINARY.name();
List<HarPostParam> postParams = content.params;
// for (HarPostParam postParam : postParams) {
// KeyValue kv = new KeyValue(postParam.name, postParam.value);
// body.getFormDataBody().add(kv);
// }
body.setBinaryBody(new BinaryBody());
} else {
bodyType = Body.BodyType.RAW.name();
body.setRawBody(new RawBody() {{
this.setValue(content.text);
}});

View File

@ -79,7 +79,12 @@ public abstract class HttpApiDefinitionImportAbstractParser<T> implements ApiDef
protected ApiDefinitionDetail buildApiDefinition(String name, String path, String method, String modulePath, ImportRequest importRequest) {
ApiDefinitionDetail apiDefinition = new ApiDefinitionDetail();
apiDefinition.setId(IDGenerator.nextStr());
if (name != null) {
apiDefinition.setName(StringUtils.trim(name));
if (apiDefinition.getName().length() > 255) {
apiDefinition.setName(apiDefinition.getName().substring(0, 250) + "...");
}
}
apiDefinition.setPath(formatPath(StringUtils.trim(path)));
apiDefinition.setProtocol(StringUtils.trim(importRequest.getProtocol()));
apiDefinition.setMethod(StringUtils.trim(method));

View File

@ -52,6 +52,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -319,7 +320,7 @@ public class ApiDefinitionImportService {
}
private void insertApiDefinition(ImportRequest request, List<ApiDefinitionDetail> insertApiData, ApiDefinitionMapper apiMapper, ApiDefinitionBlobMapper apiBlobMapper, SqlSession sqlSession) {
insertApiData.forEach(t -> {
for (ApiDefinitionDetail t : insertApiData) {
ApiDefinition apiDefinition = new ApiDefinition();
BeanUtils.copyBean(apiDefinition, t);
if (StringUtils.isEmpty(apiDefinition.getId())) {
@ -342,10 +343,10 @@ public class ApiDefinitionImportService {
//插入blob数据
ApiDefinitionBlob apiDefinitionBlob = new ApiDefinitionBlob();
apiDefinitionBlob.setId(apiDefinition.getId());
apiDefinitionBlob.setRequest(JSON.toJSONBytes(t.getRequest()));
apiDefinitionBlob.setResponse(JSON.toJSONBytes(t.getResponse()));
apiDefinitionBlob.setRequest(ApiDataUtils.toJSONString(t.getRequest()).getBytes(StandardCharsets.UTF_8));
apiDefinitionBlob.setResponse(ApiDataUtils.toJSONString(t.getResponse()).getBytes(StandardCharsets.UTF_8));
apiBlobMapper.insertSelective(apiDefinitionBlob);
});
}
sqlSession.flushStatements();
}

View File

@ -2045,6 +2045,17 @@ public class ApiDefinitionControllerTests extends BaseTest {
//去重处理
List<String> apiDefinitionIdList = newApiTestCaseList.stream().map(ApiTestCase::getApiDefinitionId).distinct().collect(Collectors.toList());
Assertions.assertEquals(2, apiDefinitionIdList.size());
// 导入额外的har文件
inputStream = new FileInputStream(new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/import/har/testjsandcss.har")).getPath()));
file = new MockMultipartFile("file", "post-page.har", MediaType.APPLICATION_OCTET_STREAM_VALUE, inputStream);
request.setPlatform("har");
paramMap = new LinkedMultiValueMap<>();
paramMap.add("request", JSON.toJSONString(request));
paramMap.add("file", file);
this.requestMultipartWithOkAndReturn(IMPORT, paramMap);
apiDefinitionBlobs = apiDefinitionImportTestService.selectBlobByProjectId(importProject.getId());
Assertions.assertTrue(apiDefinitionBlobs.size() > 2);
}
@Resource

File diff suppressed because one or more lines are too long