diff --git a/src/libraries/System.Private.Uri/src/System/Uri.cs b/src/libraries/System.Private.Uri/src/System/Uri.cs index daff7bddf4df8..1ed13695b645d 100644 --- a/src/libraries/System.Private.Uri/src/System/Uri.cs +++ b/src/libraries/System.Private.Uri/src/System/Uri.cs @@ -2265,7 +2265,7 @@ private unsafe void CreateUriInfo(Flags cF) } } } - if (notEmpty && info.Offset.PortValue != port) + if (notEmpty && _syntax.DefaultPort != port) { info.Offset.PortValue = (ushort)port; cF |= Flags.NotDefaultPort; diff --git a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriGetComponentsTest.cs b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriGetComponentsTest.cs index 98a172c3f5ddc..6c3a91ad051ff 100644 --- a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriGetComponentsTest.cs +++ b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriGetComponentsTest.cs @@ -61,11 +61,11 @@ public void GetComponents_UnknownScheme_ComponentsUnaffected() testUri = new Uri("abc://127.00.000.001:01234"); Assert.Equal("127.00.000.001:1234", testUri.Authority); - // Known limitation: port 0 is ignored. + // Port 0 is parsed correctly. testUri = new Uri("cbd://127.00.1.2:0000"); - Assert.Equal("127.00.1.2", testUri.Authority); + Assert.Equal("127.00.1.2:0", testUri.Authority); testUri = new Uri("cbd://127.00.1.2:0"); - Assert.Equal("127.00.1.2", testUri.Authority); + Assert.Equal("127.00.1.2:0", testUri.Authority); testUri = new Uri("eb://[0000::01:123.32.123.23]/dir"); Assert.Equal("[::1:7b20:7b17]", testUri.Authority); diff --git a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs index 5a976ca912991..5dea3b30e95e9 100644 --- a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs +++ b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs @@ -818,5 +818,23 @@ public static void FilePathHandlesNonAscii(string uriString, string toString, st Assert.Equal(absoluteUri, uri2.AbsoluteUri); Assert.Equal(localPath, uri2.LocalPath); } + + public static IEnumerable ZeroPortIsParsedForBothKnownAndUnknownSchemes_TestData() + { + yield return new object[] { "http://example.com:0", 0, false }; + yield return new object[] { "http://example.com", 80, true }; + yield return new object[] { "rtsp://example.com:0", 0, false }; + yield return new object[] { "rtsp://example.com", -1, true }; + } + + [Theory] + [MemberData(nameof(ZeroPortIsParsedForBothKnownAndUnknownSchemes_TestData))] + public static void ZeroPortIsParsedForBothKnownAndUnknownSchemes(string uriString, int port, bool isDefaultPort) + { + Uri.TryCreate(uriString, UriKind.Absolute, out var uri); + Assert.Equal(port, uri.Port); + Assert.Equal(isDefaultPort, uri.IsDefaultPort); + Assert.Equal(uriString + "/", uri.ToString()); + } } }