Пример #1
0
    public void AddRazorPagesOptions_AddsConventions()
    {
        // Arrange
        var services = new ServiceCollection().AddOptions()
                       .AddSingleton <IConfigureOptions <RazorPagesOptions>, RazorPagesOptionsSetup>();
        var applicationModelConvention = Mock.Of <IPageApplicationModelConvention>();
        var routeModelConvention       = Mock.Of <IPageRouteModelConvention>();
        var builder = new MvcBuilder(services, new ApplicationPartManager());

        builder.AddRazorPagesOptions(options =>
        {
            options.Conventions.Add(applicationModelConvention);
            options.Conventions.Add(routeModelConvention);
        });
        var serviceProvider = services.BuildServiceProvider();
        var accessor        = serviceProvider.GetRequiredService <IOptions <RazorPagesOptions> >();

        // Act & Assert
        var conventions = accessor.Value.Conventions;

        // Assert
        Assert.Collection(
            conventions,
            convention => Assert.Same(applicationModelConvention, convention),
            convention => Assert.Same(routeModelConvention, convention));
    }
        public MvcBuilderExtensionsTestsFixture()
        {
            var mvcBuilder = new MvcBuilder(new ServiceCollection(), new ApplicationPartManager());

            mvcBuilder.AddPugzor();
            Services = mvcBuilder.Services.Select(s => s.ServiceType).ToList();
        }
    public void ViewLocationFormats_ContainsExpectedLocations()
    {
        // Arrange
        var services      = new ServiceCollection().AddOptions();
        var viewLocations = new[]
        {
            "/MvcViews/{1}/{0}.cshtml",
            "/MvcViews/Shared/{0}.cshtml"
        };
        var builder = new MvcBuilder(services, new ApplicationPartManager());

        builder.AddRazorOptions(options =>
        {
            options.ViewLocationFormats.Clear();

            foreach (var location in viewLocations)
            {
                options.ViewLocationFormats.Add(location);
            }
        });
        var serviceProvider = services.BuildServiceProvider();
        var accessor        = serviceProvider.GetRequiredService <IOptions <RazorViewEngineOptions> >();

        // Act
        var formats = accessor.Value.ViewLocationFormats;

        // Assert
        Assert.Equal(viewLocations, formats, StringComparer.Ordinal);
    }
        public void ConfigureApiBehaviorOptions_InvokesSetupAction()
        {
            // Arrange
            var serviceCollection = new ServiceCollection()
                                    .AddOptions();

            var builder = new MvcBuilder(
                serviceCollection,
                new ApplicationPartManager());

            var part = new TestApplicationPart();

            // Act
            var result = builder.ConfigureApiBehaviorOptions(o =>
            {
                o.SuppressMapClientErrors = true;
            });

            // Assert
            var options = serviceCollection.
                          BuildServiceProvider()
                          .GetRequiredService <IOptions <ApiBehaviorOptions> >()
                          .Value;

            Assert.True(options.SuppressMapClientErrors);
        }
        private void GlobalExceptionFilterIsAdded()
        {
            var services = new ServiceCollection();
            var manager  = new ApplicationPartManager();
            var builder  = new MvcBuilder(services, manager);

            services.AddOptions();
            services.AddSingleton(typeof(ObjectPoolProvider), new DefaultObjectPoolProvider());
            services
            .AddLogging()
            .AddMvcCore()
            .AddJsonFormatters();

            builder.AddApiExtensions(null, options => { });

            var sp = services.BuildServiceProvider();

            MvcOptions mvcOptions = sp.GetService <IOptions <MvcOptions> >().Value;

            var filter = mvcOptions.Filters.OfType <TypeFilterAttribute>()
                         .Where(f => f.ImplementationType == typeof(GlobalExceptionFilter))
                         .FirstOrDefault();

            Assert.NotNull(filter);
        }
    public void AddViewComponentsAsServices_RegistersDiscoveredViewComponents()
    {
        // Arrange
        var services = new ServiceCollection();

        var manager = new ApplicationPartManager();

        manager.ApplicationParts.Add(new TestApplicationPart(
                                         typeof(ConventionsViewComponent),
                                         typeof(AttributeViewComponent)));

        manager.FeatureProviders.Add(new TestProvider());

        var builder = new MvcBuilder(services, manager);

        // Act
        builder.AddViewComponentsAsServices();

        // Assert
        var collection = services.ToList();

        Assert.Equal(3, collection.Count);

        Assert.Equal(typeof(ConventionsViewComponent), collection[0].ServiceType);
        Assert.Equal(typeof(ConventionsViewComponent), collection[0].ImplementationType);
        Assert.Equal(ServiceLifetime.Transient, collection[0].Lifetime);

        Assert.Equal(typeof(AttributeViewComponent), collection[1].ServiceType);
        Assert.Equal(typeof(AttributeViewComponent), collection[1].ImplementationType);
        Assert.Equal(ServiceLifetime.Transient, collection[1].Lifetime);

        Assert.Equal(typeof(IViewComponentActivator), collection[2].ServiceType);
        Assert.Equal(typeof(ServiceBasedViewComponentActivator), collection[2].ImplementationType);
        Assert.Equal(ServiceLifetime.Singleton, collection[2].Lifetime);
    }
