public void ConfigureMustConfigureCorsMiddlewareWithAllowAnyPolicyWhenEnvironmentIsProduction()
        {
            IConfiguration      configuration = new ConfigurationFactory().Create();
            IServiceCollection  services      = new ServiceCollection();
            IWebHostEnvironment environment   = new FakeHostingEnvironment {
                EnvironmentName = "Production"
            };
            StubStartup startup = new StubStartup(configuration, environment);

            startup.ConfigureServices(services);
            DiagnosticListener listener = new DiagnosticListener("Test");

            services.AddSingleton(listener);
            services.AddSingleton <DiagnosticSource>(listener);
            services.AddSingleton(environment);
            IApplicationBuilder app = new ApplicationBuilder(services.BuildServiceProvider());

            startup.Configure(app, environment);
            List <Func <RequestDelegate, RequestDelegate> > delegates = ReflectionHelper.GetFieldOrDefault(app, "_components") as List <Func <RequestDelegate, RequestDelegate> >;

            Assert.NotNull(delegates);
            Func <RequestDelegate, RequestDelegate> cors = delegates.FirstOrDefault(x => (ReflectionHelper.GetFieldOrDefault(x.Target, "middleware") as Type)?.Name == "CorsMiddleware");

            Assert.NotNull(cors);
            object[] args = ReflectionHelper.GetFieldOrDefault(cors.Target, "args") as object[];
            Assert.NotNull(args);
            Assert.Contains(args, x => Equals(x, "AllowAnyPolicy"));
        }
        public void ConfigureMustConfigureSwaggerUIMiddlewareWhenCalled()
        {
            IConfiguration      configuration = new ConfigurationFactory().Create();
            IServiceCollection  services      = new ServiceCollection();
            IWebHostEnvironment environment   = new FakeHostingEnvironment();
            StubStartup         startup       = new StubStartup(configuration, environment);

            startup.ConfigureServices(services);
            DiagnosticListener listener = new DiagnosticListener("Test");

            services.AddSingleton(listener);
            services.AddSingleton <DiagnosticSource>(listener);
            services.AddSingleton(environment);
            IApplicationBuilder app = new ApplicationBuilder(services.BuildServiceProvider());

            startup.Configure(app, environment);
            List <Func <RequestDelegate, RequestDelegate> > delegates = ReflectionHelper.GetFieldOrDefault(app, "_components") as List <Func <RequestDelegate, RequestDelegate> >;

            Assert.NotNull(delegates);
            Func <RequestDelegate, RequestDelegate> requestDelegate = delegates.FirstOrDefault(x => ReflectionHelper.GetFieldOrDefault(x.Target, "middleware") as Type == typeof(SwaggerUIMiddleware));

            Assert.NotNull(requestDelegate);
            object[] args = ReflectionHelper.GetFieldOrDefault(requestDelegate.Target, "args") as object[];
            Assert.NotNull(args);
            SwaggerUIOptions options = args.FirstOrDefault(x => x is SwaggerUIOptions) as SwaggerUIOptions;

            Assert.NotNull(options);
            UrlDescriptor descriptor = options.ConfigObject.Urls.FirstOrDefault(y => y.Url == "/swagger/v1/swagger.json");

            Assert.NotNull(descriptor);
            Assert.Equal("testhost API V1", descriptor.Name);
        }
        public CompositionTests()
        {
            // Load real configuration
            _configuration = new ConfigurationBuilder()
                             .SetBasePath(Path.GetDirectoryName(typeof(Program).Assembly.Location))
                             .AddJsonFile("appsettings.json", false, true)
                             .AddJsonFile("appsettings.Development.json", false, true)
                             .Build();

            // Build services
            var serviceCollection    = new ServiceCollection();
            var bootstrapperSettings = new BootstrapperSettings {
                EntryAssembly     = typeof(Program).Assembly,
                EnvironmentName   = "",
                UseDetailedErrors = true
            };
            var hostingEnvironment = new FakeHostingEnvironment();

            Composition.ConfigureServices(serviceCollection, hostingEnvironment, _configuration, bootstrapperSettings);

            // Add registrations that are performed by the WebHostStartup
            serviceCollection
            .AddSingleton <IHostingEnvironment>(hostingEnvironment)
            .AddSingleton(bootstrapperSettings)
            .AddMvc()
            .AddApplicationPart(typeof(DefaultController).Assembly)
            .AddControllersAsServices()
            .AddApplicationPart(typeof(Controllers.DefaultController).Assembly)
            .AddControllersAsServices();

            // Build service provider
            _serviceProvider = serviceCollection.BuildServiceProvider();
        }
        public void ConfigureMustNotConfigureHstsWhenEnvironmentIsNotProduction()
        {
            IConfiguration      configuration = new ConfigurationFactory().Create();
            IServiceCollection  services      = new ServiceCollection();
            IWebHostEnvironment environment   = new FakeHostingEnvironment();
            StubStartup         startup       = new StubStartup(configuration, environment);

            startup.ConfigureServices(services);
            DiagnosticListener listener = new DiagnosticListener("Test");

            services.AddSingleton(listener);
            services.AddSingleton <DiagnosticSource>(listener);
            services.AddSingleton(environment);
            IApplicationBuilder app = new ApplicationBuilder(services.BuildServiceProvider());

            startup.Configure(app, environment);
            List <Func <RequestDelegate, RequestDelegate> > delegates = ReflectionHelper.GetFieldOrDefault(app, "_components") as List <Func <RequestDelegate, RequestDelegate> >;

            Assert.NotNull(delegates);
            Assert.Null(delegates.FirstOrDefault(x => (ReflectionHelper.GetFieldOrDefault(x.Target, "middleware") as Type)?.Name == "HstsMiddleware"));
        }
        public void ConfigureMustConfigureHealthCheckRouteWhenCalled()
        {
            IConfiguration      configuration = new ConfigurationFactory().Create();
            IServiceCollection  services      = new ServiceCollection();
            IWebHostEnvironment environment   = new FakeHostingEnvironment();
            StubStartup         startup       = new StubStartup(configuration, environment);

            startup.ConfigureServices(services);
            DiagnosticListener listener = new DiagnosticListener("Test");

            services.AddSingleton(listener);
            services.AddSingleton <DiagnosticSource>(listener);
            services.AddSingleton(environment);
            IApplicationBuilder app = new ApplicationBuilder(services.BuildServiceProvider());

            startup.Configure(app, environment);
            object property = app.Properties["__EndpointRouteBuilder"];

            Assert.NotNull(property);
            List <EndpointDataSource> dataSources = ReflectionHelper.GetProperty <List <EndpointDataSource> >(property, "DataSources");

            Assert.NotNull(dataSources.FirstOrDefault(x => x.Endpoints.Any(y => (y as RouteEndpoint)?.RoutePattern.RawText == "/health")));
        }