Mark the port as implicit for empty values in Cookie (#76143)
authorDavid Sisco <dsisco11@gmail.com>
Thu, 13 Oct 2022 15:11:27 +0000 (08:11 -0700)
committerGitHub <noreply@github.com>
Thu, 13 Oct 2022 15:11:27 +0000 (08:11 -0700)
* [Bug] Fix issue #70227

* Fix cookie tests cases to reflect correct behavior.

* Remove trailing semicolon in expected result of Cookie string tests.

src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs
src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs

index ff51ae3..ee319ca 100644 (file)
@@ -585,14 +585,17 @@ namespace System.Net
             }
             set
             {
-                m_port_implicit = false;
                 if (string.IsNullOrEmpty(value))
                 {
                     // "Port" is present but has no value.
+                    // Therefore; the effective port value is implicit.
+                    m_port_implicit = true;
                     m_port = string.Empty;
                 }
                 else
                 {
+                    // "Port" value is present, so we use the provided value rather than an implicit one.
+                    m_port_implicit = false;
                     // Parse port list
                     if (!value.StartsWith('\"') || !value.EndsWith('\"'))
                     {
index 2f326dc..d2c44d4 100644 (file)
@@ -349,8 +349,14 @@ namespace System.Net.Primitives.Functional.Tests
             c.Version = 0;
             Assert.Equal("name=value; $Path=path; $Domain=domain; $Port=\"80\"", c.ToString());
 
+            // If a cookie string specifies either an empty string or no value for the port, then the port should be considered implicit.
+            // Otherwise such cookies will have no valid ports and also be incapable of assuming a usable port which in turn means they cannot be matched to ANY Uris and will effectively become nonfunctional.
             c.Port = "";
-            Assert.Equal("name=value; $Path=path; $Domain=domain; $Port", c.ToString());
+            Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());
+
+            // Test null also, for sanity.
+            c.Port = null;
+            Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());
         }
     }
 }