[Fixes #809] Multiple [Http*] verbs should create multiple actions

This commit is contained in:
jacalvar 2014-10-07 18:21:59 -07:00
parent 3f54492930
commit 39376617cc
2 changed files with 62 additions and 30 deletions

View File

@ -170,7 +170,21 @@ namespace Microsoft.AspNet.Mvc
{ {
var httpMethodProviders = actionAttributes.HttpMethodProviderAttributes; var httpMethodProviders = actionAttributes.HttpMethodProviderAttributes;
var httpMethods = httpMethodProviders.SelectMany(x => x.HttpMethods).Distinct().ToArray(); var httpMethods = httpMethodProviders.SelectMany(x => x.HttpMethods).Distinct().ToArray();
if (httpMethods.Length > 0)
{
foreach (var httpMethod in httpMethods)
{
yield return new ActionInfo()
{
HttpMethods = new string[] { httpMethod },
ActionName = actionName,
Attributes = actionAttributes.Attributes,
RequireActionNameMatch = true,
};
}
}
else
{
yield return new ActionInfo() yield return new ActionInfo()
{ {
HttpMethods = httpMethods, HttpMethods = httpMethods,
@ -179,6 +193,7 @@ namespace Microsoft.AspNet.Mvc
RequireActionNameMatch = true, RequireActionNameMatch = true,
}; };
} }
}
private static IEnumerable<ActionInfo> GetAttributeRoutedActions( private static IEnumerable<ActionInfo> GetAttributeRoutedActions(
ActionAttributes actionAttributes, ActionAttributes actionAttributes,

View File

@ -181,13 +181,18 @@ namespace Microsoft.AspNet.Mvc
var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo); var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);
// Assert // Assert
var action = Assert.Single(actionInfos); Assert.Equal(2, actionInfos.Count());
Assert.Single(actionInfos, a => a.HttpMethods.Contains("PUT"));
Assert.Single(actionInfos, a => a.HttpMethods.Contains("PATCH"));
foreach (var action in actionInfos)
{
Assert.Equal("Update", action.ActionName); Assert.Equal("Update", action.ActionName);
Assert.True(action.RequireActionNameMatch); Assert.True(action.RequireActionNameMatch);
Assert.Equal(new[] { "PUT", "PATCH" }, action.HttpMethods);
Assert.Null(action.AttributeRoute); Assert.Null(action.AttributeRoute);
Assert.IsType<CustomHttpMethodsAttribute>(Assert.Single(action.Attributes)); Assert.IsType<CustomHttpMethodsAttribute>(Assert.Single(action.Attributes));
} }
}
[Fact] [Fact]
public void GetActions_ConventionallyRoutedActionWithHttpConstraints_AndInvalidRouteTemplateProvider() public void GetActions_ConventionallyRoutedActionWithHttpConstraints_AndInvalidRouteTemplateProvider()
@ -224,16 +229,22 @@ namespace Microsoft.AspNet.Mvc
var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo); var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);
// Assert // Assert
var action = Assert.Single(actionInfos); Assert.Equal(2, actionInfos.Count());
Assert.Single(actionInfos, a => a.HttpMethods.Contains("GET"));
Assert.Single(actionInfos, a => a.HttpMethods.Contains("POST"));
foreach (var action in actionInfos)
{
Assert.Equal("Details", action.ActionName); Assert.Equal("Details", action.ActionName);
Assert.True(action.RequireActionNameMatch); Assert.True(action.RequireActionNameMatch);
Assert.Equal(new[] { "GET", "POST" }, action.HttpMethods.OrderBy(m => m, StringComparer.Ordinal));
Assert.Null(action.AttributeRoute); Assert.Null(action.AttributeRoute);
Assert.Equal(2, action.Attributes.Length); Assert.Equal(2, action.Attributes.Length);
Assert.Single(action.Attributes, a => a is HttpGetAttribute); Assert.Single(action.Attributes, a => a is HttpGetAttribute);
Assert.Single(action.Attributes, a => a is HttpPostAttribute); Assert.Single(action.Attributes, a => a is HttpPostAttribute);
} }
}
[Fact] [Fact]
public void GetActions_ConventionallyRoutedAction_WithMultipleOverlappingHttpConstraints() public void GetActions_ConventionallyRoutedAction_WithMultipleOverlappingHttpConstraints()
@ -247,10 +258,15 @@ namespace Microsoft.AspNet.Mvc
var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo); var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);
// Assert // Assert
var action = Assert.Single(actionInfos); Assert.Equal(3, actionInfos.Count());
Assert.Single(actionInfos, a => a.HttpMethods.Contains("GET"));
Assert.Single(actionInfos, a => a.HttpMethods.Contains("POST"));
Assert.Single(actionInfos, a => a.HttpMethods.Contains("PUT"));
foreach (var action in actionInfos)
{
Assert.Equal("List", action.ActionName); Assert.Equal("List", action.ActionName);
Assert.True(action.RequireActionNameMatch); Assert.True(action.RequireActionNameMatch);
Assert.Equal(new[] { "GET", "POST", "PUT" }, action.HttpMethods.OrderBy(m => m, StringComparer.Ordinal));
Assert.Null(action.AttributeRoute); Assert.Null(action.AttributeRoute);
Assert.Equal(3, action.Attributes.Length); Assert.Equal(3, action.Attributes.Length);
@ -258,6 +274,7 @@ namespace Microsoft.AspNet.Mvc
Assert.Single(action.Attributes, a => a is HttpGetAttribute); Assert.Single(action.Attributes, a => a is HttpGetAttribute);
Assert.Single(action.Attributes, a => a is AcceptVerbsAttribute); Assert.Single(action.Attributes, a => a is AcceptVerbsAttribute);
} }
}
[Fact] [Fact]
public void GetActions_AttributeRouteOnAction() public void GetActions_AttributeRouteOnAction()