|
1 | | -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Authorization; |
| 7 | +using Microsoft.AspNetCore.Cors.Infrastructure; |
6 | 8 | using Microsoft.AspNetCore.Http; |
7 | 9 | using Microsoft.AspNetCore.Http.Features; |
8 | 10 | using Microsoft.Extensions.Logging.Abstractions; |
| 11 | +using Moq; |
9 | 12 | using Xunit; |
10 | 13 |
|
11 | 14 | namespace Microsoft.AspNetCore.Routing |
@@ -90,6 +93,100 @@ public async Task Invoke_WithEndpoint_InvokesDelegate() |
90 | 93 | Assert.True(invoked); |
91 | 94 | } |
92 | 95 |
|
| 96 | + [Fact] |
| 97 | + public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMiddlewareNotInvoked() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + var httpContext = new DefaultHttpContext |
| 101 | + { |
| 102 | + RequestServices = new ServiceProvider() |
| 103 | + }; |
| 104 | + |
| 105 | + httpContext.Features.Set<IEndpointFeature>(new EndpointSelectorContext() |
| 106 | + { |
| 107 | + Endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of<IAuthorizeData>()), "Test"), |
| 108 | + }); |
| 109 | + |
| 110 | + var middleware = new EndpointMiddleware(NullLogger<EndpointMiddleware>.Instance, _ => Task.CompletedTask); |
| 111 | + |
| 112 | + // Act & Assert |
| 113 | + var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => middleware.Invoke(httpContext)); |
| 114 | + |
| 115 | + // Assert |
| 116 | + Assert.Equal("Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization.", ex.Message); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public async Task Invoke_WithEndpoint_WorksIfAuthAttributesWereFound_AndAuthMiddlewareInvoked() |
| 121 | + { |
| 122 | + // Arrange |
| 123 | + var httpContext = new DefaultHttpContext |
| 124 | + { |
| 125 | + RequestServices = new ServiceProvider() |
| 126 | + }; |
| 127 | + |
| 128 | + httpContext.Features.Set<IEndpointFeature>(new EndpointSelectorContext() |
| 129 | + { |
| 130 | + Endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of<IAuthorizeData>()), "Test"), |
| 131 | + }); |
| 132 | + |
| 133 | + httpContext.Items[EndpointMiddleware.AuthorizationMiddlewareInvokedKey] = true; |
| 134 | + |
| 135 | + var middleware = new EndpointMiddleware(NullLogger<EndpointMiddleware>.Instance, _ => Task.CompletedTask); |
| 136 | + |
| 137 | + // Act & Assert |
| 138 | + await middleware.Invoke(httpContext); |
| 139 | + |
| 140 | + // If we got this far, we can sound the everything's OK alarm. |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddlewareNotInvoked() |
| 145 | + { |
| 146 | + // Arrange |
| 147 | + var httpContext = new DefaultHttpContext |
| 148 | + { |
| 149 | + RequestServices = new ServiceProvider() |
| 150 | + }; |
| 151 | + |
| 152 | + httpContext.Features.Set<IEndpointFeature>(new EndpointSelectorContext() |
| 153 | + { |
| 154 | + Endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of<ICorsMetadata>()), "Test"), |
| 155 | + }); |
| 156 | + |
| 157 | + var middleware = new EndpointMiddleware(NullLogger<EndpointMiddleware>.Instance, _ => Task.CompletedTask); |
| 158 | + |
| 159 | + // Act & Assert |
| 160 | + var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => middleware.Invoke(httpContext)); |
| 161 | + |
| 162 | + // Assert |
| 163 | + Assert.Equal("Endpoint Test contains CORS metadata, but a middleware was not found that supports CORS.", ex.Message); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlewareInvoked() |
| 168 | + { |
| 169 | + // Arrange |
| 170 | + var httpContext = new DefaultHttpContext |
| 171 | + { |
| 172 | + RequestServices = new ServiceProvider() |
| 173 | + }; |
| 174 | + |
| 175 | + httpContext.Features.Set<IEndpointFeature>(new EndpointSelectorContext() |
| 176 | + { |
| 177 | + Endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of<ICorsMetadata>()), "Test"), |
| 178 | + }); |
| 179 | + |
| 180 | + httpContext.Items[EndpointMiddleware.CorsMiddlewareInvokedKey] = true; |
| 181 | + |
| 182 | + var middleware = new EndpointMiddleware(NullLogger<EndpointMiddleware>.Instance, _ => Task.CompletedTask); |
| 183 | + |
| 184 | + // Act & Assert |
| 185 | + await middleware.Invoke(httpContext); |
| 186 | + |
| 187 | + // If we got this far, we can sound the everything's OK alarm. |
| 188 | + } |
| 189 | + |
93 | 190 | private class ServiceProvider : IServiceProvider |
94 | 191 | { |
95 | 192 | public object GetService(Type serviceType) |
|
0 commit comments