public SingleMatcher(String matchingPathTemplate, Collection<HttpMethod> matchingMethods, boolean matchAllMethods) { // If the path template doesn't start with a forward slash it has no hope of ever matching any incoming request. if (matchingPathTemplate == null || !matchingPathTemplate.startsWith("/")) { throw new IllegalArgumentException("matchingPathTemplate cannot be null and must start with a forward " + "slash '/'"); } if (matchingMethods == null) throw new IllegalArgumentException("matchingMethods cannot be null"); matchingPathTemplate = MatcherUtil.stripEndSlash(matchingPathTemplate); this.matchingMethods = matchingMethods; this.matchingPathTemplate = matchingPathTemplate; this.matchingPathTemplates = Collections.singletonList(matchingPathTemplate); this.matchAllMethods = matchAllMethods; this.cachedMatchesPathResponse = Optional.of(matchingPathTemplate); }
@Test public void code_coverage_hoops() { // jump! new MatcherUtil(); }
/** * @return Returns the first pattern found in the collection that matches the given request if one exists. Order of * paths can be significant so when creating a MultiMatcher use an ordered collection if some path may match * multiple patterns. */ @Override public Optional<String> matchesPath(RequestInfo<?> request) { if (request == null || request.getPath() == null) return Optional.empty(); // Ignore trailing slashes on the actual path. String path = MatcherUtil.stripEndSlash(request.getPath()); return matchingPathTemplates .stream() // Ignore trailing slashes on the template. .filter(pathTemplate -> pathParamExtractor.match(pathTemplate, path)) .findFirst(); }
/** * {@inheritDoc} */ public Optional<String> matchesPath(RequestInfo<?> request) { if (request == null || request.getPath() == null) return Optional.empty(); // Ignore trailing slashes on actual path. String path = MatcherUtil.stripEndSlash(request.getPath()); if (pathParamExtractor.match(matchingPathTemplate, path)) { return cachedMatchesPathResponse; } else { return Optional.empty(); } }
@Test public void stripEndSlash_should_strip_an_end_slash() { String path = "/something/ending/in/slash/"; assertThat(MatcherUtil.stripEndSlash(path), is("/something/ending/in/slash")); } }