From bd603b6c8427b6882073ed242f4ae8c834f0df42 Mon Sep 17 00:00:00 2001 From: Nicolo Avanzini Date: Wed, 24 Jun 2026 09:43:16 +0200 Subject: [PATCH 1/4] Add HttpMethod.Query to static method test data --- .../System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index c85444be7d10a6..be94de833ec2c9 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -21,7 +21,8 @@ static HttpMethodTest() new object[] { HttpMethod.Delete }, new object[] { HttpMethod.Head }, new object[] { HttpMethod.Options }, - new object[] { HttpMethod.Trace } + new object[] { HttpMethod.Trace }, + new object[] { HttpMethod.Query } }; AddStaticHttpMethods(staticHttpMethods); StaticHttpMethods = staticHttpMethods; From 4e48697b383ce3c7132174533b33b6f2ae102428 Mon Sep 17 00:00:00 2001 From: Nicolo Avanzini Date: Thu, 25 Jun 2026 14:13:32 +0200 Subject: [PATCH 2/4] test: consolidate StaticHttpMethods and remove duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace separate MemberData lists with a single canonical StaticHttpMethods List containing all known HTTP methods (Connect, Delete, Get, Head, Options, Patch, Post, Put, Query, Trace). Convert GetHashCode_StaticMethods and Parse_KnownMethod tests from Theory with MemberData to Fact with foreach loops over StaticHttpMethods. Remove the Parse_UsesKnownInstances_MemberData helper and AddStaticHttpMethods static initializer — the list is now declared directly as a field. This eliminates the duplication of having the static methods list defined in three different places. --- .../tests/FunctionalTests/HttpMethodTest.cs | 75 +++++++------------ 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index be94de833ec2c9..7ae5134ef7013c 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -9,24 +9,19 @@ namespace System.Net.Http.Functional.Tests { public class HttpMethodTest { - public static IEnumerable StaticHttpMethods { get; } - - static HttpMethodTest() - { - List staticHttpMethods = new List - { - new object[] { HttpMethod.Get }, - new object[] { HttpMethod.Put }, - new object[] { HttpMethod.Post }, - new object[] { HttpMethod.Delete }, - new object[] { HttpMethod.Head }, - new object[] { HttpMethod.Options }, - new object[] { HttpMethod.Trace }, - new object[] { HttpMethod.Query } - }; - AddStaticHttpMethods(staticHttpMethods); - StaticHttpMethods = staticHttpMethods; - } + public static readonly List StaticHttpMethods = new List + { + HttpMethod.Connect, + HttpMethod.Delete, + HttpMethod.Get, + HttpMethod.Head, + HttpMethod.Options, + HttpMethod.Patch, + HttpMethod.Post, + HttpMethod.Put, + HttpMethod.Query, + HttpMethod.Trace, + }; [Fact] public void StaticProperties_VerifyValues_PropertyNameMatchesHttpMethodName() @@ -116,11 +111,13 @@ public void GetHashCode_CustomStringMethod_SameAsStringToUpperInvariantHashCode( Assert.Equal(input.ToUpperInvariant().GetHashCode(), method.GetHashCode()); } - [Theory] - [MemberData(nameof(StaticHttpMethods))] - public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode(HttpMethod method) + [Fact] + public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode() { - Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode()); + foreach (HttpMethod method in StaticHttpMethods) + { + Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode()); + } } [Fact] @@ -152,27 +149,16 @@ public void Patch_VerifyValue_PropertyNameMatchesHttpMethodName() Assert.Equal("PATCH", HttpMethod.Patch.Method); } - public static IEnumerable Parse_UsesKnownInstances_MemberData() - { - yield return new object[] { HttpMethod.Connect, nameof(HttpMethod.Connect) }; - yield return new object[] { HttpMethod.Delete, nameof(HttpMethod.Delete) }; - yield return new object[] { HttpMethod.Get, nameof(HttpMethod.Get) }; - yield return new object[] { HttpMethod.Head, nameof(HttpMethod.Head) }; - yield return new object[] { HttpMethod.Options, nameof(HttpMethod.Options) }; - yield return new object[] { HttpMethod.Patch, nameof(HttpMethod.Patch) }; - yield return new object[] { HttpMethod.Post, nameof(HttpMethod.Post) }; - yield return new object[] { HttpMethod.Put, nameof(HttpMethod.Put) }; - yield return new object[] { HttpMethod.Query, nameof(HttpMethod.Query) }; - yield return new object[] { HttpMethod.Trace, nameof(HttpMethod.Trace) }; - } - - [Theory] - [MemberData(nameof(Parse_UsesKnownInstances_MemberData))] - public void Parse_KnownMethod_UsesKnownInstances(HttpMethod method, string methodName) + [Fact] + public void Parse_KnownMethod_UsesKnownInstances() { - Assert.Same(method, HttpMethod.Parse(methodName)); - Assert.Same(method, HttpMethod.Parse(methodName.ToUpperInvariant())); - Assert.Same(method, HttpMethod.Parse(methodName.ToLowerInvariant())); + foreach (HttpMethod method in StaticHttpMethods) + { + string methodName = method.Method; + Assert.Same(method, HttpMethod.Parse(methodName)); + Assert.Same(method, HttpMethod.Parse(methodName.ToUpperInvariant())); + Assert.Same(method, HttpMethod.Parse(methodName.ToLowerInvariant())); + } } [Theory] @@ -202,10 +188,5 @@ public void Parse_InvalidToken_Throws(string method) { Assert.Throws(() => HttpMethod.Parse(method)); } - - private static void AddStaticHttpMethods(List staticHttpMethods) - { - staticHttpMethods.Add(new object[] { HttpMethod.Patch }); - } } } From 77d566ce93ef8568ef05d5ff5a1d6e7fc41379ff Mon Sep 17 00:00:00 2001 From: Nicolo Avanzini Date: Thu, 25 Jun 2026 14:27:29 +0200 Subject: [PATCH 3/4] test: restore data-driven theories for StaticHttpMethods tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a StaticHttpMethods_MemberData source derived from the StaticHttpMethods list and use it to drive GetHashCode_StaticMethods and Parse_KnownMethod as [Theory] cases again. This keeps the single canonical StaticHttpMethods list while preserving per-method failure granularity — each method runs as its own case rather than being hidden behind a single [Fact] loop that exits on first failure. --- .../tests/FunctionalTests/HttpMethodTest.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index 7ae5134ef7013c..1f3667caea7eeb 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -23,6 +23,14 @@ public class HttpMethodTest HttpMethod.Trace, }; + public static IEnumerable StaticHttpMethods_MemberData() + { + foreach (HttpMethod method in StaticHttpMethods) + { + yield return new object[] { method }; + } + } + [Fact] public void StaticProperties_VerifyValues_PropertyNameMatchesHttpMethodName() { @@ -111,13 +119,11 @@ public void GetHashCode_CustomStringMethod_SameAsStringToUpperInvariantHashCode( Assert.Equal(input.ToUpperInvariant().GetHashCode(), method.GetHashCode()); } - [Fact] - public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode() + [Theory] + [MemberData(nameof(StaticHttpMethods_MemberData))] + public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode(HttpMethod method) { - foreach (HttpMethod method in StaticHttpMethods) - { - Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode()); - } + Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode()); } [Fact] @@ -149,16 +155,14 @@ public void Patch_VerifyValue_PropertyNameMatchesHttpMethodName() Assert.Equal("PATCH", HttpMethod.Patch.Method); } - [Fact] - public void Parse_KnownMethod_UsesKnownInstances() + [Theory] + [MemberData(nameof(StaticHttpMethods_MemberData))] + public void Parse_KnownMethod_UsesKnownInstances(HttpMethod method) { - foreach (HttpMethod method in StaticHttpMethods) - { - string methodName = method.Method; - Assert.Same(method, HttpMethod.Parse(methodName)); - Assert.Same(method, HttpMethod.Parse(methodName.ToUpperInvariant())); - Assert.Same(method, HttpMethod.Parse(methodName.ToLowerInvariant())); - } + string methodName = method.Method; + Assert.Same(method, HttpMethod.Parse(methodName)); + Assert.Same(method, HttpMethod.Parse(methodName.ToUpperInvariant())); + Assert.Same(method, HttpMethod.Parse(methodName.ToLowerInvariant())); } [Theory] From e86d897f75edc5418ec8403580bda07206220767 Mon Sep 17 00:00:00 2001 From: Nicolo Avanzini Date: Thu, 25 Jun 2026 14:35:37 +0200 Subject: [PATCH 4/4] test: encapsulate StaticHttpMethods and restore case-insensitivity coverage - Make StaticHttpMethods private and immutable (IReadOnlyList backed by array) to prevent accidental mutation and inter-test coupling - Remove redundant ToUpperInvariant() call in Parse_KnownMethod test - Add ToMixedCase() helper and restore meaningful case-insensitivity coverage by testing lower-case and mixed-case variants alongside exact case --- .../tests/FunctionalTests/HttpMethodTest.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index 1f3667caea7eeb..3c2fbcdd768366 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -9,7 +9,7 @@ namespace System.Net.Http.Functional.Tests { public class HttpMethodTest { - public static readonly List StaticHttpMethods = new List + private static readonly IReadOnlyList StaticHttpMethods = new HttpMethod[] { HttpMethod.Connect, HttpMethod.Delete, @@ -159,10 +159,20 @@ public void Patch_VerifyValue_PropertyNameMatchesHttpMethodName() [MemberData(nameof(StaticHttpMethods_MemberData))] public void Parse_KnownMethod_UsesKnownInstances(HttpMethod method) { - string methodName = method.Method; + string methodName = method.Method; // Already upper-case (e.g. "GET"). Assert.Same(method, HttpMethod.Parse(methodName)); - Assert.Same(method, HttpMethod.Parse(methodName.ToUpperInvariant())); Assert.Same(method, HttpMethod.Parse(methodName.ToLowerInvariant())); + Assert.Same(method, HttpMethod.Parse(ToMixedCase(methodName))); + } + + private static string ToMixedCase(string value) + { + char[] chars = value.ToCharArray(); + for (int i = 0; i < chars.Length; i++) + { + chars[i] = (i % 2 == 0) ? char.ToUpperInvariant(chars[i]) : char.ToLowerInvariant(chars[i]); + } + return new string(chars); } [Theory]