Skip to content

Commit 9d46675

Browse files
committed
port 'should work with glob'
1 parent 1e9ba55 commit 9d46675

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import static com.microsoft.playwright.impl.Serialization.gson;
2727

28-
class LocalUtils extends ChannelOwner {
28+
public class LocalUtils extends ChannelOwner {
2929
LocalUtils(ChannelOwner parent, String type, String guid, JsonObject initializer) {
3030
super(parent, type, guid, initializer);
3131
markAsInternalType();
@@ -61,14 +61,15 @@ String tracingStarted(String tracesDir, String traceName) {
6161
return json.get("stacksId").getAsString();
6262
}
6363

64-
Pattern globToRegex(String glob, String baseURL, boolean webSocketUrl) {
64+
public Pattern globToRegex(String glob, String baseURL, boolean webSocketUrl) {
6565
JsonObject params = new JsonObject();
6666
params.addProperty("glob", glob);
6767
if (baseURL != null) {
6868
params.addProperty("baseURL", baseURL);
6969
}
7070
params.addProperty("webSocketUrl", webSocketUrl);
7171
JsonObject json = connection.localUtils().sendMessage("globToRegex", params).getAsJsonObject();
72-
return Pattern.compile(json.get("regex").getAsString());
72+
String regex = json.get("regex").getAsString();
73+
return Pattern.compile(regex);
7374
}
7475
}

playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ void unregisterSelectors() {
9090
sharedSelectors.removeChannel(selectors);
9191
}
9292

93+
public LocalUtils localUtils() {
94+
return connection.localUtils;
95+
}
96+
9397
public JsonArray deviceDescriptors() {
94-
return connection.localUtils.deviceDescriptors();
98+
return localUtils().deviceDescriptors();
9599
}
96100

97101
@Override

playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import com.microsoft.playwright.impl.PlaywrightImpl;
22+
2123
import java.io.OutputStreamWriter;
2224
import java.io.Writer;
2325
import java.util.HashMap;
2426
import java.util.concurrent.ExecutionException;
2527
import java.util.concurrent.Future;
28+
import java.util.regex.Pattern;
2629

2730
import static org.junit.jupiter.api.Assertions.*;
2831

@@ -139,4 +142,93 @@ void shouldNotFollowRedirectsWhenMaxRedirectsIsSetTo0InRouteFetch() {
139142
page.navigate(server.PREFIX + "/foo");
140143
assertTrue(page.content().contains("hello"));
141144
}
145+
146+
@Test
147+
void shouldWorkWithGlob() {
148+
assertTrue(globToRegex("**/*.js").matcher("https://localhost:8080/foo.js").find());
149+
assertFalse(globToRegex("**/*.css").matcher("https://localhost:8080/foo.js").find());
150+
assertFalse(globToRegex("*.js").matcher("https://localhost:8080/foo.js").find());
151+
assertTrue(globToRegex("https://**/*.js").matcher("https://localhost:8080/foo.js").find());
152+
assertTrue(globToRegex("http://localhost:8080/simple/path.js").matcher("http://localhost:8080/simple/path.js").find());
153+
assertTrue(globToRegex("**/{a,b}.js").matcher("https://localhost:8080/a.js").find());
154+
assertTrue(globToRegex("**/{a,b}.js").matcher("https://localhost:8080/b.js").find());
155+
assertFalse(globToRegex("**/{a,b}.js").matcher("https://localhost:8080/c.js").find());
156+
157+
assertTrue(globToRegex("**/*.{png,jpg,jpeg}").matcher("https://localhost:8080/c.jpg").find());
158+
assertTrue(globToRegex("**/*.{png,jpg,jpeg}").matcher("https://localhost:8080/c.jpeg").find());
159+
assertTrue(globToRegex("**/*.{png,jpg,jpeg}").matcher("https://localhost:8080/c.png").find());
160+
assertFalse(globToRegex("**/*.{png,jpg,jpeg}").matcher("https://localhost:8080/c.css").find());
161+
assertTrue(globToRegex("foo*").matcher("foo.js").find());
162+
assertFalse(globToRegex("foo*").matcher("foo/bar.js").find());
163+
assertFalse(globToRegex("http://localhost:3000/signin-oidc*").matcher("http://localhost:3000/signin-oidc/foo").find());
164+
assertTrue(globToRegex("http://localhost:3000/signin-oidc*").matcher("http://localhost:3000/signin-oidcnice").find());
165+
166+
// range [] is NOT supported
167+
assertTrue(globToRegex("**/api/v[0-9]").matcher("http://example.com/api/v[0-9]").find());
168+
assertFalse(globToRegex("**/api/v[0-9]").matcher("http://example.com/api/version").find());
169+
170+
// query params
171+
assertTrue(globToRegex("**/api\\?param").matcher("http://example.com/api?param").find());
172+
assertFalse(globToRegex("**/api\\?param").matcher("http://example.com/api-param").find());
173+
assertTrue(globToRegex("**/three-columns/settings.html\\?**id=settings-**").matcher("http://mydomain:8080/blah/blah/three-columns/settings.html?id=settings-e3c58efe-02e9-44b0-97ac-dd138100cf7c&blah").find());
174+
175+
assertEquals("^\\?$", globToRegex("\\?").pattern());
176+
assertEquals("^\\\\$", globToRegex("\\").pattern());
177+
assertEquals("^\\\\$", globToRegex("\\\\").pattern());
178+
assertEquals("^\\[$", globToRegex("\\[").pattern());
179+
assertEquals("^\\[a-z\\]$", globToRegex("[a-z]").pattern());
180+
assertEquals("^\\$\\^\\+\\.\\*\\(\\)\\|\\?\\{\\}\\[\\]$", globToRegex("$^+.\\*()|\\?\\{\\}\\[\\]").pattern());
181+
182+
183+
assertTrue(urlMatches(null, "http://playwright.dev/", "http://playwright.dev"));
184+
assertTrue(urlMatches(null, "http://playwright.dev/?a=b", "http://playwright.dev?a=b"));
185+
assertTrue(urlMatches(null, "http://playwright.dev/", "h*://playwright.dev"));
186+
assertTrue(urlMatches(null, "http://api.playwright.dev/?x=y", "http://*.playwright.dev?x=y"));
187+
assertTrue(urlMatches(null, "http://playwright.dev/foo/bar", "**/foo/**"));
188+
assertTrue(urlMatches("http://playwright.dev", "http://playwright.dev/?x=y", "?x=y"));
189+
assertTrue(urlMatches("http://playwright.dev/foo/", "http://playwright.dev/foo/bar?x=y", "./bar?x=y"));
190+
191+
// This is not supported, we treat ? as a query separator.
192+
assertFalse(urlMatches(null, "http://localhost:8080/Simple/path.js", "http://localhost:8080/?imple/path.js"));
193+
assertFalse(urlMatches(null, "http://playwright.dev/", "http://playwright.?ev"));
194+
assertTrue(urlMatches(null, "http://playwright./?ev", "http://playwright.?ev"));
195+
assertFalse(urlMatches(null, "http://playwright.dev/foo", "http://playwright.dev/f??"));
196+
assertTrue(urlMatches(null, "http://playwright.dev/f??", "http://playwright.dev/f??"));
197+
assertTrue(urlMatches(null, "http://playwright.dev/?x=y", "http://playwright.dev\\\\?x=y"));
198+
assertTrue(urlMatches(null, "http://playwright.dev/?x=y", "http://playwright.dev/\\\\?x=y"));
199+
assertTrue(urlMatches("http://playwright.dev/foo", "http://playwright.dev/foo?bar", "?bar"));
200+
assertTrue(urlMatches("http://playwright.dev/foo", "http://playwright.dev/foo?bar", "\\\\?bar"));
201+
assertTrue(urlMatches("http://first.host/", "http://second.host/foo", "**/foo"));
202+
assertTrue(urlMatches("http://playwright.dev/", "http://localhost/", "*//localhost/"));
203+
}
204+
205+
Pattern globToRegex(String glob) {
206+
return globToRegex(glob, null, false);
207+
}
208+
209+
Pattern globToRegex(String glob, String baseURL, boolean webSocketUrl) {
210+
return ((PlaywrightImpl) playwright).localUtils().globToRegex(glob, baseURL, webSocketUrl);
211+
}
212+
213+
boolean urlMatches(String baseURL, String urlString, Object match) {
214+
return urlMatches(baseURL, urlString, match, false);
215+
}
216+
217+
boolean urlMatches(String baseURL, String urlString, Object match, boolean webSocketUrl) {
218+
if (match == null) {
219+
return true;
220+
}
221+
222+
if (match instanceof String) {
223+
String glob = (String) match;
224+
if (glob.isEmpty()) {
225+
return true;
226+
}
227+
228+
match = globToRegex(glob, baseURL, webSocketUrl);
229+
}
230+
231+
Pattern pattern = (Pattern) match;
232+
return pattern.matcher(urlString).find();
233+
}
142234
}

0 commit comments

Comments
 (0)