我正在使用 testng 进行测试 url 重定向的放心测试。我想匹配 header 位置响应以匹配正则表达式。
我正在尝试创建以下方法,但我没有找到任何使用 Hamcrest 匹配器的正则表达式匹配器。我想使用方法中使用的一些方法,如匹配(或任何其他选项)。
public Response matchRedirect(String url, Integer statusCode, String urlRegex) {
return
given().
redirects().follow(false).and().redirects().max(0).
expect().
statusCode(statusCode).
header("Location", **matches**(urlRegex)).
when().get(url);
}
最佳答案
我使用了 https://piotrga.wordpress.com/2009/03/27/hamcrest-regex-matcher/ 中的类与我的方法一起使用。
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
public class RegexMatcher extends BaseMatcher<Object>{
private final String regex;
public RegexMatcher(String regex){
this.regex = regex;
}
public boolean matches(Object o){
return ((String)o).matches(regex);
}
public void describeTo(Description description){
description.appendText("matches regex=");
}
public static RegexMatcher matches(String regex){
return new RegexMatcher(regex);
}
}
关于java - 如何使用 rest-assured 将 header 位置响应与正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298990/