88using Microsoft . AspNetCore . Http ;
99using Microsoft . AspNetCore . Http . Features ;
1010using Microsoft . Extensions . Logging . Abstractions ;
11+ using Microsoft . Extensions . Options ;
1112using Moq ;
1213using Xunit ;
1314
1415namespace Microsoft . AspNetCore . Routing
1516{
1617 public class EndpointMiddlewareTest
1718 {
19+ private readonly IOptions < RouteOptions > RouteOptions = Options . Create ( new RouteOptions ( ) ) ;
20+
1821 [ Fact ]
1922 public async Task Invoke_NoFeature_NoOps ( )
2023 {
@@ -27,7 +30,7 @@ public async Task Invoke_NoFeature_NoOps()
2730 return Task . CompletedTask ;
2831 } ;
2932
30- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
33+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
3134
3235 // Act
3336 await middleware . Invoke ( httpContext ) ;
@@ -52,7 +55,7 @@ public async Task Invoke_NoEndpoint_NoOps()
5255 return Task . CompletedTask ;
5356 } ;
5457
55- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
58+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
5659
5760 // Act
5861 await middleware . Invoke ( httpContext ) ;
@@ -84,7 +87,7 @@ public async Task Invoke_WithEndpoint_InvokesDelegate()
8487 return Task . CompletedTask ;
8588 } ;
8689
87- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
90+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
8891
8992 // Act
9093 await middleware . Invoke ( httpContext ) ;
@@ -97,6 +100,9 @@ public async Task Invoke_WithEndpoint_InvokesDelegate()
97100 public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMiddlewareNotInvoked ( )
98101 {
99102 // Arrange
103+ var expected = "Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization." +
104+ Environment . NewLine +
105+ "Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code." ;
100106 var httpContext = new DefaultHttpContext
101107 {
102108 RequestServices = new ServiceProvider ( )
@@ -107,13 +113,13 @@ public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMid
107113 Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < IAuthorizeData > ( ) ) , "Test" ) ,
108114 } ) ;
109115
110- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
116+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
111117
112118 // Act & Assert
113119 var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => middleware . Invoke ( httpContext ) ) ;
114120
115121 // Assert
116- Assert . Equal ( "Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization." , ex . Message ) ;
122+ Assert . Equal ( expected , ex . Message ) ;
117123 }
118124
119125 [ Fact ]
@@ -132,18 +138,41 @@ public async Task Invoke_WithEndpoint_WorksIfAuthAttributesWereFound_AndAuthMidd
132138
133139 httpContext . Items [ EndpointMiddleware . AuthorizationMiddlewareInvokedKey ] = true ;
134140
135- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
141+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
136142
137143 // Act & Assert
138144 await middleware . Invoke ( httpContext ) ;
139145
140146 // If we got this far, we can sound the everything's OK alarm.
141147 }
142148
149+ [ Fact ]
150+ public async Task Invoke_WithEndpoint_DoesNotThrowIfUnevaluatedAuthAttributesWereFound_ButSuppressedViaOptions ( )
151+ {
152+ // Arrange
153+ var httpContext = new DefaultHttpContext
154+ {
155+ RequestServices = new ServiceProvider ( )
156+ } ;
157+
158+ httpContext . Features . Set < IEndpointFeature > ( new EndpointSelectorContext ( )
159+ {
160+ Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < IAuthorizeData > ( ) ) , "Test" ) ,
161+ } ) ;
162+ var routeOptions = Options . Create ( new RouteOptions { SuppressCheckForUnevaluatedEndpointMetadata = true } ) ;
163+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , routeOptions ) ;
164+
165+ // Act & Assert
166+ await middleware . Invoke ( httpContext ) ;
167+ }
168+
143169 [ Fact ]
144170 public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddlewareNotInvoked ( )
145171 {
146172 // Arrange
173+ var expected = "Endpoint Test contains CORS metadata, but a middleware was not found that supports CORS." +
174+ Environment . NewLine +
175+ "Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code." ;
147176 var httpContext = new DefaultHttpContext
148177 {
149178 RequestServices = new ServiceProvider ( )
@@ -154,13 +183,13 @@ public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddle
154183 Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < ICorsMetadata > ( ) ) , "Test" ) ,
155184 } ) ;
156185
157- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
186+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
158187
159188 // Act & Assert
160189 var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => middleware . Invoke ( httpContext ) ) ;
161190
162191 // Assert
163- Assert . Equal ( "Endpoint Test contains CORS metadata, but a middleware was not found that supports CORS." , ex . Message ) ;
192+ Assert . Equal ( expected , ex . Message ) ;
164193 }
165194
166195 [ Fact ]
@@ -179,14 +208,34 @@ public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlew
179208
180209 httpContext . Items [ EndpointMiddleware . CorsMiddlewareInvokedKey ] = true ;
181210
182- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
211+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
183212
184213 // Act & Assert
185214 await middleware . Invoke ( httpContext ) ;
186215
187216 // If we got this far, we can sound the everything's OK alarm.
188217 }
189218
219+ [ Fact ]
220+ public async Task Invoke_WithEndpoint_DoesNotThrowIfUnevaluatedCorsAttributesWereFound_ButSuppressedViaOptions ( )
221+ {
222+ // Arrange
223+ var httpContext = new DefaultHttpContext
224+ {
225+ RequestServices = new ServiceProvider ( )
226+ } ;
227+
228+ httpContext . Features . Set < IEndpointFeature > ( new EndpointSelectorContext ( )
229+ {
230+ Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < IAuthorizeData > ( ) ) , "Test" ) ,
231+ } ) ;
232+ var routeOptions = Options . Create ( new RouteOptions { SuppressCheckForUnevaluatedEndpointMetadata = true } ) ;
233+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , routeOptions ) ;
234+
235+ // Act & Assert
236+ await middleware . Invoke ( httpContext ) ;
237+ }
238+
190239 private class ServiceProvider : IServiceProvider
191240 {
192241 public object GetService ( Type serviceType )
0 commit comments