You can create customer matcher like below (Example only):
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
public class CustomMatchers {
public static Matcher<String> matchesRegex(final String regex) {
return new BaseMatcher<String>() {
public boolean matches(final Object item) {
return ((String) item).matches(regex);
}
public void describeTo(final Description description) {
description.appendText("should match regex: " + regex);
}
};
}
}
and then check header match your regex:
public Response matchRedirect(String url, Integer statusCode, String urlRegex) {
return
given().
redirects().follow(false).and().redirects().max(0).
expect().
statusCode(statusCode).
header("Location", CustomMatchers.matchesRegex(urlRegex)).
when().get(url);
}
You can check https://piotrga.wordpress.com/2009/03/27/hamcrest-regex-matcher/ t for more information