Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2dcf647

Browse files
committed
Fix regression in HttpMethod.GetHashCode()
A PR from last month changed how GetHashCode() works. This ended up breaking tests against the .NET Native build of System.Net.Http which shares the same source code as CoreFx. So, reverting the change in GetHashCode() and porting additional ToF tests to GitHub.
1 parent 6ef2364 commit 2dcf647

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/System.Net.Http/src/System/Net/Http/HttpMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override bool Equals(object obj)
106106

107107
public override int GetHashCode()
108108
{
109-
return StringComparer.OrdinalIgnoreCase.GetHashCode(_method);
109+
return _method.ToUpperInvariant().GetHashCode();
110110
}
111111

112112
public override string ToString()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Net;
6+
using System.Net.Http;
7+
8+
using Xunit;
9+
10+
namespace System.Net.Http.Tests
11+
{
12+
public class HttpMethodTest
13+
{
14+
[Fact]
15+
public void StaticProperties_VerifyValues_PropertyNameMatchesHttpMethodName()
16+
{
17+
Assert.Equal("GET", HttpMethod.Get.Method);
18+
Assert.Equal("PUT", HttpMethod.Put.Method);
19+
Assert.Equal("POST", HttpMethod.Post.Method);
20+
Assert.Equal("DELETE", HttpMethod.Delete.Method);
21+
Assert.Equal("HEAD", HttpMethod.Head.Method);
22+
Assert.Equal("OPTIONS", HttpMethod.Options.Method);
23+
Assert.Equal("TRACE", HttpMethod.Trace.Method);
24+
}
25+
26+
[Fact]
27+
public void Ctor_ValidMethodToken_Success()
28+
{
29+
new HttpMethod("GET");
30+
new HttpMethod("custom");
31+
32+
// Note that '!' is the first ASCII char after CTLs and '~' is the last character before DEL char.
33+
new HttpMethod("validtoken!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz^_`|~");
34+
}
35+
36+
[Fact]
37+
public void Ctor_NullMethod_Exception()
38+
{
39+
Assert.Throws<ArgumentException>(() => { new HttpMethod(null); } );
40+
}
41+
42+
// TODO: This should be a [Theory]
43+
[Fact]
44+
public void Ctor_SeparatorInMethod_Exception()
45+
{
46+
char[] separators = new char[] { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']',
47+
'?', '=', '{', '}', ' ', '\t' };
48+
49+
for (int i = 0; i < separators.Length; i++)
50+
{
51+
Assert.Throws<FormatException>(() => { new HttpMethod("Get" + separators[i]); } );
52+
}
53+
}
54+
55+
[Fact]
56+
public void Equals_DifferentComparisonMethodsForSameMethods_MethodsConsideredEqual()
57+
{
58+
// Positive test cases
59+
Assert.True(new HttpMethod("GET") == HttpMethod.Get);
60+
Assert.False(new HttpMethod("GET") != HttpMethod.Get);
61+
Assert.True((new HttpMethod("GET")).Equals(HttpMethod.Get));
62+
63+
Assert.True(new HttpMethod("get") == HttpMethod.Get);
64+
Assert.False(new HttpMethod("get") != HttpMethod.Get);
65+
Assert.True((new HttpMethod("get")).Equals(HttpMethod.Get));
66+
}
67+
68+
[Fact]
69+
public void Equals_CompareWithMethodCastedToObject_ReturnsTrue()
70+
{
71+
object other = new HttpMethod("GET");
72+
Assert.True(HttpMethod.Get.Equals(other));
73+
Assert.False(HttpMethod.Get.Equals("GET"));
74+
}
75+
76+
[Fact]
77+
public void Equals_NullComparand_ReturnsFalse()
78+
{
79+
Assert.False(null == HttpMethod.Options);
80+
Assert.False(HttpMethod.Trace == null);
81+
}
82+
83+
[Fact]
84+
public void GetHashCode_UseCustomStringMethod_SameAsStringHashCode()
85+
{
86+
string custom = "CUSTOM";
87+
HttpMethod method = new HttpMethod(custom);
88+
Assert.Equal(custom.GetHashCode(), method.GetHashCode());
89+
}
90+
91+
[Fact]
92+
public void GetHashCode_DifferentlyCasedMethod_SameHashCode()
93+
{
94+
string input = "GeT";
95+
HttpMethod method = new HttpMethod(input);
96+
Assert.Equal(HttpMethod.Get.GetHashCode(), method.GetHashCode());
97+
}
98+
99+
[Fact]
100+
public void ToString_UseCustomStringMethod_SameAsString()
101+
{
102+
string custom = "custom";
103+
HttpMethod method = new HttpMethod(custom);
104+
Assert.Equal(custom, method.ToString());
105+
}
106+
107+
[Fact]
108+
public void Method_AccessProperty_MatchesCtorString()
109+
{
110+
HttpMethod method = new HttpMethod("custom");
111+
Assert.Equal("custom", method.Method);
112+
}
113+
}
114+
}

src/System.Net.Http/tests/FunctionalTests/System.Net.Http.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Windows_Release|AnyCPU' " />
2121
<ItemGroup>
2222
<Compile Include="HttpClientHandlerTest.cs" />
23+
<Compile Include="HttpMethodTest.cs" />
2324
<Compile Include="XunitTestAssemblyAtrributes.cs" />
2425
</ItemGroup>
2526
<ItemGroup>

0 commit comments

Comments
 (0)