Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Adding HTTP method constants #712

Merged
merged 11 commits into from
Sep 27, 2016
69 changes: 69 additions & 0 deletions src/Microsoft.AspNetCore.Http.Abstractions/HttpMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNetCore.Http
{
public static class HttpMethods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be singular?
HttpMethod.Get
HttpMethod.IsGet(string)
HttpMethod.Equals(string, string)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This is similar to a non-flag enum.

{
public const string Connect = "CONNECT";
public const string Delete = "DELETE";
public const string Get = "GET";
public const string Head = "HEAD";
public const string Options = "OPTIONS";
public const string Patch = "PATCH";
public const string Post = "POST";
public const string Put = "PUT";
public const string Trace = "TRACE";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm for keeping these const. ReferenceEquals still works across assembly boundaries. Just don't mess up 😛 .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static readonly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, constants would be nicer (e.g they could be used for attribute values) 👍


public static bool IsConnectMethod(string method)
{
return object.ReferenceEquals(Connect, method) || StringComparer.OrdinalIgnoreCase.Equals(Connect, method);
}

public static bool IsDeleteMethod(string method)
{
return object.ReferenceEquals(Delete, method) || StringComparer.OrdinalIgnoreCase.Equals(Delete, method);
}

public static bool IsGetMethod(string method)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpMethod.IsGetMethod(string) seems redundant. What about just HttpMethod.IsGet(string)?

{
return object.ReferenceEquals(Get, method) || StringComparer.OrdinalIgnoreCase.Equals(Get, method);
}

public static bool IsHeadMethod(string method){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting?

return object.ReferenceEquals(Head, method) || StringComparer.OrdinalIgnoreCase.Equals(Head, method);
}

public static bool IsOptionsMethod(string method)
{
return object.ReferenceEquals(Options, method) || StringComparer.OrdinalIgnoreCase.Equals(Options, method);
}

public static bool IsPatchMethod(string method)
{
return object.ReferenceEquals(Patch, method) || StringComparer.OrdinalIgnoreCase.Equals(Patch, method);
}

public static bool IsPostMethod(string method)
{
return object.ReferenceEquals(Post, method) || StringComparer.OrdinalIgnoreCase.Equals(Post, method);
}

public static bool IsPutMethod(string method)
{
return object.ReferenceEquals(Put, method) || StringComparer.OrdinalIgnoreCase.Equals(Put, method);
}

public static bool IsTraceMethod(string method)
{
return object.ReferenceEquals(Trace, method) || StringComparer.OrdinalIgnoreCase.Equals(Trace, method);
}

public static bool Equals(string left, string right)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is pointless... We hard code a few methods there's no point having a case insensitive overload here. It adds nothing..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna let you and @Tratcher settle this...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tratcher lets decide on this offline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? If anything this could be private and called by the Is* methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a big discussion on this. We're taking it out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its decided, nuke it.

{
return StringComparer.OrdinalIgnoreCase.Equals(left, right);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should still have the Equals(string, string) method

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit;

Expand All @@ -25,15 +26,15 @@ public void OwinHttpEnvironmentCanBeCreated()
{
var env = new Dictionary<string, object>
{
{ "owin.RequestMethod", "POST" },
{ "owin.RequestMethod", HttpMethods.Post },
{ "owin.RequestPath", "/path" },
{ "owin.RequestPathBase", "/pathBase" },
{ "owin.RequestQueryString", "name=value" },
};
var features = new OwinFeatureCollection(env);

var requestFeature = Get<IHttpRequestFeature>(features);
Assert.Equal(requestFeature.Method, "POST");
Assert.Equal(requestFeature.Method, HttpMethods.Post);
Assert.Equal(requestFeature.Path, "/path");
Assert.Equal(requestFeature.PathBase, "/pathBase");
Assert.Equal(requestFeature.QueryString, "?name=value");
Expand Down