Пример #7
0
        private IServiceProvider GetProvider(IDesignTimeMvcBuilderConfiguration mvcBuilderConfiguration)
        {
            var services = new ServiceCollection();

            var hostingEnvironment = new HostingEnvironment
            {
                ApplicationName         = _applicationName,
                WebRootFileProvider     = new PhysicalFileProvider(_projectPath),
                ContentRootFileProvider = new PhysicalFileProvider(_contentRoot),
                ContentRootPath         = _contentRoot,
            };
            var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");

            services
            .AddSingleton <IHostingEnvironment>(hostingEnvironment)
            .AddSingleton <DiagnosticSource>(diagnosticSource)
            .AddLogging()
            .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>();

            var mvcCoreBuilder = services
                                 .AddMvcCore()
                                 .AddRazorViewEngine();

            var mvcBuilder = new MvcBuilder(mvcCoreBuilder.Services, mvcCoreBuilder.PartManager);

            mvcBuilderConfiguration?.ConfigureMvc(mvcBuilder);

            return(mvcBuilder.Services.BuildServiceProvider());
        }
            private static IServiceProvider MvcBuilder()
            {
                var services = new ServiceCollection();
                var mvc      = new MvcBuilder(services, new ApplicationPartManager());

                mvc.AddFluentRestBuilder();
                return(services.BuildServiceProvider());
            }
    private static MvcBuilder CreateBuilder()
    {
        var services = new ServiceCollection();
        var manager  = new ApplicationPartManager();
        var builder  = new MvcBuilder(services, manager);

        return(builder);
    }
Пример #10
0
 public ApiBuilder AddControllers()
 {
     Services.Configure <MvcOptions>(opt =>
     {
         opt.Conventions.Insert(0, new ApiRouteConvention(new RouteAttribute(Options.RoutePrefix + "/[controller]")));
     });
     MvcBuilder.ConfigureApplicationPartManager(p => p.FeatureProviders.Add(new ApiControllerFeatureProvider()));
     return(this);
 }
        public void CSGenericControlDirectiveChangesInheritsDirective() {
            var filter = new ViewTypeParserFilter();
            var attributes = new Dictionary<string, string> { { "inherits", "foobar<baz>" } };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("control", attributes);
            filter.ParseComplete(builder);

            Assert.AreEqual(typeof(ViewUserControl).FullName, attributes["inherits"]);
            Assert.AreEqual("foobar<baz>", builder.Inherits);
        }
        public void CSGenericUnknownDirectiveDoesNotChangeInheritsDirective() {
            var filter = new ViewTypeParserFilter();
            var attributes = new Dictionary<string, string> { { "inherits", "foobar<baz>" } };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("unknown", attributes);
            filter.ParseComplete(builder);

            Assert.AreEqual("foobar<baz>", attributes["inherits"]);
            Assert.IsNull(builder.Inherits);
        }
Пример #13
0
        private void WebApiVersioningOptionsIsRegisteredAsSingleton()
        {
            var services = new ServiceCollection();
            var builder  = new MvcBuilder(services);

            builder.AddVersioning(options => options.Route = "myroute");

            var registrations = services.Where(sd => sd.ServiceType == typeof(IConfigureOptions <WebApiVersioningOptions>)).ToArray();

            Assert.Equal(1, registrations.Count());
            Assert.Equal(ServiceLifetime.Singleton, registrations[0].Lifetime);
        }
        public void NonGenericMasterDirectiveDoesNotChangeInheritsDirective()
        {
            var filter = new ViewTypeParserFilter();
            var attributes = new Dictionary<string, string> { { "inherits", "foobar" } };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("master", attributes);
            filter.ParseComplete(builder);

            Assert.Equal("foobar", attributes["inherits"]);
            Assert.Null(builder.Inherits);
        }
