diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index c85444be7d10a6..3c2fbcdd768366 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -9,22 +9,26 @@ namespace System.Net.Http.Functional.Tests { public class HttpMethodTest { - public static IEnumerable StaticHttpMethods { get; } - - static HttpMethodTest() - { - List staticHttpMethods = new List + private static readonly IReadOnlyList StaticHttpMethods = new HttpMethod[] + { + HttpMethod.Connect, + HttpMethod.Delete, + HttpMethod.Get, + HttpMethod.Head, + HttpMethod.Options, + HttpMethod.Patch, + HttpMethod.Post, + HttpMethod.Put, + HttpMethod.Query, + HttpMethod.Trace, + }; + + public static IEnumerable StaticHttpMethods_MemberData() + { + foreach (HttpMethod method in StaticHttpMethods) { - 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 } - }; - AddStaticHttpMethods(staticHttpMethods); - StaticHttpMethods = staticHttpMethods; + yield return new object[] { method }; + } } [Fact] @@ -116,7 +120,7 @@ public void GetHashCode_CustomStringMethod_SameAsStringToUpperInvariantHashCode( } [Theory] - [MemberData(nameof(StaticHttpMethods))] + [MemberData(nameof(StaticHttpMethods_MemberData))] public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode(HttpMethod method) { Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode()); @@ -151,27 +155,24 @@ 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) + [MemberData(nameof(StaticHttpMethods_MemberData))] + public void Parse_KnownMethod_UsesKnownInstances(HttpMethod 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] @@ -201,10 +202,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 }); - } } }