Skip to content

Commit f9c248a

Browse files
committed
Add MapActionEndpointDataSourceBuilderExtensionsTest
1 parent 7782b6c commit f9c248a

File tree

3 files changed

+78
-29
lines changed

3 files changed

+78
-29
lines changed

src/Http/Routing/src/MapAction/MapActionEndpointRouteBuilderExtensions.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static IEndpointConventionBuilder MapAction(
3737
}
3838

3939
var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(action);
40-
var httpMethodMetadata = GenerateHtpMethodMetadata(action.Method);
4140

4241
var routeAttributes = action.Method.GetCustomAttributes().OfType<IRouteTemplateProvider>();
4342
var conventionBuilders = new List<IEndpointConventionBuilder>();
@@ -57,11 +56,6 @@ public static IEndpointConventionBuilder MapAction(
5756
{
5857
endpointBuilder.Metadata.Add(attribute);
5958
}
60-
61-
if (httpMethodMetadata is not null)
62-
{
63-
endpointBuilder.Metadata.Add(httpMethodMetadata);
64-
}
6559
});
6660

6761
conventionBuilders.Add(conventionBuilder);
@@ -75,23 +69,6 @@ public static IEndpointConventionBuilder MapAction(
7569
return new CompositeEndpointConventionBuilder(conventionBuilders);
7670
}
7771

78-
private static HttpMethodMetadata? GenerateHtpMethodMetadata(MethodInfo methodInfo)
79-
{
80-
var httpMethods = methodInfo
81-
.GetCustomAttributes()
82-
.OfType<IHttpMethodMetadata>()
83-
.SelectMany(a => a.HttpMethods)
84-
.Distinct(StringComparer.OrdinalIgnoreCase)
85-
.ToArray();
86-
87-
if (httpMethods.Length == 0)
88-
{
89-
return null;
90-
}
91-
92-
return new HttpMethodMetadata(httpMethods);
93-
}
94-
9572
private class CompositeEndpointConventionBuilder : IEndpointConventionBuilder
9673
{
9774
private readonly List<IEndpointConventionBuilder> _endpointConventionBuilders;

src/Http/Routing/test/FunctionalTests/MapActionTest.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MapActionTest
2424
[Fact]
2525
public async Task MapAction_FromBodyWorksWithJsonPayload()
2626
{
27-
[HttpPost("/EchoTodo")]
27+
[HttpMethods(new[] { "POST" }, "/EchoTodo")]
2828
Todo EchoTodo([FromBody] Todo todo) => todo;
2929

3030
using var host = new HostBuilder()
@@ -51,7 +51,7 @@ public async Task MapAction_FromBodyWorksWithJsonPayload()
5151

5252
var todo = new Todo
5353
{
54-
Name = "Custom Todo"
54+
Name = "Write tests!"
5555
};
5656

5757
var response = await client.PostAsJsonAsync("/EchoTodo", todo);
@@ -71,16 +71,17 @@ private class Todo
7171

7272
private class FromBodyAttribute : Attribute, IFromBodyMetadata { }
7373

74-
private class HttpPostAttribute : Attribute, IRouteTemplateProvider, IHttpMethodMetadata
74+
private class HttpMethodsAttribute : Attribute, IRouteTemplateProvider, IHttpMethodMetadata
7575
{
76-
public HttpPostAttribute(string template)
76+
public HttpMethodsAttribute(string[] httpMethods, string? template)
7777
{
78+
HttpMethods = httpMethods;
7879
Template = template;
7980
}
8081

81-
public string Template { get; }
82+
public string? Template { get; }
8283

83-
public IReadOnlyList<string> HttpMethods { get; } = new[] { "POST" };
84+
public IReadOnlyList<string> HttpMethods { get; }
8485

8586
public int? Order => null;
8687

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Mvc.Routing;
11+
using Microsoft.AspNetCore.Routing;
12+
using Moq;
13+
using Xunit;
14+
15+
namespace Microsoft.AspNetCore.Builder
16+
{
17+
public class MapActionEndpointDataSourceBuilderExtensionsTest
18+
{
19+
private ModelEndpointDataSource GetBuilderEndpointDataSource(IEndpointRouteBuilder endpointRouteBuilder)
20+
{
21+
return Assert.IsType<ModelEndpointDataSource>(Assert.Single(endpointRouteBuilder.DataSources));
22+
}
23+
24+
private RouteEndpointBuilder GetRouteEndpointBuilder(IEndpointRouteBuilder endpointRouteBuilder)
25+
{
26+
return Assert.IsType<RouteEndpointBuilder>(Assert.Single(GetBuilderEndpointDataSource(endpointRouteBuilder).EndpointBuilders));
27+
}
28+
29+
[Fact]
30+
public void MapAction_BuildsEndpointFromAttributes()
31+
{
32+
const string customMethod = "CUSTOM_METHOD";
33+
const string customTemplate = "/CustomTemplate";
34+
35+
[HttpMethods(new[] { customMethod }, customTemplate)]
36+
void MyAction() { };
37+
38+
var builder = new DefaultEndpointRouteBuilder(Mock.Of<IApplicationBuilder>());
39+
var endpointBuilder = builder.MapAction((Action)MyAction);
40+
41+
var dataSource = Assert.Single(builder.DataSources);
42+
var endpoint = Assert.Single(dataSource.Endpoints);
43+
44+
var httpMethodMetadata = Assert.Single(endpoint.Metadata.OfType<IHttpMethodMetadata>());
45+
var method = Assert.Single(httpMethodMetadata.HttpMethods);
46+
Assert.Equal(customMethod, method);
47+
48+
var routeEndpointBuilder = GetRouteEndpointBuilder(builder);
49+
Assert.Equal(customTemplate, routeEndpointBuilder.RoutePattern.RawText);
50+
}
51+
52+
private class HttpMethodsAttribute : Attribute, IRouteTemplateProvider, IHttpMethodMetadata
53+
{
54+
public HttpMethodsAttribute(string[] httpMethods, string? template)
55+
{
56+
HttpMethods = httpMethods;
57+
Template = template;
58+
}
59+
60+
public string? Template { get; }
61+
62+
public IReadOnlyList<string> HttpMethods { get; }
63+
64+
public int? Order => null;
65+
66+
public string? Name => null;
67+
68+
public bool AcceptCorsPreflight => false;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)