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.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -189,12 +190,19 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (this.body != null) {
List<KeyValue> bodyParams = this.body.getBodyParams(sampler, this.getId());
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 -> {
if (StringUtils.isNotEmpty(files.getName()) && "file".equals(files.getType()) && CollectionUtils.isNotEmpty(files.getFiles())) {
sampler.setDoBrowserCompatibleMultipart(true);
}
if (StringUtils.isNotEmpty(files.getName())) {
kvIsEmpty.set(false);
}
});
//值不为空时才会设置doMultiPart
if (!kvIsEmpty.get()) {
sampler.setDoMultipart(true);
}
}
if (CollectionUtils.isNotEmpty(bodyParams)) {
Arguments arguments = httpArguments(bodyParams);
@ -684,28 +692,28 @@ public class MsHTTPSamplerProxy extends MsTestElement {
list.stream().
filter(KeyValue::isValid).
filter(KeyValue::isEnable).forEach(keyValue -> {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if(StringUtils.equalsIgnoreCase(this.method,"get")){
if(StringUtils.isNotEmpty(httpArgument.getValue())){
arguments.addArgument(httpArgument);
}
}else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument);
}
} else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
}
}
);
}
}
);
return arguments;
}

View File

@ -1645,18 +1645,9 @@ public class ApiDefinitionService {
return extApiDefinitionMapper.selectEffectiveIdByProjectId(projectId);
}
// public List<ApiDefinition> selectByProjectIdAndMethodAndUrl(String projectId, String method,String url) {
// 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 baseUrlSuffix) {
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String urlSuffix) {
if (StringUtils.isEmpty(urlSuffix)) {
if (StringUtils.isEmpty(baseUrlSuffix)) {
return new ArrayList<>();
} else {
ApiDefinitionExample example = new ApiDefinitionExample();
@ -1665,6 +1656,7 @@ public class ApiDefinitionService {
List<String> apiIdList = new ArrayList<>();
boolean urlSuffixEndEmpty = false;
String urlSuffix = baseUrlSuffix;
if (urlSuffix.endsWith("/")) {
urlSuffixEndEmpty = true;
urlSuffix = urlSuffix + "testMock";
@ -1674,29 +1666,33 @@ public class ApiDefinitionService {
urlParams[urlParams.length - 1] = "";
}
for (ApiDefinition api : apiList) {
String path = api.getPath();
if (StringUtils.isEmpty(path)) {
continue;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
if (StringUtils.isNotEmpty(path)) {
String[] pathArr = path.split("/");
if (pathArr.length == urlParams.length) {
boolean isFetch = true;
for (int i = 0; i < urlParams.length; i++) {
String pathItem = pathArr[i];
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
if (!StringUtils.equals(pathArr[i], urlParams[i])) {
isFetch = false;
break;
if(StringUtils.equalsAny(api.getPath(),baseUrlSuffix,"/"+baseUrlSuffix)){
apiIdList.add(api.getId());
}else {
String path = api.getPath();
if (StringUtils.isEmpty(path)) {
continue;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
if (StringUtils.isNotEmpty(path)) {
String[] pathArr = path.split("/");
if (pathArr.length == urlParams.length) {
boolean isFetch = true;
for (int i = 0; i < urlParams.length; i++) {
String pathItem = pathArr[i];
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
if (!StringUtils.equals(pathArr[i], urlParams[i])) {
isFetch = false;
break;
}
}
}
}
if (isFetch) {
apiIdList.add(api.getId());
}
if (isFetch) {
apiIdList.add(api.getId());
}
}
}
}

View File

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