public void CustomRoutingServiceCollectionShouldReplaceTheDefaultOne()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                var servicesClone = new ServiceCollection {
                    services
                };

                servicesClone.AddTransient <IInjectedService, InjectedService>();

                var routingServices = new RoutingServices
                {
                    ServiceCollection = servicesClone
                };

                services.ReplaceSingleton <IRoutingServices>(routingServices);
            });

            var defaultService = TestApplication.Services.GetService <IInjectedService>();
            var routingService = TestApplication.RoutingServices.GetService <IInjectedService>();

            Assert.Null(defaultService);
            Assert.NotNull(routingService);

            MyApplication
            .IsRunningOn(server => server
                         .WithStartup <DefaultStartup>());
        }
        public void MissingRoutingServiceCollectionShouldThrowException()
        {
            Test.AssertException <InvalidOperationException>(
                () =>
            {
                MyApplication
                .StartsFrom <DefaultStartup>()
                .WithServices(services =>
                {
                    var routingServices = new RoutingServices
                    {
                        ServiceCollection = null
                    };

                    services.ReplaceSingleton <IRoutingServices>(routingServices);
                });

                // This call ensures services are loaded (uses lazy loading).
                var setupServices = TestApplication.Services;
            },
                "Route testing requires the registered IRoutingServices service implementation to provide test routing services by either the ServiceProvider property or the ServiceCollection one.");

            MyApplication
            .IsRunningOn(server => server
                         .WithStartup <DefaultStartup>());
        }
Exemplo n.º 3
0
        public static IRoutingServices AddRoutingServices(this IServiceCollection serviceCollection)
        {
            var routingServices = new RoutingServices
            {
                ServiceCollection = serviceCollection
            };

            serviceCollection.AddSingleton <IRoutingServices>(routingServices);

            return(routingServices);
        }
Exemplo n.º 4
0
        public void Configure(IApplicationBuilder builder)
        {
            builder.UseServices(services =>
            {
                services.Add(RoutingServices.GetDefaultServices());
            });

            var endpoint1 = new DelegateRouteEndpoint(async(context) =>
                                                      await context
                                                      .HttpContext
                                                      .Response
                                                      .WriteAsync(
                                                          "match1, route values -" + context.RouteData.Values.Print()));

            var endpoint2 = new DelegateRouteEndpoint(async(context) =>
                                                      await context
                                                      .HttpContext
                                                      .Response
                                                      .WriteAsync("Hello, World!"));

            var routeBuilder = new RouteBuilder();

            routeBuilder.DefaultHandler  = endpoint1;
            routeBuilder.ServiceProvider = builder.ApplicationServices;

            routeBuilder.AddPrefixRoute("api/store");

            routeBuilder.MapRoute("defaultRoute",
                                  "api/constraint/{controller}",
                                  null,
                                  new { controller = "my.*" });
            routeBuilder.MapRoute("regexStringRoute",
                                  "api/rconstraint/{controller}",
                                  new { foo = "Bar" },
                                  new { controller = new RegexRouteConstraint("^(my.*)$") });
            routeBuilder.MapRoute("regexRoute",
                                  "api/r2constraint/{controller}",
                                  new { foo = "Bar2" },
                                  new { controller = new RegexRouteConstraint(new Regex("^(my.*)$")) });

            routeBuilder.MapRoute("parameterConstraintRoute",
                                  "api/{controller}/{*extra}",
                                  new { controller = "Store" });

            routeBuilder.AddPrefixRoute("hello/world", endpoint2);
            routeBuilder.AddPrefixRoute("", endpoint2);
            builder.UseRouter(routeBuilder.Build());
        }
        public void AddMvcUniverseTestingWithRoutingTestingServicesShouldNotOverrideThem()
        {
            IRoutingServices routingServices = new RoutingServices();

            var services = new ServiceCollection();

            services.AddMvc();
            services.AddSingleton(typeof(IRoutingServices), routingServices);

            Assert.NotNull(services.BuildServiceProvider().GetService <IRoutingServices>());

            services.AddMvcUniverseTesting();
            var actualRoutingServices = services.BuildServiceProvider().GetService <IRoutingServices>();

            Assert.NotNull(routingServices);
            Assert.IsAssignableFrom <IRoutingServices>(routingServices);
            Assert.Same(routingServices, actualRoutingServices);
        }