Skip to content

Commit b25c842

Browse files
committed
Start adding EndpointMethodInfoApiDescriptionProviderTests
1 parent 2c3051d commit b25c842

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/Mvc/Mvc.ApiExplorer/src/EndpointMethodInfoApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void OnProvidersExecuted(ApiDescriptionProviderContext context)
5252
{
5353
}
5454

55-
private static ApiDescription CreateApiDescription(RoutePattern pattern, string httpMethod, MethodInfo methodInfo)
55+
internal static ApiDescription CreateApiDescription(RoutePattern pattern, string httpMethod, MethodInfo methodInfo)
5656
{
5757
var apiDescription = new ApiDescription
5858
{
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc.Abstractions;
10+
using Microsoft.AspNetCore.Routing;
11+
using Microsoft.AspNetCore.Routing.Patterns;
12+
using Xunit;
13+
14+
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
15+
{
16+
public class EndpointMethodInfoApiDescriptionProviderTest
17+
{
18+
[Fact]
19+
public void ApiDescription_MultipleCreatedForMultipleHttpMethods()
20+
{
21+
Action action = () => { };
22+
var httpMethods = new string[] { "FOO", "BAR" };
23+
24+
var apiDescriptions = GetApiDescriptions(action.Method, httpMethods);
25+
26+
Assert.Equal(httpMethods.Length, apiDescriptions.Count);
27+
}
28+
29+
[Fact]
30+
public void ApiDescription_NotCreatedIfNoHttpMethods()
31+
{
32+
Action action = () => { };
33+
var emptyHttpMethods = new string[0];
34+
35+
var apiDescriptions = GetApiDescriptions(action.Method, emptyHttpMethods);
36+
37+
Assert.Empty(apiDescriptions);
38+
}
39+
40+
private IList<ApiDescription> GetApiDescriptions(
41+
MethodInfo methodInfo,
42+
IEnumerable<string> httpMethods = null,
43+
string pattern = null)
44+
{
45+
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
46+
47+
var httpMethodMetadata = new HttpMethodMetadata(httpMethods ?? new[] { "GET" });
48+
var endpointMetadata = new EndpointMetadataCollection(methodInfo, httpMethodMetadata);
49+
var routePattern = RoutePatternFactory.Pattern(pattern ?? "/");
50+
51+
var endpoint = new RouteEndpoint(httpContext => Task.CompletedTask, routePattern, 0, endpointMetadata, null);
52+
var endpointDataSource = new DefaultEndpointDataSource(endpoint);
53+
54+
var provider = new EndpointMethodInfoApiDescriptionProvider(endpointDataSource);
55+
56+
provider.OnProvidersExecuting(context);
57+
provider.OnProvidersExecuted(context);
58+
59+
return context.Results;
60+
}
61+
}
62+
}

src/Shared/RequestDelegateFactoryUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static class RequestDelegateFactoryUtilities
1212
{
1313
private static readonly MethodInfo EnumTryParseMethod = GetEnumTryParseMethod();
1414

15-
// Since this is shared source the cache won't be shared between RequestDelegateFactory and the ApiDescriptionProvider sadly :(
15+
// Since this is shared source, the cache won't be shared between RequestDelegateFactory and the ApiDescriptionProvider sadly :(
1616
private static readonly ConcurrentDictionary<Type, MethodInfo?> TryParseMethodCache = new();
1717

1818
public static bool HasTryParseMethod(ParameterInfo parameter)

0 commit comments

Comments
 (0)