Skip to content

Commit 795584e

Browse files
authored
Fix 16840: Navigate to special URLs without throwing errors (#26769)
### Summary Allow the [manual] navigation to special URL like `https://localhost:5001?keyword=你好 世界` ### Details `Uri.IsWellFormedUriString` does not tell you whether the URL is valid or not instead it tells if the URL needs to be escaped to further work with it [At least that's how it is currently described and implemented]. To know if a URL is valid `Uri.TryCreate` will do correct job, for additional information please check [Runtime#37634](dotnet/runtime#37634) ℹ For consideration, other [places in the repo](https://github.com/dotnet/aspnetcore/search?q=Uri.IsWellFormedUriString) might have the same problem. Addresses #16840
1 parent be472db commit 795584e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Components/Server/src/ComponentHub.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public async ValueTask<string> StartCircuit(string baseUri, string uri, string s
8989

9090
if (baseUri == null ||
9191
uri == null ||
92-
!Uri.IsWellFormedUriString(baseUri, UriKind.Absolute) ||
93-
!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
92+
!Uri.TryCreate(baseUri, UriKind.Absolute, out _) ||
93+
!Uri.TryCreate(uri, UriKind.Absolute, out _))
9494
{
9595
// We do some really minimal validation here to prevent obviously wrong data from getting in
9696
// without duplicating too much logic.

src/Components/test/E2ETest/Tests/RoutingTest.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Microsoft.AspNetCore.Testing;
1313
using OpenQA.Selenium;
1414
using OpenQA.Selenium.Interactions;
15-
using OpenQA.Selenium.Support.UI;
1615
using Xunit;
1716
using Xunit.Abstractions;
1817

@@ -608,18 +607,30 @@ public void OnNavigate_DoesNotRenderWhileOnNavigateExecuting()
608607
Browser.Equal("This is a long page you can scroll.", () => app.FindElement(By.Id("test-info")).Text);
609608
}
610609

610+
[Theory]
611+
[InlineData("/WithParameters/Name/Ñoño ñi/LastName/O'Jkl")]
612+
[InlineData("/WithParameters/Name/[Ñoño ñi]/LastName/O'Jkl")]
613+
[InlineData("/other?abc=Ñoño ñi")]
614+
[InlineData("/other?abc=[Ñoño ñi]")]
615+
public void CanArriveAtPageWithSpecialURL(string relativeUrl)
616+
{
617+
SetUrlViaPushState(relativeUrl, true);
618+
var errorUi = Browser.Exists(By.Id("blazor-error-ui"));
619+
Browser.Equal("none", () => errorUi.GetCssValue("display"));
620+
}
621+
611622
private long BrowserScrollY
612623
{
613624
get => (long)((IJavaScriptExecutor)Browser).ExecuteScript("return window.scrollY");
614625
set => ((IJavaScriptExecutor)Browser).ExecuteScript($"window.scrollTo(0, {value})");
615626
}
616627

617-
private string SetUrlViaPushState(string relativeUri)
628+
private string SetUrlViaPushState(string relativeUri, bool forceLoad = false)
618629
{
619630
var pathBaseWithoutHash = ServerPathBase.Split('#')[0];
620631
var jsExecutor = (IJavaScriptExecutor)Browser;
621632
var absoluteUri = new Uri(_serverFixture.RootUri, $"{pathBaseWithoutHash}{relativeUri}");
622-
jsExecutor.ExecuteScript($"Blazor.navigateTo('{absoluteUri.ToString().Replace("'", "\\'")}')");
633+
jsExecutor.ExecuteScript($"Blazor.navigateTo('{absoluteUri.ToString().Replace("'", "\\'")}', {(forceLoad ? "true" : "false")})");
623634

624635
return absoluteUri.AbsoluteUri;
625636
}

0 commit comments

Comments
 (0)