Exemple #1
0
        public void EnvEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var opts = new EnvOptions();
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();
            var host   = new GenericHostingEnvironment()
            {
                EnvironmentName = "EnvironmentName"
            };
            var ep     = new EnvEndpoint(opts, config, host);
            var middle = new EndpointOwinMiddleware <EnvironmentDescriptor>(null, ep);

            Assert.True(middle.RequestVerbAndPathMatch("GET", "/env"));
            Assert.False(middle.RequestVerbAndPathMatch("PUT", "/env"));
            Assert.False(middle.RequestVerbAndPathMatch("GET", "/badpath"));
        }
        /// <summary>
        /// Add Environment actuator endpoint to OWIN Pipeline
        /// </summary>
        /// <param name="builder">OWIN <see cref="IAppBuilder" /></param>
        /// <param name="config"><see cref="IConfiguration"/> of application for configuring env endpoint and inclusion in response</param>
        /// <param name="loggerFactory">For logging within the middleware</param>
        /// <returns>OWIN <see cref="IAppBuilder" /> with Env Endpoint added</returns>
        public static IAppBuilder UseEnvActuator(this IAppBuilder builder, IConfiguration config, ILoggerFactory loggerFactory = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var hostingEnvironment = new GenericHostingEnvironment()
            {
                ApplicationName = config[HostDefaults.ApplicationKey] ?? AppDomain.CurrentDomain.FriendlyName,
                EnvironmentName = config[HostDefaults.EnvironmentKey] ?? EnvironmentName.Production,
                ContentRootPath = AppContext.BaseDirectory
            };

            hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath);
            return(builder.UseEnvActuator(config, hostingEnvironment, loggerFactory));
        }