[Fixes #2700] Added HttpHeadAttribute

This commit is contained in:
Ajay Bhargav Baaskaran 2015-06-15 16:32:43 -07:00
parent 27283ec098
commit b245996949
5 changed files with 48 additions and 28 deletions

View File

@ -0,0 +1,33 @@
// 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.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Identifies an action that only supports the HTTP HEAD method.
/// </summary>
public class HttpHeadAttribute : HttpMethodAttribute
{
private static readonly IEnumerable<string> _supportedMethods = new string[] { "HEAD" };
/// <summary>
/// Creates a new <see cref="HttpHeadAttribute"/>.
/// </summary>
public HttpHeadAttribute()
: base(_supportedMethods)
{
}
/// <summary>
/// Creates a new <see cref="HttpHeadAttribute"/> with the given route template.
/// </summary>
/// <param name="template">The route template. May not be null.</param>
public HttpHeadAttribute([NotNull] string template)
: base(_supportedMethods, template)
{
}
}
}

View File

@ -464,13 +464,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
// Assert // Assert
var action = Assert.Single(actions); var action = Assert.Single(actions);
Assert.Contains("DELETE", action.HttpMethods);
Assert.Contains("HEAD", action.HttpMethods);
Assert.Equal("Delete", action.ActionName); Assert.Equal("Delete", action.ActionName);
var httpMethod = Assert.Single(action.HttpMethods);
Assert.Equal("DELETE", httpMethod);
Assert.Null(action.AttributeRouteModel); Assert.Null(action.AttributeRouteModel);
Assert.Single(action.Attributes.OfType<HttpDeleteAttribute>());
Assert.IsType<HttpDeleteAttribute>(Assert.Single(action.Attributes)); Assert.Single(action.Attributes.OfType<HttpHeadAttribute>());
} }
[Fact] [Fact]
@ -488,6 +488,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
var action = Assert.Single(actions); var action = Assert.Single(actions);
Assert.Contains("GET", action.HttpMethods); Assert.Contains("GET", action.HttpMethods);
Assert.Contains("POST", action.HttpMethods); Assert.Contains("POST", action.HttpMethods);
Assert.Contains("HEAD", action.HttpMethods);
Assert.Equal("Details", action.ActionName); Assert.Equal("Details", action.ActionName);
Assert.Null(action.AttributeRouteModel); Assert.Null(action.AttributeRouteModel);
} }
@ -529,12 +530,12 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
Assert.Equal("Edit", action.ActionName); Assert.Equal("Edit", action.ActionName);
var httpMethod = Assert.Single(action.HttpMethods); var httpMethod = Assert.Single(action.HttpMethods);
Assert.Equal("POST", httpMethod); Assert.Equal("HEAD", httpMethod);
Assert.NotNull(action.AttributeRouteModel); Assert.NotNull(action.AttributeRouteModel);
Assert.Equal("Change", action.AttributeRouteModel.Template); Assert.Equal("Change", action.AttributeRouteModel.Template);
Assert.IsType<HttpPostAttribute>(Assert.Single(action.Attributes)); Assert.IsType<HttpHeadAttribute>(Assert.Single(action.Attributes));
} }
[Fact] [Fact]
@ -1010,7 +1011,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
[HttpPost("List")] [HttpPost("List")]
public void Index() { } public void Index() { }
[HttpPost("Change")] [HttpHead("Change")]
public void Edit() { } public void Edit() { }
public void Remove() { } public void Remove() { }
@ -1089,11 +1090,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
[CustomHttpMethods("PUT", "PATCH")] [CustomHttpMethods("PUT", "PATCH")]
public void Update() { } public void Update() { }
[HttpHead]
[HttpDelete] [HttpDelete]
public void Delete() { } public void Delete() { }
[HttpPost] [HttpPost]
[HttpGet] [HttpGet]
[HttpHead]
public void Details() { } public void Details() { }
[HttpGet] [HttpGet]

View File

@ -530,6 +530,7 @@ namespace Microsoft.AspNet.Mvc
[InlineData("POST")] [InlineData("POST")]
[InlineData("DELETE")] [InlineData("DELETE")]
[InlineData("PATCH")] [InlineData("PATCH")]
[InlineData("HEAD")]
public async Task HttpMethodAttribute_ActionWithMultipleHttpMethodAttributes_ORsMultipleHttpMethods(string verb) public async Task HttpMethodAttribute_ActionWithMultipleHttpMethodAttributes_ORsMultipleHttpMethods(string verb)
{ {
// Arrange // Arrange
@ -873,22 +874,6 @@ namespace Microsoft.AspNet.Mvc
} }
} }
private class HttpMethodAttributeTests_DefaultMethodValidationController
{
public void Index()
{
}
// Method with custom attribute.
[HttpGet]
public void Get()
{ }
// InvalidMethod ( since its private)
private void Post()
{ }
}
private class ActionNameController private class ActionNameController
{ {
[ActionName("CustomActionName_Verb")] [ActionName("CustomActionName_Verb")]
@ -914,6 +899,7 @@ namespace Microsoft.AspNet.Mvc
[HttpPost] [HttpPost]
[HttpDelete] [HttpDelete]
[HttpPatch] [HttpPatch]
[HttpHead]
public void Put() public void Put()
{ {
} }
@ -923,9 +909,5 @@ namespace Microsoft.AspNet.Mvc
{ {
} }
} }
private class HttpMethodAttributeTests_DerivedController : HttpMethodAttributeTests_RestOnlyController
{
}
} }
} }

View File

@ -28,6 +28,7 @@ namespace Microsoft.AspNet.Mvc
data.Add(new HttpPutAttribute(), new[] { "PUT" }); data.Add(new HttpPutAttribute(), new[] { "PUT" });
data.Add(new HttpPatchAttribute(), new[] { "PATCH" }); data.Add(new HttpPatchAttribute(), new[] { "PATCH" });
data.Add(new HttpDeleteAttribute(), new[] { "DELETE" }); data.Add(new HttpDeleteAttribute(), new[] { "DELETE" });
data.Add(new HttpHeadAttribute(), new[] { "HEAD" });
data.Add(new AcceptVerbsAttribute("MERGE", "OPTIONS"), new[] { "MERGE", "OPTIONS" }); data.Add(new AcceptVerbsAttribute("MERGE", "OPTIONS"), new[] { "MERGE", "OPTIONS" });
return data; return data;

View File

@ -26,6 +26,7 @@ namespace Microsoft.AspNet.Mvc
data.Add(new HttpPutAttribute()); data.Add(new HttpPutAttribute());
data.Add(new HttpPatchAttribute()); data.Add(new HttpPatchAttribute());
data.Add(new HttpDeleteAttribute()); data.Add(new HttpDeleteAttribute());
data.Add(new HttpHeadAttribute());
data.Add(new RouteAttribute("")); data.Add(new RouteAttribute(""));
return data; return data;