Guest User

Untitled

a guest
Dec 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. public Response matchRedirect(String url, Integer statusCode, String urlRegex) {
  2. return
  3. given().
  4. redirects().follow(false).and().redirects().max(0).
  5. expect().
  6. statusCode(statusCode).
  7. header("Location", **matches**(urlRegex)).
  8. when().get(url);
  9. }
  10.  
  11. import org.hamcrest.BaseMatcher;
  12. import org.hamcrest.Description;
  13.  
  14. public class RegexMatcher extends BaseMatcher<Object>{
  15. private final String regex;
  16.  
  17. public RegexMatcher(String regex){
  18. this.regex = regex;
  19. }
  20.  
  21. public boolean matches(Object o){
  22. return ((String)o).matches(regex);
  23.  
  24. }
  25.  
  26. public void describeTo(Description description){
  27. description.appendText("matches regex=");
  28. }
  29.  
  30. public static RegexMatcher matches(String regex){
  31. return new RegexMatcher(regex);
  32. }
  33. }
  34.  
  35. import org.hamcrest.BaseMatcher;
  36. import org.hamcrest.Description;
  37. import org.hamcrest.Matcher;
  38.  
  39. public class CustomMatchers {
  40.  
  41. public static Matcher<String> matchesRegex(final String regex) {
  42. return new BaseMatcher<String>() {
  43.  
  44. public boolean matches(final Object item) {
  45. return ((String) item).matches(regex);
  46. }
  47.  
  48. public void describeTo(final Description description) {
  49. description.appendText("should match regex: " + regex);
  50. }
  51. };
  52. }
  53.  
  54. }
  55.  
  56. public Response matchRedirect(String url, Integer statusCode, String urlRegex) {
  57. return
  58. given().
  59. redirects().follow(false).and().redirects().max(0).
  60. expect().
  61. statusCode(statusCode).
  62. header("Location", CustomMatchers.matchesRegex(urlRegex)).
  63. when().get(url);
  64. }
Add Comment
Please, Sign In to add comment