/** * Convenience function to create a MultiMatcher from a Collection of patterns and collection of HttpMethods. * First match is used so and ordered collection may be needed if a path could match a more general pattern. */ static Matcher multiMatch(Collection<String> matchingPathTemplates, Collection<HttpMethod> matchingMethods) { return MultiMatcher.match(matchingPathTemplates, matchingMethods); } }
/** * @return A new multi-matcher with the given path template that matches all HTTP methods ({@link * #isMatchAllMethods()} will return true). */ public static MultiMatcher match(Collection<String> matchingPathTemplates) { return new MultiMatcher(matchingPathTemplates, Collections.emptyList(), true); }
@Test public void static_factory_with_path_arg_only_sets_values_as_expected() { // given String path = "/" + UUID.randomUUID().toString(); Collection<String> paths = new ArrayList<String>() {{ add(path); }}; // when MultiMatcher matcher = MultiMatcher.match(paths); // then assertThat(matcher.matchingPathTemplates(), is(paths)); assertThat(matcher.matchingMethods(), notNullValue()); assertThat(matcher.matchingMethods().isEmpty(), is(true)); assertThat(matcher.isMatchAllMethods(), is(true)); }
@SuppressWarnings("ConstantConditions") @Test public void constructor_sets_fields_as_expected() { // given String path1 = "/" + UUID.randomUUID().toString(); String path2 = "/" + UUID.randomUUID().toString(); Collection<String> paths = new ArrayList<String>() {{ add(path1); add(path2); }}; Collection<HttpMethod> methods = Arrays.asList(HttpMethod.CONNECT, HttpMethod.PATCH); boolean matchAllMethods = false; // when MultiMatcher matcher = new MultiMatcher(paths, methods, matchAllMethods); // then assertThat(matcher.matchingPathTemplates(), is(paths)); assertThat(matcher.matchingMethods(), is(methods)); assertThat(matcher.isMatchAllMethods(), is(matchAllMethods)); }
@Test public void matchesMethod_returns_false_if_request_is_null() { // given MultiMatcher matcher = MultiMatcher.match(Collections.singleton("/foo"), HttpMethod.GET); // expect assertThat(matcher.matchesMethod(null), is(false)); }
@Test public void matchesPath_returns_empty_if_request_is_null() { // given String path = "/some/path"; Collection<String> paths = new ArrayList<String>() {{ add(path); }}; MultiMatcher matcher = MultiMatcher.match(paths); // expect assertThat(matcher.matchesPath(null), is(Optional.empty())); }
/** * {@inheritDoc} */ @Override public boolean matchesMethod(RequestInfo<?> request) { if (matchAllMethods) return true; //noinspection SimplifiableIfStatement if (request == null || request.getMethod() == null) return false; return matchingMethods().contains(request.getMethod()); }
@Test public void static_factory_with_path_and_methods_collection_sets_values_as_expected() { // given String path = "/" + UUID.randomUUID().toString(); Collection<String> paths = new ArrayList<String>() {{ add(path); }}; Collection<HttpMethod> methodCollection = Arrays.asList(HttpMethod.GET, HttpMethod.PUT); // when MultiMatcher matcher = MultiMatcher.match(paths, methodCollection); // then assertThat(matcher.matchingPathTemplates(), is(paths)); assertThat(matcher.matchingMethods(), is(methodCollection)); assertThat(matcher.isMatchAllMethods(), is(false)); }
@Test public void matchesMethod_returns_true_if_matchAllMethods_is_true() { // given MultiMatcher matcher = MultiMatcher.match(Collections.singleton("/foo")); // expect assertThat(matcher.matchesMethod(null), is(true)); }
@Test @UseDataProvider("knownData") public void matchesPath_works_as_expected_for_known_data(List<String> matcherPathTemplates, String requestPath, Optional<String> expectedMatchValue) { // given MultiMatcher matcher = MultiMatcher.match(matcherPathTemplates); RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "path", requestPath); // expect assertThat(matcher.matchesPath(requestInfo), is(expectedMatchValue)); }
@Test public void static_factory_with_path_and_methods_varargs_sets_values_as_expected() { // given String path = "/" + UUID.randomUUID().toString(); Collection<String> paths = new ArrayList<String>() {{ add(path); }}; HttpMethod[] methodVarargs = new HttpMethod[]{HttpMethod.GET, HttpMethod.PUT}; // when MultiMatcher matcher = MultiMatcher.match(paths, methodVarargs); // then assertThat(matcher.matchingPathTemplates(), is(paths)); assertThat(matcher.matchingMethods(), notNullValue()); assertThat(matcher.matchingMethods().size(), is(methodVarargs.length)); for (HttpMethod expectedMethod : methodVarargs) { assertThat(matcher.matchingMethods().contains(expectedMethod), is(true)); } assertThat(matcher.isMatchAllMethods(), is(false)); }
/** * Convenience function to create a MultiMatcher from a Collection of patterns and varargs of HttpMethod. * First match is used so and ordered collection may be needed if a path could match a more general pattern. */ static Matcher multiMatch(Collection<String> matchingPathTemplates, HttpMethod... matchingMethods) { return MultiMatcher.match(matchingPathTemplates, matchingMethods); }
@Test @DataProvider(value = { "GET,POST | GET | true", "GET,POST | POST | true", "GET,POST | PUT | false", "GET,POST | null | false" }, splitBy = "\\|") public void matchesMethod_works_as_expected_for_known_data(String matcherMethodStrings, String requestMethodString, boolean expectedMatchValue) { // given List<HttpMethod> matcherMethods = Arrays.asList(matcherMethodStrings.split(",")).stream().map(HttpMethod::valueOf).collect(Collectors.toList()); Collection<String> paths = new ArrayList<String>() {{ add("/foo"); }}; MultiMatcher matcher = MultiMatcher.match(paths, matcherMethods); RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); if (requestMethodString == null) Whitebox.setInternalState(requestInfo, "method", null); else Whitebox.setInternalState(requestInfo, "method", HttpMethod.valueOf(requestMethodString)); // expect assertThat(matcher.matchesMethod(requestInfo), is(expectedMatchValue)); } }
/** * @return A new multi-matcher with the given path templates that match the given HTTP methods. */ public static MultiMatcher match(Collection<String> matchingPathTemplates, Collection<HttpMethod> matchingMethods) { if (matchingMethods == null || matchingMethods.isEmpty()) { throw new IllegalArgumentException("matchingMethods cannot be null or empty. If you want to match all " + "methods use the single-arg match(Collection<String>) method."); } return new MultiMatcher(matchingPathTemplates, matchingMethods, false); }
/** * Convenience function to create a MultiMatcher from a Collection of patterns. First match is used so and ordered * collection may be needed if a path could match a more general pattern. */ static Matcher multiMatch(Collection<String> matchingPathTemplates) { return MultiMatcher.match(matchingPathTemplates); }
/** * @return A new multi-matcher with the given path templates that match the given HTTP methods. */ public static MultiMatcher match(Collection<String> matchingPathTemplates, HttpMethod... matchingMethods) { if (matchingMethods == null || matchingMethods.length == 0) { throw new IllegalArgumentException("matchingMethods cannot be null or empty. If you want to match all " + "methods use the single-arg match(Collection<String>) method."); } return new MultiMatcher(matchingPathTemplates, Arrays.asList(matchingMethods), false); }
@Override public Matcher requestMatcher() { return MultiMatcher.match(Arrays.asList(INTERNAL_MATCHING_PATH, EXTERNAL_MATCHING_PATH)); } }
@DataProvider(value = { "true", "false" }) @Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_on_null_or_empty_path_template(boolean useNull) { // given Collection<String> pathTemplates = (useNull) ? null : Collections.emptyList(); // expect new MultiMatcher(pathTemplates, Collections.emptyList(), false); }
@Override public Matcher requestMatcher() { return MultiMatcher.match(Arrays.asList(INTERNAL_MATCHING_PATH, NON_INTERNAL_MATCHING_PATH)); }
@Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_on_path_template_that_does_not_start_with_forward_slash() { Collection<String> paths = new ArrayList<String>() {{ add("whoops"); }}; // expect new MultiMatcher(paths, Collections.emptyList(), false); }