fix(接口测试): 修复Mock匹配不到的问题

--bug=1011558 --user=宋天阳 【接口测试】接口-新建mock-运行返回错误
https://www.tapd.cn/55049933/s/1123528
This commit is contained in:
song-tianyang 2022-03-22 17:53:14 +08:00 committed by CountryBuilder
parent 8527b24911
commit 17f1296319
3 changed files with 62 additions and 56 deletions

View File

@ -57,6 +57,7 @@ import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -189,12 +190,19 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (this.body != null) { if (this.body != null) {
List<KeyValue> bodyParams = this.body.getBodyParams(sampler, this.getId()); List<KeyValue> bodyParams = this.body.getBodyParams(sampler, this.getId());
if (StringUtils.isNotEmpty(this.body.getType()) && "Form Data".equals(this.body.getType())) { if (StringUtils.isNotEmpty(this.body.getType()) && "Form Data".equals(this.body.getType())) {
sampler.setDoMultipart(true); AtomicBoolean kvIsEmpty = new AtomicBoolean(true);
this.body.getKvs().forEach(files -> { this.body.getKvs().forEach(files -> {
if (StringUtils.isNotEmpty(files.getName()) && "file".equals(files.getType()) && CollectionUtils.isNotEmpty(files.getFiles())) { if (StringUtils.isNotEmpty(files.getName()) && "file".equals(files.getType()) && CollectionUtils.isNotEmpty(files.getFiles())) {
sampler.setDoBrowserCompatibleMultipart(true); sampler.setDoBrowserCompatibleMultipart(true);
} }
if (StringUtils.isNotEmpty(files.getName())) {
kvIsEmpty.set(false);
}
}); });
//值不为空时才会设置doMultiPart
if (!kvIsEmpty.get()) {
sampler.setDoMultipart(true);
}
} }
if (CollectionUtils.isNotEmpty(bodyParams)) { if (CollectionUtils.isNotEmpty(bodyParams)) {
Arguments arguments = httpArguments(bodyParams); Arguments arguments = httpArguments(bodyParams);
@ -694,11 +702,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (StringUtils.isNotBlank(keyValue.getContentType())) { if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType()); httpArgument.setContentType(keyValue.getContentType());
} }
if(StringUtils.equalsIgnoreCase(this.method,"get")){ if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if(StringUtils.isNotEmpty(httpArgument.getValue())){ if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument); arguments.addArgument(httpArgument);
} }
}else { } else {
arguments.addArgument(httpArgument); arguments.addArgument(httpArgument);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -1645,18 +1645,9 @@ public class ApiDefinitionService {
return extApiDefinitionMapper.selectEffectiveIdByProjectId(projectId); return extApiDefinitionMapper.selectEffectiveIdByProjectId(projectId);
} }
// public List<ApiDefinition> selectByProjectIdAndMethodAndUrl(String projectId, String method,String url) { public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String baseUrlSuffix) {
// ApiDefinitionExample example = new ApiDefinitionExample();
// ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId);
// if(StringUtils.isNotEmpty(url)){
// criteria.andPathEqualTo(url);
// }
// return apiDefinitionMapper.selectByExample(example);
// }
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String urlSuffix) { if (StringUtils.isEmpty(baseUrlSuffix)) {
if (StringUtils.isEmpty(urlSuffix)) {
return new ArrayList<>(); return new ArrayList<>();
} else { } else {
ApiDefinitionExample example = new ApiDefinitionExample(); ApiDefinitionExample example = new ApiDefinitionExample();
@ -1665,6 +1656,7 @@ public class ApiDefinitionService {
List<String> apiIdList = new ArrayList<>(); List<String> apiIdList = new ArrayList<>();
boolean urlSuffixEndEmpty = false; boolean urlSuffixEndEmpty = false;
String urlSuffix = baseUrlSuffix;
if (urlSuffix.endsWith("/")) { if (urlSuffix.endsWith("/")) {
urlSuffixEndEmpty = true; urlSuffixEndEmpty = true;
urlSuffix = urlSuffix + "testMock"; urlSuffix = urlSuffix + "testMock";
@ -1674,6 +1666,9 @@ public class ApiDefinitionService {
urlParams[urlParams.length - 1] = ""; urlParams[urlParams.length - 1] = "";
} }
for (ApiDefinition api : apiList) { for (ApiDefinition api : apiList) {
if(StringUtils.equalsAny(api.getPath(),baseUrlSuffix,"/"+baseUrlSuffix)){
apiIdList.add(api.getId());
}else {
String path = api.getPath(); String path = api.getPath();
if (StringUtils.isEmpty(path)) { if (StringUtils.isEmpty(path)) {
continue; continue;
@ -1701,6 +1696,7 @@ public class ApiDefinitionService {
} }
} }
} }
}
if (apiIdList.isEmpty()) { if (apiIdList.isEmpty()) {
return new ArrayList<>(); return new ArrayList<>();
} else { } else {

View File

@ -393,9 +393,11 @@ public class MockConfigService {
if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.containsKey("kvs")) { if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.containsKey("kvs")) {
JSONArray kvsArr = expectBodyObject.getJSONArray("kvs"); JSONArray kvsArr = expectBodyObject.getJSONArray("kvs");
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr); List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr);
if(CollectionUtils.isNotEmpty(mockConfigRequestParams)){
if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) { if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) {
return false; return false;
} }
}
} else { } else {
JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject); JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
if (mockExpectJsonArray instanceof JSONObject) { if (mockExpectJsonArray instanceof JSONObject) {