}
@Test
- public void match_declared_web_services() {
+ public void match_declared_web_services_with_optional_suffix() {
initWebServiceEngine(
newWsUrl("api/issues", "search"),
newWsUrl("batch", "index"));
assertThat(underTest.doGetPattern().matches("/api/issues/search")).isTrue();
assertThat(underTest.doGetPattern().matches("/api/issues/search.protobuf")).isTrue();
assertThat(underTest.doGetPattern().matches("/batch/index")).isTrue();
+ assertThat(underTest.doGetPattern().matches("/batch/index.protobuf")).isTrue();
assertThat(underTest.doGetPattern().matches("/foo")).isFalse();
}
assertThat(underTest.doGetPattern().matches("/api/authentication/login")).isFalse();
}
+ @Test
+ public void does_not_match_servlet_filter_that_prefix_a_ws() {
+ initWebServiceEngine(
+ newWsUrl("api/foo", "action").setHandler(ServletFilterHandler.INSTANCE),
+ newWsUrl("api/foo", "action_2"));
+
+ assertThat(underTest.doGetPattern().matches("/api/foo/action")).isFalse();
+ assertThat(underTest.doGetPattern().matches("/api/foo/action_2")).isTrue();
+ }
+
@Test
public void does_not_match_api_properties_ws() {
initWebServiceEngine(newWsUrl("api/properties", "index"));
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
+import static org.apache.commons.lang.StringUtils.substringBeforeLast;
/**
* @since 3.1
/**
* Add inclusion patterns. Supported formats are:
* <ul>
- * <li>path prefixed by / and ended by *, for example "/api/foo/*", to match all paths "/api/foo" and "api/api/foo/something/else"</li>
+ * <li>path prefixed by / and ended by * or /*, for example "/api/foo/*", to match all paths "/api/foo" and "api/api/foo/something/else"</li>
+ * <li>path prefixed by / and ended by .*, for example "/api/foo.*", to match exact path "/api/foo" with any suffix like "/api/foo.protobuf"</li>
* <li>path prefixed by *, for example "*\/foo", to match all paths "/api/foo" and "something/else/foo"</li>
* <li>path with leading slash and no wildcard, for example "/api/foo", to match exact path "/api/foo"</li>
* </ul>
checkArgument(countStars == 1, "URL pattern accepts only zero or one wildcard character '*': %s", pattern);
if (pattern.charAt(0) == '/') {
checkArgument(pattern.endsWith(WILDCARD_CHAR), "URL pattern must end with wildcard character '*': %s", pattern);
- // remove the ending /* or *
- String path = pattern.replaceAll("/?\\*", "");
+ if (pattern.endsWith("/*")) {
+ String path = pattern.substring(0, pattern.length() - "/*".length());
+ return url -> url.startsWith(path);
+ }
+ if (pattern.endsWith(".*")) {
+ String path = pattern.substring(0, pattern.length() - ".*".length());
+ return url -> substringBeforeLast(url, ".").equals(path);
+ }
+ String path = pattern.substring(0, pattern.length() - "*".length());
return url -> url.startsWith(path);
}
checkArgument(pattern.startsWith(WILDCARD_CHAR), "URL pattern must start with wildcard character '*': %s", pattern);
assertThat(pattern.matches("/foo/index")).isTrue();
}
+ @Test
+ public void use_include_and_exclude_prefix() {
+ ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder()
+ .includes("/foo_2")
+ .excludes("/foo")
+ .build();
+ assertThat(pattern.matches("/")).isFalse();
+ assertThat(pattern.matches("/foo_2")).isTrue();
+ assertThat(pattern.matches("/foo")).isFalse();
+ }
+
@Test
public void exclude_pattern_has_higher_priority_than_include_pattern() {
ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder()