From 0978a7dd166472f30ac40138fcc71da4cfb1df61 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 1 Jul 2016 09:29:02 -0700 Subject: [PATCH] Add some tests, rename misnamed file --- ...ensions.cs => RoutingBuilderExtensions.cs} | 8 ++ .../RoutingBuilderExtensionsTest.cs | 116 ++++++++++++++++++ 2 files changed, 124 insertions(+) rename src/Microsoft.AspNetCore.Routing/{BuilderExtensions.cs => RoutingBuilderExtensions.cs} (88%) create mode 100644 test/Microsoft.AspNetCore.Routing.Tests/RoutingBuilderExtensionsTest.cs diff --git a/src/Microsoft.AspNetCore.Routing/BuilderExtensions.cs b/src/Microsoft.AspNetCore.Routing/RoutingBuilderExtensions.cs similarity index 88% rename from src/Microsoft.AspNetCore.Routing/BuilderExtensions.cs rename to src/Microsoft.AspNetCore.Routing/RoutingBuilderExtensions.cs index 2ec4318737..cc2e2457e8 100644 --- a/src/Microsoft.AspNetCore.Routing/BuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Routing/RoutingBuilderExtensions.cs @@ -61,6 +61,14 @@ namespace Microsoft.AspNetCore.Builder throw new ArgumentNullException(nameof(action)); } + if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null) + { + throw new InvalidOperationException(Resources.FormatUnableToFindServices( + nameof(IServiceCollection), + nameof(RoutingServiceCollectionExtensions.AddRouting), + "ConfigureServices(...)")); + } + var routeBuilder = new RouteBuilder(builder); action(routeBuilder); diff --git a/test/Microsoft.AspNetCore.Routing.Tests/RoutingBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/RoutingBuilderExtensionsTest.cs new file mode 100644 index 0000000000..47eda87d45 --- /dev/null +++ b/test/Microsoft.AspNetCore.Routing.Tests/RoutingBuilderExtensionsTest.cs @@ -0,0 +1,116 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder.Internal; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.Routing.Internal; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Xunit; + +namespace Microsoft.AspNetCore.Builder +{ + public class RoutingBuilderExtensionsTest + { + [Fact] + public void UseRouter_IRouter_ThrowsWithoutCallingAddRouting() + { + // Arrange + var app = new ApplicationBuilder(Mock.Of()); + + // Act + var ex = Assert.Throws(() => app.UseRouter(Mock.Of())); + + // Assert + Assert.Equal( + "Unable to find the required services. " + + "Please add all the required services by calling 'IServiceCollection.AddRouting' " + + "inside the call to 'ConfigureServices(...)' in the application startup code.", + ex.Message); + } + + [Fact] + public void UseRouter_Action_ThrowsWithoutCallingAddRouting() + { + // Arrange + var app = new ApplicationBuilder(Mock.Of()); + + // Act + var ex = Assert.Throws(() => app.UseRouter(b => { })); + + // Assert + Assert.Equal( + "Unable to find the required services. " + + "Please add all the required services by calling 'IServiceCollection.AddRouting' " + + "inside the call to 'ConfigureServices(...)' in the application startup code.", + ex.Message); + } + + [Fact] + public async Task UseRouter_IRouter_CallsRoute() + { + // Arrange + var services = CreateServices(); + + var app = new ApplicationBuilder(services); + + var router = new Mock(MockBehavior.Strict); + router + .Setup(r => r.RouteAsync(It.IsAny())) + .Returns(TaskCache.CompletedTask) + .Verifiable(); + + app.UseRouter(router.Object); + + var appFunc = app.Build(); + + // Act + await appFunc(new DefaultHttpContext()); + + // Assert + router.Verify(); + } + + [Fact] + public async Task UseRouter_Action_CallsRoute() + { + // Arrange + var services = CreateServices(); + + var app = new ApplicationBuilder(services); + + var router = new Mock(MockBehavior.Strict); + router + .Setup(r => r.RouteAsync(It.IsAny())) + .Returns(TaskCache.CompletedTask) + .Verifiable(); + + app.UseRouter(b => + { + b.Routes.Add(router.Object); + }); + + var appFunc = app.Build(); + + // Act + await appFunc(new DefaultHttpContext()); + + // Assert + router.Verify(); + } + + private IServiceProvider CreateServices() + { + var services = new ServiceCollection(); + + services.AddLogging(); + services.AddOptions(); + services.AddRouting(); + + return services.BuildServiceProvider(); + } + } +}