From 7bab0159efeeaaef48df3ba7dfcbfc236c677b12 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Wed, 17 Apr 2024 18:24:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=E6=96=AD=E8=A8=80=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E6=9C=89=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResponseHeaderAssertionConverter.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java index 57508e9425..a30d9fd979 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java @@ -9,6 +9,8 @@ import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.ResponseAssertion; import org.apache.jorphan.collections.HashTree; +import java.util.regex.Pattern; + /** * @Author: jianxing * @CreateTime: 2023-12-27 21:01 @@ -50,14 +52,25 @@ public class ResponseHeaderAssertionConverter extends AssertionConverter StringUtils.join("\\b", header,": .*", expectedValue, ".*\\b"); - case NOT_CONTAINS -> StringUtils.join("\\b", header,": (?!.*", expectedValue, ").*\\b"); - case EQUALS -> StringUtils.join("\\b", header,": ",expectedValue, "\\b"); - case NOT_EQUALS -> StringUtils.join("\\b", header,": (?!", expectedValue,"\\b)\\d+"); - default -> expectedValue; + String regexTemplate = switch (msAssertionCondition) { + case NOT_CONTAINS, CONTAINS -> "((?:[\\r\\n]%key|^%key):.*%value)"; + case EQUALS, NOT_EQUALS -> "((?:[\\r\\n]%key|^%key):\\s*(?:%value[\\r\\n]|%value$))"; + default -> null; }; - assertion.setName(String.format("Response header %s %s %s", header, condition.toLowerCase().replace("_", ""), expectedValue)); + + String testString = expectedValue; + if (StringUtils.isNotEmpty(regexTemplate)) { + testString = regexTemplate + .replace("%key", Pattern.quote(header)) + .replace("%value", Pattern.quote(expectedValue)); + } + + if (StringUtils.startsWith(msAssertionCondition.name(), "NOT")) { + // 如果是 not 则结果取反 + assertion.setToNotType(); + } + + assertion.setName(String.format("Response header %s %s %s", header, condition.toLowerCase().replace("_", " "), expectedValue)); assertion.addTestString(testString); assertion.setToContainsType();