Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs
T
Stephen Toub 60d1224ddd Re-enable StyleCop warnings SA1028 and SA1518 (trailing whitespace and blank lines) (dotnet/corefx#40113)
Mechanical/automated change.  The only manual changes were a) removing the two rules from the CodeAnalysis.ruleset file, b) removing spaces from one line of a .targets file that is used to generate a .cs file, and c) reverting the changes in a few test files where the naive replacement happened in the middle of a verbatim string where the whitespace actually mattered.

Commit migrated from https://github.com/dotnet/corefx/commit/09a6e66b0c2b31bd0f6e7e3170537d14027aeed0
2019-08-07 21:07:58 -04:00

154 lines
5.2 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using Xunit;
namespace System.Net.Http.Functional.Tests
{
public partial class HttpMethodTest
{
public static IEnumerable<object[]> StaticHttpMethods { get; }
static HttpMethodTest()
{
List<object[]> staticHttpMethods = new List<object[]>
{
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;
}
static partial void AddStaticHttpMethods(List<object[]> staticHttpMethods);
[Fact]
public void StaticProperties_VerifyValues_PropertyNameMatchesHttpMethodName()
{
Assert.Equal("GET", HttpMethod.Get.Method);
Assert.Equal("PUT", HttpMethod.Put.Method);
Assert.Equal("POST", HttpMethod.Post.Method);
Assert.Equal("DELETE", HttpMethod.Delete.Method);
Assert.Equal("HEAD", HttpMethod.Head.Method);
Assert.Equal("OPTIONS", HttpMethod.Options.Method);
Assert.Equal("TRACE", HttpMethod.Trace.Method);
}
[Fact]
public void Ctor_ValidMethodToken_Success()
{
new HttpMethod("GET");
new HttpMethod("custom");
// Note that '!' is the first ASCII char after CTLs and '~' is the last character before DEL char.
new HttpMethod("validtoken!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz^_`|~");
}
[Fact]
public void Ctor_NullMethod_Exception()
{
AssertExtensions.Throws<ArgumentException>("method", () => { new HttpMethod(null); } );
}
[Theory]
[InlineData('(')]
[InlineData(')')]
[InlineData('<')]
[InlineData('>')]
[InlineData('@')]
[InlineData(',')]
[InlineData(';')]
[InlineData(':')]
[InlineData('\\')]
[InlineData('"')]
[InlineData('/')]
[InlineData('[')]
[InlineData(']')]
public void Ctor_SeparatorInMethod_Exception(char separator)
{
Assert.Throws<FormatException>(() => { new HttpMethod("Get" + separator); } );
}
[Fact]
public void Equals_DifferentComparisonMethodsForSameMethods_MethodsConsideredEqual()
{
// Positive test cases
Assert.True(new HttpMethod("GET") == HttpMethod.Get);
Assert.False(new HttpMethod("GET") != HttpMethod.Get);
Assert.True((new HttpMethod("GET")).Equals(HttpMethod.Get));
Assert.True(new HttpMethod("get") == HttpMethod.Get);
Assert.False(new HttpMethod("get") != HttpMethod.Get);
Assert.True((new HttpMethod("get")).Equals(HttpMethod.Get));
}
[Fact]
public void Equals_CompareWithMethodCastedToObject_ReturnsTrue()
{
object other = new HttpMethod("GET");
Assert.True(HttpMethod.Get.Equals(other));
Assert.False(HttpMethod.Get.Equals("GET"));
}
[Fact]
public void Equals_NullComparand_ReturnsFalse()
{
Assert.False(null == HttpMethod.Options);
Assert.False(HttpMethod.Trace == null);
}
[Theory]
[InlineData("GET")]
[InlineData("get")]
[InlineData("Get")]
[InlineData("CUSTOM")]
[InlineData("cUsToM")]
public void GetHashCode_CustomStringMethod_SameAsStringToUpperInvariantHashCode(string input)
{
HttpMethod method = new HttpMethod(input);
Assert.Equal(input.ToUpperInvariant().GetHashCode(), method.GetHashCode());
}
[Theory]
[MemberData(nameof(StaticHttpMethods))]
public void GetHashCode_StaticMethods_SameAsStringToUpperInvariantHashCode(HttpMethod method)
{
Assert.Equal(method.ToString().ToUpperInvariant().GetHashCode(), method.GetHashCode());
}
[Fact]
public void GetHashCode_DifferentlyCasedMethod_SameHashCode()
{
string input = "GeT";
HttpMethod method = new HttpMethod(input);
Assert.Equal(HttpMethod.Get.GetHashCode(), method.GetHashCode());
}
[Fact]
public void ToString_UseCustomStringMethod_SameAsString()
{
string custom = "custom";
HttpMethod method = new HttpMethod(custom);
Assert.Equal(custom, method.ToString());
}
[Fact]
public void Method_AccessProperty_MatchesCtorString()
{
HttpMethod method = new HttpMethod("custom");
Assert.Equal("custom", method.Method);
}
}
}