Пример #15
0
        private static void TestBuilderInternal <TResolver>(Action <IMvcBuilder> useAction) where TResolver : IJsonFormatterResolver <byte, TResolver>, new()
        {
            var options = GetOptions <MvcOptions>(services =>
            {
                var builder = new MvcBuilder(services, new ApplicationPartManager());
                useAction(builder);
            });

            Assert.NotNull(options);
            Assert.Single(options.InputFormatters);
            Assert.Single(options.OutputFormatters);
            Assert.IsType <SpanJsonInputFormatter <TResolver> >(options.InputFormatters[0]);
            Assert.IsType <SpanJsonOutputFormatter <TResolver> >(options.OutputFormatters[0]);
        }
Пример #16
0
        private void WebApiVersioningOptionsSetupIsRegisteredAsTransient()
        {
            var services = new ServiceCollection();
            var builder  = new MvcBuilder(services);

            builder.AddVersioning();

            var registrations = services.Where(sd => sd.ServiceType == typeof(IConfigureOptions <MvcOptions>) &&
                                               sd.ImplementationType == typeof(WebApiVersioningOptionsSetup))
                                .ToArray();

            Assert.Equal(1, registrations.Count());
            Assert.Equal(ServiceLifetime.Transient, registrations[0].Lifetime);
        }
Пример #17
0
        public void NonGenericMasterDirectiveDoesNotChangeInheritsDirective()
        {
            var filter     = new ViewTypeParserFilter();
            var attributes = new Dictionary <string, string> {
                { "inherits", "foobar" }
            };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("master", attributes);
            filter.ParseComplete(builder);

            Assert.AreEqual("foobar", attributes["inherits"]);
            Assert.IsNull(builder.Inherits);
        }
Пример #18
0
        private void WebApiVersionProviderIsRegisteredAsSingleton()
        {
            var services = new ServiceCollection();
            var builder  = new MvcBuilder(services);

            builder.AddVersioning();

            var registrations = services.Where(sd => sd.ServiceType == typeof(IVersionProvider) &&
                                               sd.ImplementationType == typeof(WebApiVersionProvider))
                                .ToArray();

            Assert.Equal(1, registrations.Count());
            Assert.Equal(ServiceLifetime.Singleton, registrations[0].Lifetime);
        }
        public void AddApplicationPart_AddsAnApplicationPart_ToTheListOfPartsOnTheBuilder()
        {
            // Arrange
            var manager = new ApplicationPartManager();
            var builder = new MvcBuilder(Mock.Of <IServiceCollection>(), manager);

            var assembly = typeof(MvcBuilder).GetTypeInfo().Assembly;

            // Act
            var result = builder.AddApplicationPart(assembly);

            // Assert
            Assert.Same(result, builder);
            var part         = Assert.Single(builder.PartManager.ApplicationParts);
            var assemblyPart = Assert.IsType <AssemblyPart>(part);

            Assert.Equal(assembly, assemblyPart.Assembly);
        }
        public void UseOpenIddict_MvcBuilder_RegistersModelBinderProvider()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddOptions();

            var builder = new MvcBuilder(services, new ApplicationPartManager());

            // Act
            builder.UseOpenIddict();

            var provider = services.BuildServiceProvider();
            var options  = provider.GetRequiredService <IOptions <MvcOptions> >();

            // Assert
            Assert.Contains(options.Value.ModelBinderProviders, binder => binder is OpenIddictMvcBinderProvider);
        }
        public void ConfigureApplicationParts_InvokesSetupAction()
        {
            // Arrange
            var builder = new MvcBuilder(
                Mock.Of <IServiceCollection>(),
                new ApplicationPartManager());

            var part = new TestApplicationPart();

            // Act
            var result = builder.ConfigureApplicationPartManager(manager =>
            {
                manager.ApplicationParts.Add(part);
            });

            // Assert
            Assert.Same(result, builder);
            Assert.Equal(new ApplicationPart[] { part }, builder.PartManager.ApplicationParts.ToArray());
        }
        public void AddTagHelpersAsServices_RegistersDiscoveredTagHelpers()
        {
            // Arrange
            var services = new ServiceCollection();

            var manager = new ApplicationPartManager();

            manager.ApplicationParts.Add(new TestApplicationPart(
                                             typeof(TestTagHelperOne),
                                             typeof(TestTagHelperTwo)));

            manager.FeatureProviders.Add(new TestFeatureProvider());

            var builder = new MvcBuilder(services, manager);

            // Act
            builder.AddTagHelpersAsServices();

            // Assert
            var collection = services.ToList();

            Assert.Equal(4, collection.Count);

            var tagHelperOne = Assert.Single(collection, t => t.ServiceType == typeof(TestTagHelperOne));

            Assert.Equal(typeof(TestTagHelperOne), tagHelperOne.ImplementationType);
            Assert.Equal(ServiceLifetime.Transient, tagHelperOne.Lifetime);

            var tagHelperTwo = Assert.Single(collection, t => t.ServiceType == typeof(TestTagHelperTwo));

            Assert.Equal(typeof(TestTagHelperTwo), tagHelperTwo.ImplementationType);
            Assert.Equal(ServiceLifetime.Transient, tagHelperTwo.Lifetime);

            var activator = Assert.Single(collection, t => t.ServiceType == typeof(ITagHelperActivator));

            Assert.Equal(typeof(ServiceBasedTagHelperActivator), activator.ImplementationType);
            Assert.Equal(ServiceLifetime.Transient, activator.Lifetime);

            var typeResolver = Assert.Single(collection, t => t.ServiceType == typeof(ITagHelperTypeResolver));

            Assert.Equal(typeof(FeatureTagHelperTypeResolver), typeResolver.ImplementationType);
            Assert.Equal(ServiceLifetime.Transient, typeResolver.Lifetime);
        }
