/** * @return A new single matcher with the given path template that matches all HTTP methods ({@link * #isMatchAllMethods()} will return true). */ public static SingleMatcher match(String matchingPathTemplate) { return new SingleMatcher(matchingPathTemplate, Collections.emptyList(), true); }
/** * Convenience function to create a SingleMatcher from a pattern and collection of HttpMethods */ static Matcher match(String matchingPathTemplate, Collection<HttpMethod> matchingMethods) { return SingleMatcher.match(matchingPathTemplate, matchingMethods); }
@Test public void static_factory_with_path_arg_only_sets_values_as_expected() { // given String path = "/" + UUID.randomUUID().toString(); // when SingleMatcher matcher = SingleMatcher.match(path); // then assertThat(matcher.matchingPathTemplates(), is(Arrays.asList(path))); 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 path = "/" + UUID.randomUUID().toString(); Collection<HttpMethod> methods = Arrays.asList(HttpMethod.CONNECT, HttpMethod.PATCH); boolean matchAllMethods = false; // when SingleMatcher matcher = new SingleMatcher(path, methods, matchAllMethods); // then assertThat(matcher.matchingPathTemplates(), is(Arrays.asList(path))); assertThat(matcher.matchingMethods(), is(methods)); assertThat(matcher.isMatchAllMethods(), is(matchAllMethods)); }
@Test public void matchesMethod_returns_true_if_matchAllMethods_is_true() { // given SingleMatcher matcher = SingleMatcher.match("/foo"); // expect assertThat(matcher.matchesMethod(null), is(true)); }
@Test public void matchesPath_returns_empty_if_request_is_null() { // given SingleMatcher matcher = SingleMatcher.match("/some/path"); // expect assertThat(matcher.matchesPath(null), is(Optional.empty())); }
/** * {@inheritDoc} */ 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<HttpMethod> methodCollection = Arrays.asList(HttpMethod.GET, HttpMethod.PUT); // when SingleMatcher matcher = SingleMatcher.match(path, methodCollection); // then assertThat(matcher.matchingPathTemplates(), is(Arrays.asList(path))); assertThat(matcher.matchingMethods(), is(methodCollection)); assertThat(matcher.isMatchAllMethods(), is(false)); }
@Test public void matchesMethod_returns_false_if_request_is_null() { // given SingleMatcher matcher = SingleMatcher.match("/foo", HttpMethod.GET); // expect assertThat(matcher.matchesMethod(null), is(false)); }
@Test @UseDataProvider("knownData") public void matchesPath_works_as_expected_for_known_data(String matcherPathTemplate, String requestPath, Optional<String> expectedMatchValue) { // given SingleMatcher matcher = SingleMatcher.match(matcherPathTemplate); 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(); HttpMethod[] methodVarargs = new HttpMethod[]{HttpMethod.GET, HttpMethod.PUT}; // when SingleMatcher matcher = SingleMatcher.match(path, methodVarargs); // then assertThat(matcher.matchingPathTemplates(), is(Arrays.asList(path))); 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 SingleMatcher from a pattern and varargs of HttpMethod */ static Matcher match(String matchingPathTemplate, HttpMethod... matchingMethods) { return SingleMatcher.match(matchingPathTemplate, 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()); SingleMatcher matcher = SingleMatcher.match("/foo", 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 single matcher with the given path template that matches the given HTTP methods. */ public static SingleMatcher match(String matchingPathTemplate, 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(String) method."); } return new SingleMatcher(matchingPathTemplate, matchingMethods, false); }
/** * Convenience function to create a SingleMatcher from a pattern */ static Matcher match(String matchingPathTemplate) { return SingleMatcher.match(matchingPathTemplate); }
/** * @return A new single matcher with the given path template that matches the given HTTP methods. */ public static SingleMatcher match(String matchingPathTemplate, 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(String) method."); } return new SingleMatcher(matchingPathTemplate, Arrays.asList(matchingMethods), false); }
@Test(expected = IllegalArgumentException.class) public void static_factory_with_path_and_methods_varargs_throws_IllegalArgumentException_if_passed_empty_vararg_array() { // given String path = "/" + UUID.randomUUID().toString(); HttpMethod[] methodVarargs = new HttpMethod[0]; // expect SingleMatcher.match(path, methodVarargs); }
@Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_on_path_template_that_does_not_start_with_forward_slash() { // expect new SingleMatcher("whoops", Collections.emptyList(), false); }
@Test(expected = IllegalArgumentException.class) public void static_factory_with_path_and_methods_collection_throws_IllegalArgumentException_if_passed_null_collection() { // given String path = "/" + UUID.randomUUID().toString(); // expect SingleMatcher.match(path, (Collection<HttpMethod>) null); }
@Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_on_null_method_collection() { // expect new SingleMatcher("/some/path", null, false); }