fix(接口测试): 修复mock相关缺陷
--bug=1040885 --user=王孝刚 【接口测试】Mock-匹配规则为正则表达式,请求Mock地址,参数符合匹配规则,匹配失败 https://www.tapd.cn/55049933/s/1518198
This commit is contained in:
parent
e0d3997b7f
commit
0eef2ef893
|
@ -56,7 +56,7 @@ public class BodyParamMatchRule {
|
|||
private BinaryBody binaryBody = new BinaryBody();
|
||||
|
||||
public boolean matchXml(Map<String, Object> requestMap) {
|
||||
Map<String, Object> mockMap = XMLUtils.xmlStringToJson(rawBody.getValue());
|
||||
Map<String, Object> mockMap = XMLUtils.xmlStringToJson(xmlBody.getValue());
|
||||
return this.matchMap(mockMap, requestMap);
|
||||
}
|
||||
|
||||
|
@ -109,6 +109,9 @@ public class BodyParamMatchRule {
|
|||
}
|
||||
|
||||
private boolean matchMap(Map<String, Object> mockMap, Map<String, Object> requestMap) {
|
||||
if ((mockMap.isEmpty() && !requestMap.isEmpty()) || (!mockMap.isEmpty() && requestMap.isEmpty())) {
|
||||
return false;
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : mockMap.entrySet()) {
|
||||
if (!this.matchObject(entry.getValue(), requestMap.get(entry.getKey()))) {
|
||||
return false;
|
||||
|
|
|
@ -5,8 +5,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
|||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Data
|
||||
public class KeyValueInfo {
|
||||
@Schema(description = "Key")
|
||||
|
@ -23,19 +21,24 @@ public class KeyValueInfo {
|
|||
private String description;
|
||||
|
||||
public boolean matchValue(String value) {
|
||||
return switch (ParamConditionEnums.valueOf(this.condition)) {
|
||||
case EQUALS -> StringUtils.equals(this.value, value);
|
||||
case NOT_EQUALS -> !StringUtils.equals(this.value, value);
|
||||
case CONTAINS -> StringUtils.contains(value, this.value);
|
||||
case NOT_CONTAINS -> !StringUtils.contains(value, this.value);
|
||||
case LENGTH_EQUALS -> this.value.length() == value.length();
|
||||
case LENGTH_NOT_EQUALS -> this.value.length() != value.length();
|
||||
case LENGTH_SHOT -> value.length() < this.value.length();
|
||||
case LENGTH_LARGE -> value.length() > this.value.length();
|
||||
case REGULAR_MATCH -> value.matches(Pattern.quote(this.value));
|
||||
case IS_EMPTY -> StringUtils.isBlank(value);
|
||||
case IS_NOT_EMPTY -> StringUtils.isNotBlank(value);
|
||||
default -> false;
|
||||
};
|
||||
try {
|
||||
return switch (ParamConditionEnums.valueOf(this.condition)) {
|
||||
case EQUALS -> StringUtils.equals(this.value, value);
|
||||
case NOT_EQUALS -> !StringUtils.equals(this.value, value);
|
||||
case CONTAINS -> StringUtils.contains(value, this.value);
|
||||
case NOT_CONTAINS -> !StringUtils.contains(value, this.value);
|
||||
case LENGTH_EQUALS -> value.length() == Integer.parseInt(this.value);
|
||||
case LENGTH_NOT_EQUALS -> value.length() != Integer.parseInt(this.value);
|
||||
case LENGTH_SHOT -> value.length() < Integer.parseInt(this.value);
|
||||
case LENGTH_LARGE -> value.length() > Integer.parseInt(this.value);
|
||||
case REGULAR_MATCH -> value.matches(this.value);
|
||||
case IS_EMPTY -> StringUtils.isBlank(value);
|
||||
case IS_NOT_EMPTY -> StringUtils.isNotBlank(value);
|
||||
default -> false;
|
||||
};
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -128,7 +128,7 @@ public class MockServerService {
|
|||
List<ApiDefinitionMockConfig> mockConfigs = apiDefinitionMockConfigMapper.selectByExampleWithBLOBs(mockConfigExample);
|
||||
// 寻找匹配的 ApiDefinitionMockConfig
|
||||
ApiDefinitionMockConfig apiDefinitionMockConfig = mockConfigs.stream()
|
||||
.filter(mockConfig -> MockServerUtils.matchMockConfig(mockConfig.getMatching(), requestHeaderMap, param) && matchBinaryBody(mockConfig, param.getBinaryParamsObj(), apiId))
|
||||
.filter(mockConfig -> MockServerUtils.matchMockConfig(mockConfig.getMatching(), requestHeaderMap, param) && matchBinaryBody(mockConfig, param.getBinaryParamsObj(), apiDefinitionMockList.getFirst().getProjectId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
|
|
|
@ -46,20 +46,36 @@
|
|||
v-model="record.enable"
|
||||
type="line"
|
||||
:before-change="() => handleBeforeEnableChange(record)"
|
||||
:disabled="!hasAnyPermission(['PROJECT_API_DEFINITION_MOCK:READ+UPDATE'])"
|
||||
></a-switch>
|
||||
</template>
|
||||
<template #action="{ record }">
|
||||
<MsButton type="text" class="!mr-0" @click="editMock(record)">
|
||||
<MsButton
|
||||
v-permission="['PROJECT_API_DEFINITION_MOCK:READ+UPDATE']"
|
||||
type="text"
|
||||
class="!mr-0"
|
||||
@click="editMock(record)"
|
||||
>
|
||||
{{ t('common.edit') }}
|
||||
</MsButton>
|
||||
<a-divider direction="vertical" :margin="8"></a-divider>
|
||||
<MsButton type="text" class="!mr-0" @click="debugMock(record)">
|
||||
<MsButton
|
||||
v-permission="['PROJECT_API_DEFINITION:READ+EXECUTE']"
|
||||
type="text"
|
||||
class="!mr-0"
|
||||
@click="debugMock(record)"
|
||||
>
|
||||
{{ t('apiTestManagement.debug') }}
|
||||
</MsButton>
|
||||
<a-divider direction="vertical" :margin="8"></a-divider>
|
||||
<MsButton type="text" class="!mr-0" @click="handleCopyMock(record)">
|
||||
{{ t('common.copy') }}
|
||||
</MsButton>
|
||||
<MsButton
|
||||
v-permission="['PROJECT_API_DEFINITION_MOCK:READ+ADD']"
|
||||
type="text"
|
||||
class="!mr-0"
|
||||
@click="handleCopyMock(record)"
|
||||
>
|
||||
{{ t('common.copy') }} </MsButton
|
||||
>x
|
||||
<a-divider direction="vertical" :margin="8"></a-divider>
|
||||
<MsTableMoreAction :list="tableMoreActionList" @select="handleTableMoreActionSelect($event, record)" />
|
||||
</template>
|
||||
|
@ -322,11 +338,13 @@
|
|||
{
|
||||
label: 'mockManagement.batchEdit',
|
||||
eventTag: 'edit',
|
||||
permission: ['PROJECT_API_DEFINITION_MOCK:READ+UPDATE'],
|
||||
},
|
||||
{
|
||||
label: 'mockManagement.batchDelete',
|
||||
eventTag: 'delete',
|
||||
danger: true,
|
||||
permission: ['PROJECT_API_DEFINITION_MOCK:READ+DELETE'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
@ -335,11 +353,13 @@
|
|||
eventTag: 'copyMock',
|
||||
label: t('mockManagement.copyMock'),
|
||||
danger: false,
|
||||
permission: ['PROJECT_API_DEFINITION_MOCK:READ+ADD'],
|
||||
},
|
||||
{
|
||||
eventTag: 'delete',
|
||||
label: t('common.delete'),
|
||||
danger: true,
|
||||
permission: ['PROJECT_API_DEFINITION_MOCK:READ+DELETE'],
|
||||
},
|
||||
];
|
||||
|
||||
|
|
Loading…
Reference in New Issue