Exemplo n.º 1
0
        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"));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Starts the stub and waits for it to be listening on specified ports.
 /// </summary>
 /// <param name="standardOutputTextWriter">A text writer that the stub standard output will be redirected to.</param>
 /// <param name="httpPort">The http port to start the stub on</param>
 /// <param name="httpsPort">The https port to start the stub on</param>
 /// <param name="timeoutSeconds">Timeout on waiting for the stub to start</param>
 /// <exception cref="Exception"></exception>
 public void Start(TextWriter standardOutputTextWriter, int httpPort = DefaultPortConfiguration.HttpPort, int httpsPort = DefaultPortConfiguration.HttpsPort, int timeoutSeconds = 5)
 {
     LocalStubAddress = $"Https://localhost:{httpsPort}";
     stubTask         = Task.Run(() => StubStartup.StartStub(standardOutputTextWriter, httpPort, httpsPort));
     try
     {
         using (var stubClient = new StubClient(LocalStubAddress))
         {
             // ReSharper disable once AccessToDisposedClosure - this closure is only used in the timing helper and will not be used outside the using.
             TimingHelper.WaitFor(() => stubClient.HealthCheck().Result, timeoutSeconds);
         }
     }
     catch
     {
         throw new Exception("Stub did not start correctly.");
     }
 }
Exemplo n.º 4
0
        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"));
        }
Exemplo n.º 5
0
        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")));
        }