RequestMatchers { public static RequestMatcher hasQueryParameterNames(final String... expectedParamsNames) { return new RequestMatcher("matched query parameters names") { @Override protected boolean matchesSafely(RecordedRequest item) { try { URL mockUrl = new URL("http", "localhost", item.getPath()); List<QueryParam> actualParams = RestMockUtils.splitQuery(mockUrl); if (actualParams.size() == 0 || expectedParamsNames.length == 0) { return false; } List<String> expectedParamNamesList = varArgToList(expectedParamsNames); int matchedParams = 0; for (QueryParam actualParam : actualParams) { if (expectedParamNamesList.contains(actualParam.getKey())) { matchedParams++; } } return matchedParams == expectedParamsNames.length; } catch (MalformedURLException e) { RESTMockServer.getLogger().error("URL appears to be malformed with path: " + item.getPath()); return false; } catch (UnsupportedEncodingException e) { return false; } } }; } 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 shouldMatchProperSubsetOfQueryParametersNames() throws IOException { RecordedRequest recordedRequest = createRecordedRequest("/foo/?bar=bar&baz=baz&boo=boo"); RequestMatcher matcher = RequestMatchers.hasQueryParameterNames("bar", "baz"); assertTrue(matcher.matches(recordedRequest)); }
@Test public void shouldNotMatchInproperSubsetOfQueryParametersNames() throws IOException { RecordedRequest recordedRequest = createRecordedRequest("/foo/?bar=bar&baz=baz&boo=boo"); RequestMatcher matcher = RequestMatchers.hasQueryParameterNames("bar", "ban"); assertFalse(matcher.matches(recordedRequest)); }