RequestMatchers { public static RequestMatcher hasHeaderNames(final String... headerNames) { return new RequestMatcher("has headers: " + Arrays.toString(headerNames)) { @Override protected boolean matchesSafely(RecordedRequest item) { if (headerNames == null) { throw new IllegalArgumentException("You did not specify any header names"); } if (headerNames.length > 0 && item.getHeaders() == null) { return false; } for (String header : headerNames) { if (item.getHeader(header) == null) { return false; } } return true; } }; } private  RequestMatchers(); static RequestMatcher pathContains(final String urlPart); static RequestMatcher pathDoesNotContain(final String urlPart); static RequestMatcher pathMatchesRegex(final String pattern); static RequestMatcher pathEndsWith(final String urlPart); static RequestMatcher pathEndsWithIgnoringQueryParams(final String endOfUrlPath); static RequestMatcher pathStartsWith(final String urlPart); static RequestMatcher hasHeaderNames(final String... headerNames); static RequestMatcher hasQueryParameters(); static RequestMatcher hasQueryParameterNames(final String... expectedParamsNames); static RequestMatcher hasExactQueryParameters(final QueryParam... expectedParams); static RequestMatcher httpMethodIs(final String method); static RequestMatcher isGET(); static RequestMatcher isPOST(); static RequestMatcher isPATCH(); static RequestMatcher isDELETE(); static RequestMatcher isPUT(); static RequestMatcher isHEAD();  }
@Test public void hasHeaderNamesFailWhenNoHeaders() { RecordedRequest request = createRecordedRequest("/path"); RequestMatcher matcher = hasHeaderNames("header1"); assertFalse(matcher.matches(request)); }
@Test public void hasHeaderNamesSuccessWhenMatchesAll() { RecordedRequest request = createRecordedRequest("/path", "h1", "v1", "h2", "v2", "h3", "v3"); RequestMatcher matcher = hasHeaderNames("h1", "h2", "h3"); assertTrue(matcher.matches(request)); }
@Test public void hasHeaderNamesSuccessWhenMatchesAllAsSubset() { RecordedRequest request = createRecordedRequest("/path", "h1", "v1", "h2", "v2", "h3", "v3"); RequestMatcher matcher = hasHeaderNames("h1", "h2"); assertTrue(matcher.matches(request)); }