Skip to content

Commit 497b7b0

Browse files
authored
chore align route handling with upstream (#3057)
1 parent 7797511 commit 497b7b0

File tree

5 files changed

+147
-133
lines changed

5 files changed

+147
-133
lines changed

src/Playwright/Core/BrowserContext.cs

+54-52
Original file line numberDiff line numberDiff line change
@@ -487,28 +487,28 @@ public async Task<IPage> NewPageAsync()
487487
}
488488

489489
[MethodImpl(MethodImplOptions.NoInlining)]
490-
public Task RouteAsync(string url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
491-
=> RouteAsync(new Regex(CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
490+
public Task RouteAsync(string globMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
491+
=> RouteAsync(globMatch, null, null, handler, options);
492492

493493
[MethodImpl(MethodImplOptions.NoInlining)]
494-
public Task RouteAsync(string url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
495-
=> RouteAsync(new Regex(CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
494+
public Task RouteAsync(string globMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
495+
=> RouteAsync(globMatch, null, null, handler, options);
496496

497497
[MethodImpl(MethodImplOptions.NoInlining)]
498-
public Task RouteAsync(Regex url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
499-
=> RouteAsync(url, null, handler, options);
498+
public Task RouteAsync(Regex reMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
499+
=> RouteAsync(null, reMatch, null, handler, options);
500500

501501
[MethodImpl(MethodImplOptions.NoInlining)]
502-
public Task RouteAsync(Regex url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = default)
503-
=> RouteAsync(url, null, handler, options);
502+
public Task RouteAsync(Regex reMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
503+
=> RouteAsync(null, reMatch, null, handler, options);
504504

505505
[MethodImpl(MethodImplOptions.NoInlining)]
506-
public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
507-
=> RouteAsync(null, url, handler, options);
506+
public Task RouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
507+
=> RouteAsync(null, null, funcMatch, handler, options);
508508

509509
[MethodImpl(MethodImplOptions.NoInlining)]
510-
public Task RouteAsync(Func<string, bool> url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = default)
511-
=> RouteAsync(null, url, handler, options);
510+
public Task RouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
511+
=> RouteAsync(null, null, funcMatch, handler, options);
512512

513513
[MethodImpl(MethodImplOptions.NoInlining)]
514514
public Task SetExtraHTTPHeadersAsync(IEnumerable<KeyValuePair<string, string>> headers)
@@ -558,28 +558,28 @@ public async Task UnrouteAllAsync(BrowserContextUnrouteAllOptions options = defa
558558
}
559559

560560
[MethodImpl(MethodImplOptions.NoInlining)]
561-
public Task UnrouteAsync(string urlString, Action<IRoute> handler = default)
562-
=> UnrouteAsync(new Regex(CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
561+
public Task UnrouteAsync(string globMatch, Action<IRoute> handler)
562+
=> UnrouteAsync(globMatch, null, null, handler);
563563

564564
[MethodImpl(MethodImplOptions.NoInlining)]
565-
public Task UnrouteAsync(string urlString, Func<IRoute, Task> handler = null)
566-
=> UnrouteAsync(new Regex(CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
565+
public Task UnrouteAsync(string globMatch, Func<IRoute, Task> handler)
566+
=> UnrouteAsync(globMatch, null, null, handler);
567567

568568
[MethodImpl(MethodImplOptions.NoInlining)]
569-
public Task UnrouteAsync(Regex urlRegex, Action<IRoute> handler = default)
570-
=> UnrouteAsync(urlRegex, null, handler);
569+
public Task UnrouteAsync(Regex reMatch, Action<IRoute> handler)
570+
=> UnrouteAsync(null, reMatch, null, handler);
571571

572572
[MethodImpl(MethodImplOptions.NoInlining)]
573-
public Task UnrouteAsync(Regex urlRegex, Func<IRoute, Task> handler = default)
574-
=> UnrouteAsync(urlRegex, null, handler);
573+
public Task UnrouteAsync(Regex reMatch, Func<IRoute, Task> handler)
574+
=> UnrouteAsync(null, reMatch, null, handler);
575575

576576
[MethodImpl(MethodImplOptions.NoInlining)]
577-
public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler = default)
578-
=> UnrouteAsync(null, urlFunc, handler);
577+
public Task UnrouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler)
578+
=> UnrouteAsync(null, null, funcMatch, handler);
579579

580580
[MethodImpl(MethodImplOptions.NoInlining)]
581-
public Task UnrouteAsync(Func<string, bool> urlFunc, Func<IRoute, Task> handler = default)
582-
=> UnrouteAsync(null, urlFunc, handler);
581+
public Task UnrouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler)
582+
=> UnrouteAsync(null, null, funcMatch, handler);
583583

584584
internal string _effectiveCloseReason()
585585
{
@@ -674,9 +674,7 @@ internal async Task OnRouteAsync(Route route)
674674
{
675675
return;
676676
}
677-
var matches = routeHandler.Regex?.IsMatch(route.Request.Url) == true ||
678-
routeHandler.Function?.Invoke(route.Request.Url) == true;
679-
if (!matches)
677+
if (!routeHandler.Matches(route.Request.Url))
680678
{
681679
continue;
682680
}
@@ -731,11 +729,16 @@ internal string CombineUrlWithBase(string url)
731729
return URLMatch.JoinWithBaseURL(Options?.BaseURL, url);
732730
}
733731

734-
private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler, BrowserContextRouteOptions options)
732+
private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, BrowserContextRouteOptions options)
735733
=> RouteAsync(new()
736734
{
737-
Regex = urlRegex,
738-
Function = urlFunc,
735+
urlMatcher = new URLMatch()
736+
{
737+
glob = globMatch,
738+
re = reMatch,
739+
func = funcMatch,
740+
baseURL = Options.BaseURL,
741+
},
739742
Handler = handler,
740743
Times = options?.Times,
741744
});
@@ -746,23 +749,22 @@ private Task RouteAsync(RouteHandler setting)
746749
return UpdateInterceptionAsync();
747750
}
748751

749-
private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler = null)
750-
=> UnrouteAsync(new()
751-
{
752-
Function = urlFunc,
753-
Regex = urlRegex,
754-
Handler = handler,
755-
});
756-
757-
private Task UnrouteAsync(RouteHandler setting)
752+
private async Task UnrouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
758753
{
759-
var newRoutes = new List<RouteHandler>();
760-
newRoutes.AddRange(_routes.Where(r =>
761-
(setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||
762-
(setting.Function != null && r.Function != setting.Function) ||
763-
(setting.Handler != null && r.Handler != setting.Handler)));
764-
_routes = newRoutes;
765-
return UpdateInterceptionAsync();
754+
var removed = new List<RouteHandler>();
755+
var remaining = new List<RouteHandler>();
756+
foreach (var routeHandler in _routes)
757+
{
758+
if (routeHandler.urlMatcher.Equals(globMatch, reMatch, funcMatch, Options.BaseURL) && (handler == null || routeHandler.Handler == handler))
759+
{
760+
removed.Add(routeHandler);
761+
}
762+
else
763+
{
764+
remaining.Add(routeHandler);
765+
}
766+
}
767+
await UnrouteInternalAsync(removed, remaining, UnrouteBehavior.Default).ConfigureAwait(false);
766768
}
767769

768770
private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHandler> remaining, UnrouteBehavior? behavior)
@@ -773,7 +775,7 @@ private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHa
773775
{
774776
return;
775777
}
776-
var tasks = removed.Select(routeHandler => routeHandler.StopAsync((UnrouteBehavior)behavior));
778+
var tasks = removed.Select(routeHandler => routeHandler.StopAsync(behavior.Value));
777779
await Task.WhenAll(tasks).ConfigureAwait(false);
778780
}
779781

@@ -916,12 +918,12 @@ private Task RouteWebSocketAsync(string globMatch, Regex reMatch, Func<string, b
916918
{
917919
_webSocketRoutes.Insert(0, new WebSocketRouteHandler()
918920
{
919-
URL = new URLMatch()
921+
urlMatcher = new URLMatch()
920922
{
921-
BaseURL = Options.BaseURL,
922-
globMatch = globMatch,
923-
reMatch = reMatch,
924-
funcMatch = funcMatch,
923+
baseURL = Options.BaseURL,
924+
glob = globMatch,
925+
re = reMatch,
926+
func = funcMatch,
925927
},
926928
Handler = handler,
927929
});

src/Playwright/Core/Page.cs

+57-56
Original file line numberDiff line numberDiff line change
@@ -932,59 +932,59 @@ public Task AddInitScriptAsync(string script, string scriptPath)
932932
});
933933

934934
[MethodImpl(MethodImplOptions.NoInlining)]
935-
public Task RouteAsync(string url, Func<IRoute, Task> handler, PageRouteOptions options = null)
936-
=> RouteAsync(new Regex(Context.CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
935+
public Task RouteAsync(string globMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
936+
=> RouteAsync(globMatch, null, null, handler, options);
937937

938938
[MethodImpl(MethodImplOptions.NoInlining)]
939-
public Task RouteAsync(string url, Action<IRoute> handler, PageRouteOptions options = null)
940-
=> RouteAsync(new Regex(Context.CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
939+
public Task RouteAsync(string globMatch, Action<IRoute> handler, PageRouteOptions options = null)
940+
=> RouteAsync(globMatch, null, null, handler, options);
941941

942942
[MethodImpl(MethodImplOptions.NoInlining)]
943-
public Task RouteAsync(Regex url, Action<IRoute> handler, PageRouteOptions options = null)
944-
=> RouteAsync(url, null, handler, options);
943+
public Task RouteAsync(Regex reMatch, Action<IRoute> handler, PageRouteOptions options = null)
944+
=> RouteAsync(null, reMatch, null, handler, options);
945945

946946
[MethodImpl(MethodImplOptions.NoInlining)]
947-
public Task RouteAsync(Regex url, Func<IRoute, Task> handler, PageRouteOptions options = null)
948-
=> RouteAsync(url, null, handler, options);
947+
public Task RouteAsync(Regex reMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
948+
=> RouteAsync(null, reMatch, null, handler, options);
949949

950950
[MethodImpl(MethodImplOptions.NoInlining)]
951-
public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, PageRouteOptions options = null)
952-
=> RouteAsync(null, url, handler, options);
951+
public Task RouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler, PageRouteOptions options = null)
952+
=> RouteAsync(null, null, funcMatch, handler, options);
953953

954954
[MethodImpl(MethodImplOptions.NoInlining)]
955-
public Task RouteAsync(Func<string, bool> url, Func<IRoute, Task> handler, PageRouteOptions options = null)
956-
=> RouteAsync(null, url, handler, options);
955+
public Task RouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
956+
=> RouteAsync(null, null, funcMatch, handler, options);
957957

958958
[MethodImpl(MethodImplOptions.NoInlining)]
959959
public async Task UnrouteAllAsync(PageUnrouteAllOptions options = default)
960960
{
961-
await UnrouteInternalAsync(_routes, new(), options?.Behavior).ConfigureAwait(false);
961+
await UnrouteInternalAsync(_routes, [], options?.Behavior).ConfigureAwait(false);
962962
DisposeHarRouters();
963963
}
964964

965965
[MethodImpl(MethodImplOptions.NoInlining)]
966-
public Task UnrouteAsync(string urlString, Action<IRoute> handler)
967-
=> UnrouteAsync(new Regex(Context.CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
966+
public Task UnrouteAsync(string globMatch, Action<IRoute> handler)
967+
=> UnrouteAsync(globMatch, null, null, handler);
968968

969969
[MethodImpl(MethodImplOptions.NoInlining)]
970-
public Task UnrouteAsync(string urlString, Func<IRoute, Task> handler)
971-
=> UnrouteAsync(new Regex(Context.CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
970+
public Task UnrouteAsync(string globMatch, Func<IRoute, Task> handler)
971+
=> UnrouteAsync(globMatch, null, null, handler);
972972

973973
[MethodImpl(MethodImplOptions.NoInlining)]
974-
public Task UnrouteAsync(Regex urlString, Action<IRoute> handler)
975-
=> UnrouteAsync(urlString, null, handler);
974+
public Task UnrouteAsync(Regex reMatch, Action<IRoute> handler)
975+
=> UnrouteAsync(null, reMatch, null, handler);
976976

977977
[MethodImpl(MethodImplOptions.NoInlining)]
978-
public Task UnrouteAsync(Regex urlString, Func<IRoute, Task> handler)
979-
=> UnrouteAsync(urlString, null, handler);
978+
public Task UnrouteAsync(Regex reMatch, Func<IRoute, Task> handler)
979+
=> UnrouteAsync(null, reMatch, null, handler);
980980

981981
[MethodImpl(MethodImplOptions.NoInlining)]
982-
public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler)
983-
=> UnrouteAsync(null, urlFunc, handler);
982+
public Task UnrouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler)
983+
=> UnrouteAsync(null, null, funcMatch, handler);
984984

985985
[MethodImpl(MethodImplOptions.NoInlining)]
986-
public Task UnrouteAsync(Func<string, bool> urlFunc, Func<IRoute, Task> handler)
987-
=> UnrouteAsync(null, urlFunc, handler);
986+
public Task UnrouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler)
987+
=> UnrouteAsync(null, null, funcMatch, handler);
988988

989989
[MethodImpl(MethodImplOptions.NoInlining)]
990990
public Task WaitForLoadStateAsync(LoadState? state = default, PageWaitForLoadStateOptions options = default)
@@ -1235,11 +1235,16 @@ internal void OnFrameNavigated(Frame frame)
12351235

12361236
internal void FirePageError(string error) => PageError?.Invoke(this, error);
12371237

1238-
private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler, PageRouteOptions options)
1238+
private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, PageRouteOptions options)
12391239
=> RouteAsync(new()
12401240
{
1241-
Regex = urlRegex,
1242-
Function = urlFunc,
1241+
urlMatcher = new URLMatch()
1242+
{
1243+
glob = globMatch,
1244+
re = reMatch,
1245+
func = funcMatch,
1246+
baseURL = Context.Options.BaseURL,
1247+
},
12431248
Handler = handler,
12441249
Times = options?.Times,
12451250
});
@@ -1250,23 +1255,22 @@ private Task RouteAsync(RouteHandler setting)
12501255
return UpdateInterceptionAsync();
12511256
}
12521257

1253-
private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler = null)
1254-
=> UnrouteAsync(new()
1255-
{
1256-
Function = urlFunc,
1257-
Regex = urlRegex,
1258-
Handler = handler,
1259-
});
1260-
1261-
private Task UnrouteAsync(RouteHandler setting)
1258+
private async Task UnrouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
12621259
{
1263-
var newRoutes = new List<RouteHandler>();
1264-
newRoutes.AddRange(_routes.Where(r =>
1265-
(setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||
1266-
(setting.Function != null && r.Function != setting.Function) ||
1267-
(setting.Handler != null && r.Handler != setting.Handler)));
1268-
_routes = newRoutes;
1269-
return UpdateInterceptionAsync();
1260+
var removed = new List<RouteHandler>();
1261+
var remaining = new List<RouteHandler>();
1262+
foreach (var routeHandler in _routes)
1263+
{
1264+
if (routeHandler.urlMatcher.Equals(globMatch, reMatch, funcMatch, Context.Options.BaseURL) && (handler == null || routeHandler.Handler == handler))
1265+
{
1266+
removed.Add(routeHandler);
1267+
}
1268+
else
1269+
{
1270+
remaining.Add(routeHandler);
1271+
}
1272+
}
1273+
await UnrouteInternalAsync(removed, remaining, UnrouteBehavior.Default).ConfigureAwait(false);
12701274
}
12711275

12721276
private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHandler> remaining, UnrouteBehavior? behavior)
@@ -1277,7 +1281,7 @@ private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHa
12771281
{
12781282
return;
12791283
}
1280-
var tasks = removed.Select(routeHandler => routeHandler.StopAsync((UnrouteBehavior)behavior));
1284+
var tasks = removed.Select(routeHandler => routeHandler.StopAsync(behavior.Value));
12811285
await Task.WhenAll(tasks).ConfigureAwait(false);
12821286
}
12831287

@@ -1317,17 +1321,14 @@ private void Channel_BindingCall(object sender, BindingCall bindingCall)
13171321
private async Task OnRouteAsync(Route route)
13181322
{
13191323
route._context = Context;
1320-
var routeHandlers = _routes.ToArray();
1321-
foreach (var routeHandler in routeHandlers)
1324+
foreach (var routeHandler in _routes.ToArray())
13221325
{
13231326
// If the page was closed we stall all requests right away.
13241327
if (CloseWasCalled || Context.CloseWasCalled)
13251328
{
13261329
return;
13271330
}
1328-
var matches = (routeHandler.Regex?.IsMatch(route.Request.Url) == true) ||
1329-
(routeHandler.Function?.Invoke(route.Request.Url) == true);
1330-
if (!matches)
1331+
if (!routeHandler.Matches(route.Request.Url))
13311332
{
13321333
continue;
13331334
}
@@ -1578,7 +1579,7 @@ public async Task RemoveLocatorHandlerAsync(ILocator locator)
15781579
["uid"] = uid,
15791580
}).ConfigureAwait(false);
15801581
}
1581-
catch (System.Exception)
1582+
catch (Exception)
15821583
{
15831584
// Ignore
15841585
}
@@ -1602,12 +1603,12 @@ private Task RouteWebSocketAsync(string globMatch, Regex urlRegex, Func<string,
16021603
{
16031604
_webSocketRoutes.Insert(0, new WebSocketRouteHandler()
16041605
{
1605-
URL = new URLMatch()
1606+
urlMatcher = new URLMatch()
16061607
{
1607-
BaseURL = Context.Options.BaseURL,
1608-
globMatch = globMatch,
1609-
reMatch = urlRegex,
1610-
funcMatch = urlFunc,
1608+
baseURL = Context.Options.BaseURL,
1609+
glob = globMatch,
1610+
re = urlRegex,
1611+
func = urlFunc,
16111612
},
16121613
Handler = handler,
16131614
});

0 commit comments

Comments
 (0)