Пример #23
0
        public void AddRazorOptions_ConfiguresOptionsAsExpected()
        {
            // Arrange
            var services     = new ServiceCollection().AddOptions();
            var fileProvider = new TestFileProvider();

            // Act
            var builder = new MvcBuilder(services, new ApplicationPartManager());

            builder.AddRazorOptions(options =>
            {
                options.FileProviders.Add(fileProvider);
            });
            var serviceProvider = services.BuildServiceProvider();

            // Assert
            var accessor = serviceProvider.GetRequiredService <IOptions <RazorViewEngineOptions> >();

            Assert.Same(fileProvider, accessor.Value.FileProviders[0]);
        }
        public void AddApplicationPart_UsesPartFactory_ToRetrieveApplicationParts()
        {
            // Arrange
            var manager  = new ApplicationPartManager();
            var builder  = new MvcBuilder(Mock.Of <IServiceCollection>(), manager);
            var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run);

            var attribute = new CustomAttributeBuilder(typeof(ProvideApplicationPartFactoryAttribute).GetConstructor(
                                                           new[] { typeof(Type) }),
                                                       new[] { typeof(TestApplicationPartFactory) });

            assembly.SetCustomAttribute(attribute);

            // Act
            builder.AddApplicationPart(assembly);

            // Assert
            var part = Assert.Single(builder.PartManager.ApplicationParts);

            Assert.Same(TestApplicationPartFactory.TestPart, part);
        }
        private void JsonOutputFormatterSupportsHAL()
        {
            var services = new ServiceCollection();
            var manager  = new ApplicationPartManager();
            var builder  = new MvcBuilder(services, manager);

            services.AddOptions();
            services.AddSingleton(typeof(ObjectPoolProvider), new DefaultObjectPoolProvider());
            services
            .AddLogging()
            .AddMvcCore()
            .AddJsonFormatters();

            builder.AddApiExtensions();

            var sp = services.BuildServiceProvider();

            MvcOptions mvcOptions = sp.GetService <IOptions <MvcOptions> >().Value;

            var jsonOutputFormatter = mvcOptions.OutputFormatters.OfType <JsonOutputFormatter>().First();

            Assert.True(jsonOutputFormatter.SupportedMediaTypes.Any(x => x == "application/hal+json"));
        }
        public void VBDirectivesAfterPageDirectiveProperlyPreserveInheritsDirective()
        {
            var filter = new ViewTypeParserFilter();
            var pageAttributes = new Dictionary<string, string> { { "inherits", "foobar(of baz)" } };
            var importAttributes = new Dictionary<string, string> { { "inherits", "dummyvalue(of baz)" } };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("page", pageAttributes);
            filter.PreprocessDirective("import", importAttributes);
            filter.ParseComplete(builder);

            Assert.Equal(typeof(ViewPage).FullName, pageAttributes["inherits"]);
            Assert.Equal("foobar(of baz)", builder.Inherits);
        }
        public void VBGenericMasterDirectiveChangesInheritsDirective()
        {
            var filter = new ViewTypeParserFilter();
            var attributes = new Dictionary<string, string> { { "inherits", "foobar(of baz)" } };
            var builder = new MvcBuilder();

            filter.PreprocessDirective("master", attributes);
            filter.ParseComplete(builder);

            Assert.Equal(typeof(ViewMasterPage).FullName, attributes["inherits"]);
            Assert.Equal("foobar(of baz)", builder.Inherits);
        }