Add test for HTTP method metadata order (#7225)
This commit is contained in:
parent
6b7e821913
commit
cc7b35439c
|
|
@ -2,6 +2,8 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
@ -153,10 +155,57 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
Assert.IsType<Metadata>(endpointBuilder1.Metadata[0]);
|
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]
|
[Attribute1]
|
||||||
[Attribute2]
|
[Attribute2]
|
||||||
private static Task Handle(HttpContext context) => Task.CompletedTask;
|
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
|
private class Attribute1 : Attribute
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue