2323 */
2424
2525using System . Net ;
26+ using System . Text . RegularExpressions ;
2627using Microsoft . Playwright . Helpers ;
2728
2829namespace Microsoft . Playwright . Tests ;
@@ -32,32 +33,85 @@ public class InterceptionTests : PageTestEx
3233 [ PlaywrightTest ( "interception.spec.ts" , "should work with glob" ) ]
3334 public void ShouldWorkWithGlob ( )
3435 {
35- Assert . That ( "https://localhost:8080/foo.js" , Does . Match ( StringExtensions . GlobToRegex ( "**/*.js" ) ) ) ;
36- Assert . That ( "https://localhost:8080/foo.js" , Does . Not . Match ( StringExtensions . GlobToRegex ( "**/*.css" ) ) ) ;
37- Assert . That ( "https://localhost:8080/foo.js" , Does . Not . Match ( StringExtensions . GlobToRegex ( "*.js" ) ) ) ;
38- Assert . That ( "https://localhost:8080/foo.js" , Does . Match ( StringExtensions . GlobToRegex ( "https://**/*.js" ) ) ) ;
39- Assert . That ( "http://localhost:8080/simple/path.js" , Does . Match ( StringExtensions . GlobToRegex ( "http://localhost:8080/simple/path.js" ) ) ) ;
40- Assert . That ( "http://localhost:8080/Simple/path.js" , Does . Match ( StringExtensions . GlobToRegex ( "http://localhost:8080/?imple/path.js" ) ) ) ;
41- Assert . That ( "https://localhost:8080/a.js" , Does . Match ( StringExtensions . GlobToRegex ( "**/{a,b}.js" ) ) ) ;
42- Assert . That ( "https://localhost:8080/b.js" , Does . Match ( StringExtensions . GlobToRegex ( "**/{a,b}.js" ) ) ) ;
43- Assert . That ( "https://localhost:8080/c.js" , Does . Not . Match ( StringExtensions . GlobToRegex ( "**/{a,b}.js" ) ) ) ;
44- Assert . That ( "https://localhost:8080/c.jpg" , Does . Match ( StringExtensions . GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
45- Assert . That ( "https://localhost:8080/c.jpeg" , Does . Match ( StringExtensions . GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
46- Assert . That ( "https://localhost:8080/c.png" , Does . Match ( StringExtensions . GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
47- Assert . That ( "https://localhost:8080/c.css" , Does . Not . Match ( StringExtensions . GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
48- Assert . That ( "foo.js" , Does . Match ( StringExtensions . GlobToRegex ( "foo*" ) ) ) ;
49- Assert . That ( "foo/bar.js" , Does . Not . Match ( StringExtensions . GlobToRegex ( "foo*" ) ) ) ;
50- Assert . That ( "http://localhost:3000/signin-oidc/foo" , Does . Not . Match ( StringExtensions . GlobToRegex ( "http://localhost:3000/signin-oidc*" ) ) ) ;
51- Assert . That ( "http://localhost:3000/signin-oidcnice" , Does . Match ( StringExtensions . GlobToRegex ( "http://localhost:3000/signin-oidc*" ) ) ) ;
52-
53- Assert . That ( "http://mydomain:8080/blah/blah/three-columns/settings.html?id=settings-e3c58efe-02e9-44b0-97ac-dd138100cf7c&blah" , Does . Match ( StringExtensions . GlobToRegex ( "**/three-columns/settings.html?**id=[a-z]**" ) ) ) ;
54-
55- Assert . AreEqual ( "^\\ ?$" , StringExtensions . GlobToRegex ( "\\ ?" ) ) ;
56- Assert . AreEqual ( "^\\ \\ $" , StringExtensions . GlobToRegex ( "\\ " ) ) ;
57- Assert . AreEqual ( "^\\ \\ $" , StringExtensions . GlobToRegex ( "\\ \\ " ) ) ;
58- Assert . AreEqual ( "^\\ [$" , StringExtensions . GlobToRegex ( "\\ [" ) ) ;
59- Assert . AreEqual ( "^[a-z]$" , StringExtensions . GlobToRegex ( "[a-z]" ) ) ;
60- Assert . AreEqual ( @"^\$\^\+\.\*\(\)\|\?\{\}\[\]$" , StringExtensions . GlobToRegex ( "$^+.\\ *()|\\ ?\\ {\\ }\\ [\\ ]" ) ) ;
36+ Regex GlobToRegex ( string glob )
37+ {
38+ return new Regex ( URLMatch . GlobToRegexPattern ( glob ) ) ;
39+ }
40+
41+ bool URLMatches ( string baseURL , string url , string glob )
42+ {
43+ return new URLMatch ( )
44+ {
45+ baseURL = baseURL ,
46+ glob = glob ,
47+ } . Match ( url ) ;
48+ }
49+
50+ Assert . That ( "https://localhost:8080/foo.js" , Does . Match ( GlobToRegex ( "**/*.js" ) ) ) ;
51+ Assert . That ( "https://localhost:8080/foo.js" , Does . Not . Match ( GlobToRegex ( "**/*.css" ) ) ) ;
52+ Assert . That ( "https://localhost:8080/foo.js" , Does . Not . Match ( GlobToRegex ( "*.js" ) ) ) ;
53+ Assert . That ( "https://localhost:8080/foo.js" , Does . Match ( GlobToRegex ( "https://**/*.js" ) ) ) ;
54+ Assert . That ( "http://localhost:8080/simple/path.js" , Does . Match ( GlobToRegex ( "http://localhost:8080/simple/path.js" ) ) ) ;
55+ Assert . That ( "https://localhost:8080/a.js" , Does . Match ( GlobToRegex ( "**/{a,b}.js" ) ) ) ;
56+ Assert . That ( "https://localhost:8080/b.js" , Does . Match ( GlobToRegex ( "**/{a,b}.js" ) ) ) ;
57+ Assert . That ( "https://localhost:8080/c.js" , Does . Not . Match ( GlobToRegex ( "**/{a,b}.js" ) ) ) ;
58+ Assert . That ( "https://localhost:8080/c.jpg" , Does . Match ( GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
59+ Assert . That ( "https://localhost:8080/c.jpeg" , Does . Match ( GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
60+ Assert . That ( "https://localhost:8080/c.png" , Does . Match ( GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
61+ Assert . That ( "https://localhost:8080/c.css" , Does . Not . Match ( GlobToRegex ( "**/*.{png,jpg,jpeg}" ) ) ) ;
62+ Assert . That ( "foo.js" , Does . Match ( GlobToRegex ( "foo*" ) ) ) ;
63+ Assert . That ( "foo/bar.js" , Does . Not . Match ( GlobToRegex ( "foo*" ) ) ) ;
64+ Assert . That ( "http://localhost:3000/signin-oidc/foo" , Does . Not . Match ( GlobToRegex ( "http://localhost:3000/signin-oidc*" ) ) ) ;
65+ Assert . That ( "http://localhost:3000/signin-oidcnice" , Does . Match ( GlobToRegex ( "http://localhost:3000/signin-oidc*" ) ) ) ;
66+
67+ // range [] is NOT supported
68+ Assert . That ( "http://example.com/api/v[0-9]" , Does . Match ( GlobToRegex ( "**/api/v[0-9]" ) ) ) ;
69+ Assert . That ( "http://example.com/api/version" , Does . Not . Match ( GlobToRegex ( "**/api/v[0-9]" ) ) ) ;
70+
71+ // query params
72+ Assert . That ( "http://example.com/api?param" , Does . Match ( GlobToRegex ( "**/api\\ ?param" ) ) ) ;
73+ Assert . That ( "http://example.com/api-param" , Does . Not . Match ( GlobToRegex ( "**/api\\ ?param" ) ) ) ;
74+ Assert . That ( "http://mydomain:8080/blah/blah/three-columns/settings.html?id=settings-e3c58efe-02e9-44b0-97ac-dd138100cf7c&blah" , Does . Match ( GlobToRegex ( "**/three-columns/settings.html\\ ?**id=settings-**" ) ) ) ;
75+
76+ Assert . AreEqual ( "^\\ ?$" , URLMatch . GlobToRegexPattern ( "\\ ?" ) ) ;
77+ Assert . AreEqual ( "^\\ \\ $" , URLMatch . GlobToRegexPattern ( "\\ " ) ) ;
78+ Assert . AreEqual ( "^\\ \\ $" , URLMatch . GlobToRegexPattern ( "\\ \\ " ) ) ;
79+ Assert . AreEqual ( "^\\ [$" , URLMatch . GlobToRegexPattern ( "\\ [" ) ) ;
80+ Assert . AreEqual ( "^\\ [a-z\\ ]$" , URLMatch . GlobToRegexPattern ( "[a-z]" ) ) ;
81+ Assert . AreEqual ( @"^\$\^\+\.\*\(\)\|\?\{\}\[\]$" , URLMatch . GlobToRegexPattern ( "$^+.\\ *()|\\ ?\\ {\\ }\\ [\\ ]" ) ) ;
82+
83+ Assert . True ( URLMatches ( null , "http://playwright.dev/" , "http://playwright.dev" ) ) ;
84+ Assert . True ( URLMatches ( null , "http://playwright.dev/?a=b" , "http://playwright.dev?a=b" ) ) ;
85+ Assert . True ( URLMatches ( null , "http://playwright.dev/" , "h*://playwright.dev" ) ) ;
86+ Assert . True ( URLMatches ( null , "http://api.playwright.dev/?x=y" , "http://*.playwright.dev?x=y" ) ) ;
87+ Assert . True ( URLMatches ( null , "http://playwright.dev/foo/bar" , "**/foo/**" ) ) ;
88+ Assert . True ( URLMatches ( "http://playwright.dev" , "http://playwright.dev/?x=y" , "?x=y" ) ) ;
89+ Assert . True ( URLMatches ( "http://playwright.dev/foo/" , "http://playwright.dev/foo/bar?x=y" , "./bar?x=y" ) ) ;
90+
91+ // This is not supported, we treat ? as a query separator.
92+ Assert . That ( "http://localhost:8080/Simple/path.js" , Does . Not . Match ( GlobToRegex ( "http://localhost:8080/?imple/path.js" ) ) ) ;
93+ Assert . False ( URLMatches ( null , "http://playwright.dev/" , "http://playwright.?ev" ) ) ;
94+ Assert . True ( URLMatches ( null , "http://playwright./?ev" , "http://playwright.?ev" ) ) ;
95+ Assert . False ( URLMatches ( null , "http://playwright.dev/foo" , "http://playwright.dev/f??" ) ) ;
96+ Assert . True ( URLMatches ( null , "http://playwright.dev/f??" , "http://playwright.dev/f??" ) ) ;
97+ Assert . True ( URLMatches ( null , "http://playwright.dev/?x=y" , "http://playwright.dev\\ ?x=y" ) ) ;
98+ Assert . True ( URLMatches ( null , "http://playwright.dev/?x=y" , "http://playwright.dev/\\ ?x=y" ) ) ;
99+ Assert . True ( URLMatches ( "http://playwright.dev/foo" , "http://playwright.dev/foo?bar" , "?bar" ) ) ;
100+ Assert . True ( URLMatches ( "http://playwright.dev/foo" , "http://playwright.dev/foo?bar" , "\\ \\ ?bar" ) ) ;
101+ Assert . True ( URLMatches ( "http://first.host/" , "http://second.host/foo" , "**/foo" ) ) ;
102+ Assert . True ( URLMatches ( "http://playwright.dev/" , "http://localhost/" , "*//localhost/" ) ) ;
103+ }
104+
105+ [ PlaywrightTest ( "interception.spec.ts" , "should intercept by glob" ) ]
106+ public async Task ShouldInterceptByGlob ( )
107+ {
108+ await Page . GotoAsync ( Server . EmptyPage ) ;
109+ await Page . RouteAsync ( "http://localhos**?*oo" , ( route ) =>
110+ {
111+ return route . FulfillAsync ( new ( ) { Status = ( int ) HttpStatusCode . OK , Body = "intercepted" } ) ;
112+ } ) ;
113+ var result = await Page . EvaluateAsync < string > ( "url => fetch(url).then(r => r.text())" , Server . Prefix + "/?foo" ) ;
114+ Assert . AreEqual ( "intercepted" , result ) ;
61115 }
62116
63117 [ PlaywrightTest ( "interception.spec.ts" , "should work with ignoreHTTPSErrors" ) ]
0 commit comments