|
18 | 18 |
|
19 | 19 | import org.junit.jupiter.api.Test; |
20 | 20 |
|
| 21 | +import com.microsoft.playwright.impl.PlaywrightImpl; |
| 22 | + |
21 | 23 | import java.io.OutputStreamWriter; |
22 | 24 | import java.io.Writer; |
23 | 25 | import java.util.HashMap; |
24 | 26 | import java.util.concurrent.ExecutionException; |
25 | 27 | import java.util.concurrent.Future; |
| 28 | +import java.util.regex.Pattern; |
26 | 29 |
|
27 | 30 | import static org.junit.jupiter.api.Assertions.*; |
28 | 31 |
|
@@ -139,4 +142,93 @@ void shouldNotFollowRedirectsWhenMaxRedirectsIsSetTo0InRouteFetch() { |
139 | 142 | page.navigate(server.PREFIX + "/foo"); |
140 | 143 | assertTrue(page.content().contains("hello")); |
141 | 144 | } |
| 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 | + } |
142 | 234 | } |
0 commit comments