Ver Fonte

Add test for HTTP method metadata order (#7225)

James Newton-King há 7 anos atrás
pai
commit
cc7b35439c

+ 49 - 0
src/Http/Routing/test/UnitTests/Builder/MapEndpointEndpointDataSourceBuilderExtensionsTest.cs

@@ -2,6 +2,8 @@
 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 
 using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Linq.Expressions;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Http;
@@ -153,10 +155,57 @@ namespace Microsoft.AspNetCore.Builder
             Assert.IsType<Metadata>(endpointBuilder1.Metadata[0]);
         }
 
+        [Fact]
+        public void MapEndpoint_PrecedenceOfMetadata_BuilderMetadataReturned()
+        {
+            // Arrange
+            var builder = new DefaultEndpointRouteBuilder();
+
+            // Act
+            var endpointBuilder = builder.MapGet("/", HandleHttpMetdata, new HttpMethodMetadata(new[] { "METHOD" }));
+            endpointBuilder.Add(b =>
+            {
+                b.Metadata.Add(new HttpMethodMetadata(new[] { "BUILDER" }));
+            });
+
+            // Assert
+            var dataSource = Assert.Single(builder.DataSources);
+            var endpoint = Assert.Single(dataSource.Endpoints);
+
+            Assert.Equal(4, endpoint.Metadata.Count);
+            Assert.Equal("ATTRIBUTE", GetMethod(endpoint.Metadata[0]));
+            Assert.Equal("GET", GetMethod(endpoint.Metadata[1]));
+            Assert.Equal("METHOD", GetMethod(endpoint.Metadata[2]));
+            Assert.Equal("BUILDER", GetMethod(endpoint.Metadata[3]));
+
+            Assert.Equal("BUILDER", endpoint.Metadata.GetMetadata<IHttpMethodMetadata>().HttpMethods.Single());
+
+            string GetMethod(object metadata)
+            {
+                var httpMethodMetadata = Assert.IsAssignableFrom<IHttpMethodMetadata>(metadata);
+                return Assert.Single(httpMethodMetadata.HttpMethods);
+            }
+        }
+
         [Attribute1]
         [Attribute2]
         private static Task Handle(HttpContext context) => Task.CompletedTask;
 
+        [HttpMethod("ATTRIBUTE")]
+        private static Task HandleHttpMetdata(HttpContext context) => Task.CompletedTask;
+
+        private class HttpMethodAttribute : Attribute, IHttpMethodMetadata
+        {
+            public bool AcceptCorsPreflight => false;
+
+            public IReadOnlyList<string> HttpMethods { get; }
+
+            public HttpMethodAttribute(params string[] httpMethods)
+            {
+                HttpMethods = httpMethods;
+            }
+        }
+
         private class Attribute1 : Attribute
         {
         }