Skip to content

Commit

Permalink
chore: refactor UrlMatcher (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Dec 28, 2024
1 parent 42d0203 commit 6b621ce
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand All @@ -30,20 +32,14 @@
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags;

class UrlMatcher {
final Object rawSource;
private final Predicate<String> predicate;

private static Predicate<String> toPredicate(Pattern pattern) {
return s -> pattern.matcher(s).find();
}

static UrlMatcher any() {
return new UrlMatcher((Object) null, null);
}
private final URL baseURL;
public final String glob;
public final Pattern pattern;
public final Predicate<String> predicate;

static UrlMatcher forOneOf(URL baseUrl, Object object) {
if (object == null) {
return UrlMatcher.any();
return new UrlMatcher(null, null, null, null);
}
if (object instanceof String) {
return new UrlMatcher(baseUrl, (String) object);
Expand All @@ -68,50 +64,75 @@ static String resolveUrl(URL baseUrl, String spec) {
}
}

UrlMatcher(URL base, String url) {
this(url, toPredicate(Pattern.compile(globToRegex(resolveUrl(base, url)))).or(s -> url == null || url.equals(s)));
UrlMatcher(URL baseURL, String glob) {
this(baseURL, glob, null, null);
}

UrlMatcher(Pattern pattern) {
this(pattern, toPredicate(pattern));
this(null, null, pattern, null);
}

UrlMatcher(Predicate<String> predicate) {
this(predicate, predicate);
this(null, null, null, predicate);
}

private UrlMatcher(Object rawSource, Predicate<String> predicate) {
this.rawSource = rawSource;
private UrlMatcher(URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
this.baseURL = baseURL;
this.glob = glob;
this.pattern = pattern;
this.predicate = predicate;
}

boolean test(String value) {
return predicate == null || predicate.test(value);
if (pattern != null) {
return pattern.matcher(value).find();
}
if (predicate != null) {
return predicate.test(value);
}
if (glob != null) {
return Pattern.compile(globToRegex(resolveUrl(baseURL, glob))).matcher(value).find();
}
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UrlMatcher that = (UrlMatcher) o;
if (rawSource instanceof Pattern && that.rawSource instanceof Pattern) {
Pattern a = (Pattern) rawSource;
Pattern b = (Pattern) that.rawSource;
return a.pattern().equals(b.pattern()) && a.flags() == b.flags();
if (pattern != null) {
return that.pattern != null && pattern.pattern().equals(that.pattern.pattern()) && pattern.flags() == that.pattern.flags();
}
if (predicate != null) {
return predicate.equals(that.predicate);
}
if (glob != null) {
return glob.equals(that.glob);
}
return Objects.equals(rawSource, that.rawSource);
return that.pattern == null && that.predicate == null && that.glob == null;
}

@Override
public int hashCode() {
return Objects.hash(rawSource);
if (pattern != null) {
return pattern.hashCode();
}
if (predicate != null) {
return predicate.hashCode();
}
if (glob != null) {
return glob.hashCode();
}
return 0;
}

@Override
public String toString() {
if (rawSource == null)
return "<any>";
if (rawSource instanceof Predicate)
return "matching predicate";
return rawSource.toString();
if (pattern != null)
return String.format("<regex pattern=\"%s\" flags=\"%s\">", pattern.pattern(), toJsRegexFlags(pattern));
if (this.predicate != null)
return "<predicate>";
return String.format("<glob pattern=\"%s\">", glob);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,11 @@ static JsonObject interceptionPatterns(List<UrlMatcher> matchers) {
JsonArray jsonPatterns = new JsonArray();
for (UrlMatcher matcher: matchers) {
JsonObject jsonPattern = new JsonObject();
Object urlFilter = matcher.rawSource;
if (urlFilter instanceof String) {
jsonPattern.addProperty("glob", (String) urlFilter);
} else if (urlFilter instanceof Pattern) {
Pattern pattern = (Pattern) urlFilter;
jsonPattern.addProperty("regexSource", pattern.pattern());
jsonPattern.addProperty("regexFlags", toJsRegexFlags(pattern));
if (matcher.glob != null) {
jsonPattern.addProperty("glob", matcher.glob);
} else if (matcher.pattern != null) {
jsonPattern.addProperty("regexSource", matcher.pattern.pattern());
jsonPattern.addProperty("regexFlags", toJsRegexFlags(matcher.pattern));
} else {
// Match all requests.
jsonPattern.addProperty("glob", "**/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ void shouldUnroute() {
assertEquals(asList(1), intercepted);
}

@Test
void shouldUnrouteNonExistentPatternHandler() {
List<Integer> intercepted = new ArrayList<>();
page.route(Pattern.compile("empty.html"), route -> {
intercepted.add(1);
route.fallback();
});
page.unroute("**/*");
page.navigate(server.EMPTY_PAGE);
assertEquals(asList( 1), intercepted);
}

@Test
void shouldSupportQuestionMarkInGlobPattern() {
server.setRoute("/index", exchange -> {
Expand Down

0 comments on commit 6b621ce

Please sign in to comment.