8
8
using Microsoft . AspNetCore . Http ;
9
9
using Microsoft . AspNetCore . Http . Features ;
10
10
using Microsoft . Extensions . Logging . Abstractions ;
11
+ using Microsoft . Extensions . Options ;
11
12
using Moq ;
12
13
using Xunit ;
13
14
14
15
namespace Microsoft . AspNetCore . Routing
15
16
{
16
17
public class EndpointMiddlewareTest
17
18
{
19
+ private readonly IOptions < RouteOptions > RouteOptions = Options . Create ( new RouteOptions ( ) ) ;
20
+
18
21
[ Fact ]
19
22
public async Task Invoke_NoFeature_NoOps ( )
20
23
{
@@ -27,7 +30,7 @@ public async Task Invoke_NoFeature_NoOps()
27
30
return Task . CompletedTask ;
28
31
} ;
29
32
30
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
33
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
31
34
32
35
// Act
33
36
await middleware . Invoke ( httpContext ) ;
@@ -52,7 +55,7 @@ public async Task Invoke_NoEndpoint_NoOps()
52
55
return Task . CompletedTask ;
53
56
} ;
54
57
55
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
58
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
56
59
57
60
// Act
58
61
await middleware . Invoke ( httpContext ) ;
@@ -84,7 +87,7 @@ public async Task Invoke_WithEndpoint_InvokesDelegate()
84
87
return Task . CompletedTask ;
85
88
} ;
86
89
87
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next ) ;
90
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , next , RouteOptions ) ;
88
91
89
92
// Act
90
93
await middleware . Invoke ( httpContext ) ;
@@ -97,6 +100,9 @@ public async Task Invoke_WithEndpoint_InvokesDelegate()
97
100
public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMiddlewareNotInvoked ( )
98
101
{
99
102
// 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." ;
100
106
var httpContext = new DefaultHttpContext
101
107
{
102
108
RequestServices = new ServiceProvider ( )
@@ -107,13 +113,13 @@ public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMid
107
113
Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < IAuthorizeData > ( ) ) , "Test" ) ,
108
114
} ) ;
109
115
110
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
116
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
111
117
112
118
// Act & Assert
113
119
var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => middleware . Invoke ( httpContext ) ) ;
114
120
115
121
// 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 ) ;
117
123
}
118
124
119
125
[ Fact ]
@@ -132,18 +138,41 @@ public async Task Invoke_WithEndpoint_WorksIfAuthAttributesWereFound_AndAuthMidd
132
138
133
139
httpContext . Items [ EndpointMiddleware . AuthorizationMiddlewareInvokedKey ] = true ;
134
140
135
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
141
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
136
142
137
143
// Act & Assert
138
144
await middleware . Invoke ( httpContext ) ;
139
145
140
146
// If we got this far, we can sound the everything's OK alarm.
141
147
}
142
148
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
+
143
169
[ Fact ]
144
170
public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddlewareNotInvoked ( )
145
171
{
146
172
// 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." ;
147
176
var httpContext = new DefaultHttpContext
148
177
{
149
178
RequestServices = new ServiceProvider ( )
@@ -154,13 +183,13 @@ public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddle
154
183
Endpoint = new Endpoint ( _ => Task . CompletedTask , new EndpointMetadataCollection ( Mock . Of < ICorsMetadata > ( ) ) , "Test" ) ,
155
184
} ) ;
156
185
157
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
186
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
158
187
159
188
// Act & Assert
160
189
var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => middleware . Invoke ( httpContext ) ) ;
161
190
162
191
// 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 ) ;
164
193
}
165
194
166
195
[ Fact ]
@@ -179,14 +208,34 @@ public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlew
179
208
180
209
httpContext . Items [ EndpointMiddleware . CorsMiddlewareInvokedKey ] = true ;
181
210
182
- var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask ) ;
211
+ var middleware = new EndpointMiddleware ( NullLogger < EndpointMiddleware > . Instance , _ => Task . CompletedTask , RouteOptions ) ;
183
212
184
213
// Act & Assert
185
214
await middleware . Invoke ( httpContext ) ;
186
215
187
216
// If we got this far, we can sound the everything's OK alarm.
188
217
}
189
218
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
+
190
239
private class ServiceProvider : IServiceProvider
191
240
{
192
241
public object GetService ( Type serviceType )
0 commit comments