public async Task Invoke_WithEndpoint_InvokesDelegate() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.RequestServices = new ServiceProvider(); var invoked = false; RequestDelegate endpointFunc = (c) => { invoked = true; return(Task.CompletedTask); }; httpContext.SetEndpoint(new Endpoint(endpointFunc, EndpointMetadataCollection.Empty, "Test")); RequestDelegate next = (c) => { return(Task.CompletedTask); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions); // Act await middleware.Invoke(httpContext); // Assert Assert.True(invoked); }
public async Task Invoke_WithEndpoint_DoesNotThrowIfUnhandledCorsAttributesWereFound_ButSuppressedViaOptions() { // Arrange var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; var calledEndpoint = false; RequestDelegate endpointFunc = (c) => { calledEndpoint = true; return(Task.CompletedTask); }; httpContext.SetEndpoint(new Endpoint(endpointFunc, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test")); var routeOptions = Options.Create(new RouteOptions { SuppressCheckForUnhandledSecurityMetadata = true }); RequestDelegate next = (c) => { throw new InvalidTimeZoneException("Should not be called"); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, routeOptions); // Act await middleware.Invoke(httpContext); // Assert Assert.True(calledEndpoint); }
public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlewareInvoked() { // Arrange var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; var calledEndpoint = false; RequestDelegate endpointFunc = (c) => { calledEndpoint = true; return(Task.CompletedTask); }; httpContext.SetEndpoint(new Endpoint(endpointFunc, new EndpointMetadataCollection(Mock.Of <ICorsMetadata>()), "Test")); httpContext.Items[EndpointMiddleware.CorsMiddlewareInvokedKey] = true; RequestDelegate next = (c) => { throw new InvalidTimeZoneException("Should not be called"); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions); // Act await middleware.Invoke(httpContext); // Assert Assert.True(calledEndpoint); }
public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddlewareNotInvoked() { // Arrange var expected = "Endpoint Test contains CORS metadata, but a middleware was not found that supports CORS." + Environment.NewLine + "Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. " + "The call to app.UseCors() must appear between app.UseRouting() and app.UseEndpoints(...)."; var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; RequestDelegate throwIfCalled = (c) => { throw new InvalidTimeZoneException("Should not be called"); }; httpContext.SetEndpoint(new Endpoint(throwIfCalled, new EndpointMetadataCollection(Mock.Of <ICorsMetadata>()), "Test")); var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, throwIfCalled, RouteOptions); // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => middleware.Invoke(httpContext)); // Assert Assert.Equal(expected, ex.Message); }
public async Task Invoke_NoFeature_ThrowFriendlyErrorMessage() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.RequestServices = new ServiceProvider(); RequestDelegate next = (c) => { return(Task.FromResult <object>(null)); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next); // Act var invokeTask = middleware.Invoke(httpContext); // Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await invokeTask); Assert.Equal( "Unable to execute an endpoint because the EndpointRoutingMiddleware was not run for this request. " + "Ensure EndpointRoutingMiddleware is added to the request execution pipeline before EndpointMiddleware " + "in application startup code.", ex.Message); }
public async Task Invoke_WithEndpoint_DoesNotThrowIfUnhandledCorsAttributesWereFound_ButSuppressedViaOptions() { // Arrange var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; httpContext.SetEndpoint(new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test")); var routeOptions = Options.Create(new RouteOptions { SuppressCheckForUnhandledSecurityMetadata = true }); var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, _ => Task.CompletedTask, routeOptions); // Act & Assert await middleware.Invoke(httpContext); }
public async Task Invoke_NoFeature_NoOps() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.RequestServices = new ServiceProvider(); RequestDelegate next = (c) => { return(Task.CompletedTask); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions); // Act await middleware.Invoke(httpContext); // Assert - does not throw }
public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlewareInvoked() { // Arrange var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; httpContext.SetEndpoint(new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of <ICorsMetadata>()), "Test")); httpContext.Items[EndpointMiddleware.CorsMiddlewareInvokedKey] = true; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, _ => Task.CompletedTask, RouteOptions); // Act & Assert await middleware.Invoke(httpContext); // If we got this far, we can sound the everything's OK alarm. }
public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMiddlewareNotInvoked() { // Arrange var expected = "Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization." + Environment.NewLine + "Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code."; var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; httpContext.SetEndpoint(new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test")); var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, _ => Task.CompletedTask, RouteOptions); // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => middleware.Invoke(httpContext)); // Assert Assert.Equal(expected, ex.Message); }
public async Task Invoke_WithEndpoint_WorksIfAuthAttributesWereFound_AndAuthMiddlewareInvoked() { // Arrange var httpContext = new DefaultHttpContext { RequestServices = new ServiceProvider() }; httpContext.Features.Set <IEndpointFeature>(new EndpointSelectorContext() { Endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test"), }); httpContext.Items[EndpointMiddleware.AuthorizationMiddlewareInvokedKey] = true; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, _ => Task.CompletedTask, RouteOptions); // Act & Assert await middleware.Invoke(httpContext); // If we got this far, we can sound the everything's OK alarm. }
public async Task Invoke_NoEndpoint_NoOps() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.RequestServices = new ServiceProvider(); httpContext.SetEndpoint(null); var calledNext = false; RequestDelegate next = (c) => { calledNext = true; return(Task.CompletedTask); }; var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions); // Act await middleware.Invoke(httpContext); // Assert Assert.True(calledNext); }