|
| 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 | +} |
0 commit comments