Skip to content

Commit e386eca

Browse files
Uri constructor test: state the bare-path outcome per platform
new Uri("/foo") throws UriFormatException on Windows but parses as the absolute file URI file:///foo on Unix, where a bare path is a rooted local path. The test now asserts the real behaviour on each platform instead of the Windows one everywhere. new Uri("//example.com") is unchanged: it yields file://example.com/ on both.
1 parent c4a4680 commit e386eca

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

‎aspnetcore-features/UrlEncodingAndDecoding/Tests/UrlEncodingDecodingUnitTest.cs‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,33 @@ public void GivenTheEncodeUriComponentSafeCharacters_WhenEscapingWithUri_ThenThe
104104
}
105105

106106
[TestMethod]
107-
public void GivenABarePath_WhenConstructingAUri_ThenUriFormatExceptionThrown()
107+
public void GivenABarePath_WhenConstructingAUri_ThenTheOutcomeIsPlatformDependent()
108108
{
109-
Assert.ThrowsExactly<UriFormatException>(() => new Uri("/foo"));
110-
111-
Assert.IsFalse(Uri.TryCreate("/foo", UriKind.Absolute, out _));
109+
if (OperatingSystem.IsWindows())
110+
{
111+
//A bare path is not an absolute URI on Windows and not a rooted local path either.
112+
Assert.ThrowsExactly<UriFormatException>(() => new Uri("/foo"));
113+
Assert.IsFalse(Uri.TryCreate("/foo", UriKind.Absolute, out _));
114+
}
115+
else
116+
{
117+
//On Unix a bare path IS a rooted local path, so it parses as an absolute file URI.
118+
var uri = new Uri("/foo");
119+
120+
Assert.AreEqual("file", uri.Scheme);
121+
Assert.AreEqual("file:///foo", uri.AbsoluteUri);
122+
Assert.IsTrue(Uri.TryCreate("/foo", UriKind.Absolute, out _));
123+
}
124+
125+
//The relative remedy works the same way on every platform.
112126
Assert.IsTrue(Uri.TryCreate("/foo", UriKind.Relative, out _));
127+
Assert.IsFalse(new Uri("/foo", UriKind.Relative).IsAbsoluteUri);
113128
}
114129

115130
[TestMethod]
116131
public void GivenAProtocolRelativeUrl_WhenConstructingAUri_ThenItParsesAsAFileUri()
117132
{
133+
//Same on Windows and on Unix: this does not throw, it becomes a file URI.
118134
var uri = new Uri("//example.com");
119135

120136
Assert.AreEqual("file", uri.Scheme);

0 commit comments

Comments
 (0)