/** * 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 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())); }
@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)); }
@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)); }
/** * 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 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 */ static Matcher match(String matchingPathTemplate) { return SingleMatcher.match(matchingPathTemplate); }
@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)); } }
@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 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 static_factory_with_path_and_methods_varargs_throws_IllegalArgumentException_if_passed_null_vararg_array() { // given String path = "/" + UUID.randomUUID().toString(); // expect SingleMatcher.match(path, (HttpMethod[]) null); }
@Test(expected = IllegalArgumentException.class) public void static_factory_with_path_and_methods_collection_throws_IllegalArgumentException_if_passed_empty_collection() { // given String path = "/" + UUID.randomUUID().toString(); Collection<HttpMethod> methodCollection = Collections.emptyList(); // expect SingleMatcher.match(path, methodCollection); }