// 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.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Threading; using System.Xml; using System.Xml.Linq; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Microsoft.Extensions.ObjectPool; using Microsoft.Extensions.Options; using Moq; using Newtonsoft.Json.Linq; using Xunit; namespace Microsoft.AspNetCore.Mvc { public class MvcOptionsSetupTest { [Fact] public void Setup_SetsUpViewEngines() { // Arrange & Act var options = GetOptions(AddViewEngineOptionsServices); // Assert var viewEngine = Assert.Single(options.ViewEngines); Assert.IsType(viewEngine); } [Fact] public void Setup_SetsUpModelBinderProviders() { // Arrange & Act var options = GetOptions(); // Assert Assert.Collection( options.ModelBinderProviders, binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder), binder => Assert.IsType(binder)); } [Fact] public void Setup_SetsUpValueProviders() { // Arrange & Act var options = GetOptions(); // Assert var valueProviders = options.ValueProviderFactories; Assert.Collection(valueProviders, provider => Assert.IsType(provider), provider => Assert.IsType(provider), provider => Assert.IsType(provider), provider => Assert.IsType(provider)); } [Fact] public void Setup_SetsUpOutputFormatters() { // Arrange & Act var options = GetOptions(); // Assert Assert.Collection(options.OutputFormatters, formatter => Assert.IsType(formatter), formatter => Assert.IsType(formatter), formatter => Assert.IsType(formatter), formatter => Assert.IsType(formatter)); } [Fact] public void Setup_SetsUpInputFormatters() { // Arrange & Act var options = GetOptions(); // Assert Assert.Collection(options.InputFormatters, formatter => Assert.IsType(formatter), formatter => Assert.IsType(formatter)); } [Fact] public void Setup_SetsUpModelValidatorProviders() { // Arrange & Act var options = GetOptions(); // Assert Assert.Collection(options.ModelValidatorProviders, validator => Assert.IsType(validator), validator => Assert.IsType(validator)); } [Fact] public void Setup_SetsUpClientModelValidatorProviders() { // Arrange & Act var options = GetOptions(AddViewEngineOptionsServices); // Assert Assert.Collection(options.ClientModelValidatorProviders, validator => Assert.IsType(validator), validator => Assert.IsType(validator), validator => Assert.IsType(validator)); } [Fact] public void Setup_IgnoresAcceptHeaderHavingWildCardMediaAndSubMediaTypes() { // Arrange & Act var options = GetOptions(); // Assert Assert.False(options.RespectBrowserAcceptHeader); } [Fact] public void Setup_SetsUpMetadataDetailsProviders() { // Arrange & Act var options = GetOptions(services => { var builder = new MvcCoreBuilder(services, new ApplicationPartManager()); builder.AddXmlDataContractSerializerFormatters(); }); // Assert var providers = options.ModelMetadataDetailsProviders; Assert.Collection(providers, provider => Assert.IsType(provider), provider => Assert.IsType(provider), provider => Assert.IsType(provider), provider => { var specialParameter = Assert.IsType(provider); Assert.Equal(typeof(CancellationToken), specialParameter.Type); Assert.Equal(BindingSource.Special, specialParameter.BindingSource); }, provider => { var formFileParameter = Assert.IsType(provider); Assert.Equal(typeof(IFormFile), formFileParameter.Type); Assert.Equal(BindingSource.FormFile, formFileParameter.BindingSource); }, provider => { var formCollectionParameter = Assert.IsType(provider); Assert.Equal(typeof(IFormCollection), formCollectionParameter.Type); Assert.Equal(BindingSource.FormFile, formCollectionParameter.BindingSource); }, provider => { var formFileParameter = Assert.IsType(provider); Assert.Equal(typeof(IFormFileCollection), formFileParameter.Type); Assert.Equal(BindingSource.FormFile, formFileParameter.BindingSource); }, provider => { var formFileParameter = Assert.IsType(provider); Assert.Equal(typeof(IEnumerable), formFileParameter.Type); Assert.Equal(BindingSource.FormFile, formFileParameter.BindingSource); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(Type), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(Uri), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(CancellationToken), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(IFormFile), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(IFormCollection), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(IFormFileCollection), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(Stream), excludeFilter.Type); }, provider => Assert.IsType(provider), provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(IJsonPatchDocument), excludeFilter.Type); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(JToken), excludeFilter.Type); }, provider => Assert.IsType(provider), provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(XObject).FullName, excludeFilter.FullTypeName); }, provider => { var excludeFilter = Assert.IsType(provider); Assert.Equal(typeof(XmlNode).FullName, excludeFilter.FullTypeName); }); } private static T GetOptions(Action action = null) where T : class, new() { var serviceProvider = GetServiceProvider(action); return serviceProvider.GetRequiredService>().Value; } private static IServiceProvider GetServiceProvider(Action action = null) { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(new ApplicationPartManager()); serviceCollection.AddSingleton(new DiagnosticListener("Microsoft.AspNetCore.Mvc")); serviceCollection.AddMvc(); serviceCollection .AddSingleton() .AddTransient(); if (action != null) { action(serviceCollection); } var serviceProvider = serviceCollection.BuildServiceProvider(); return serviceProvider; } private static void AddViewEngineOptionsServices(IServiceCollection serviceCollection) { var hostingEnvironment = new Mock(); hostingEnvironment.SetupGet(e => e.ApplicationName) .Returns(typeof(MvcOptionsSetupTest).GetTypeInfo().Assembly.GetName().Name); hostingEnvironment.SetupGet(e => e.ContentRootFileProvider) .Returns(Mock.Of()); serviceCollection.AddSingleton(hostingEnvironment.Object); } } }