/// <summary>
        ///
        /// </summary>
        public void TestSetup <TMessageHandler>(string configSectionName, HttpHandlerMode httpHandlerMode = HttpHandlerMode.Replace)
            where TMessageHandler : DelegatingHandler
        {
            base.TestSetup();
            var config = BUnitTestContext.Services.AddConfigurationBase <TConfiguration>(TestHost.Services.GetService <IConfiguration>(), configSectionName);

            BUnitTestContext.Services.AddAppStateBase <TAppState>();
            BUnitTestContext.Services.AddHttpClients <TMessageHandler>(config, httpHandlerMode);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessageHandler"></typeparam>
        /// <param name="configSectionName"></param>
        /// <param name="httpHandlerMode"></param>
        public void AssemblySetup <TMessageHandler>(string configSectionName, HttpHandlerMode httpHandlerMode)
            where TMessageHandler : DelegatingHandler
        {
            TestHostBuilder.AddBlazorEssentials <TConfiguration, TAppState, TMessageHandler>(configSectionName, httpHandlerMode);

            TestHostBuilder.ConfigureServices((builder, services) => {
                services.AddSingleton <NavigationManager, TestableNavigationManager>();
            });

            base.AssemblySetup();
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessageHandler"></typeparam>
        /// <param name="configSectionName"></param>
        /// <param name="httpHandlerMode"></param>
        public void AssemblySetup <TMessageHandler>(string configSectionName, HttpHandlerMode httpHandlerMode)
            where TMessageHandler : DelegatingHandler
        {
            TestHostBuilder.ConfigureServices((builder, services) => {
                var config = services.AddConfigurationBase <TConfiguration>(builder.Configuration, configSectionName);
                services.AddAppStateBase <TAppState>();
                services.AddHttpClients <TMessageHandler>(config, httpHandlerMode);
                services.AddSingleton <NavigationManager, TestableNavigationManager>();
            });

            base.AssemblySetup();
        }
        /// <summary>
        /// Adds Blazor capabilities to the provided <see cref="IHostBuilder"/>.
        /// </summary>
        /// <typeparam name="TConfiguration"></typeparam>
        /// <typeparam name="TAppState"></typeparam>
        /// <typeparam name="TMessageHandler"></typeparam>
        /// <param name="builder"></param>
        /// <param name="configSectionName"></param>
        /// <param name="httpHandlerMode"></param>
        /// <returns></returns>
        public static IHostBuilder AddBlazorEssentials <TConfiguration, TAppState, TMessageHandler>(this IHostBuilder builder, string configSectionName, HttpHandlerMode httpHandlerMode)
            where TConfiguration : ConfigurationBase
            where TAppState : AppStateBase
            where TMessageHandler : DelegatingHandler
        {
            builder.ConfigureServices((builder, services) => {
                var config = services.AddConfigurationBase <TConfiguration>(builder.Configuration, configSectionName);
                services.AddAppStateBase <TAppState>();
                services.AddHttpClients <TConfiguration, TMessageHandler>(config, httpHandlerMode);
            });

            return(builder);
        }
Esempio n. 5
0
        /// <summary>
        /// Given the <see cref="HttpHandlerMode"/>, adds the specified <typeparamref name="THandler"/> to the beginning or end of the pipeline.
        /// </summary>
        /// <typeparam name="THandler">The <see cref="DelegatingHandler"/> type to pull from the scoped <see cref="ServiceProvider"/>.</typeparam>
        /// <param name="builder">The <see cref="IHttpClientBuilder"/> instance to extend.</param>
        /// <param name="mode">A <see cref="HttpHandlerMode"/> specifying whether we are making this handler the first one in the pipeline, or the last.</param>
        /// <returns></returns>
        public static IHttpClientBuilder AddHttpMessageHandler <THandler>(this IHttpClientBuilder builder, HttpHandlerMode mode)
            where THandler : DelegatingHandler

        {
            if (mode == HttpHandlerMode.None)
            {
                return(builder);
            }
            return(mode == HttpHandlerMode.Add ? builder.AddHttpMessageHandler <THandler>() : builder.ConfigurePrimaryHttpMessageHandler <THandler>());
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessageHandler"></typeparam>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <param name="httpHandlerMode"></param>
        /// <returns></returns>
        internal static IServiceCollection AddHttpClients <TMessageHandler>(this IServiceCollection services, ConfigurationBase config, HttpHandlerMode httpHandlerMode)
            where TMessageHandler : DelegatingHandler
        {
            services.TryAddScoped <TMessageHandler>();

            if (!string.IsNullOrWhiteSpace(config.AppRoot))
            {
                services.AddHttpClient(config.AppClientName, client => client.BaseAddress = new Uri(config.AppRoot))
                .AddHttpMessageHandler <TMessageHandler>(httpHandlerMode);
            }

            if (!string.IsNullOrWhiteSpace(config.ApiRoot))
            {
                services.AddHttpClient(config.ApiClientName, client => client.BaseAddress = new Uri(config.ApiRoot))
                .AddHttpMessageHandler <TMessageHandler>(httpHandlerMode);
            }

            return(services);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TConfig"></typeparam>
        /// <typeparam name="TMessageHandler"></typeparam>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <param name="httpHandlerMode"></param>
        /// <returns></returns>
        internal static IServiceCollection AddHttpClients <TConfig, TMessageHandler>(this IServiceCollection services, TConfig config, HttpHandlerMode httpHandlerMode)
            where TMessageHandler : DelegatingHandler
            where TConfig : ConfigurationBase
        {
            if (httpHandlerMode != HttpHandlerMode.None)
            {
                services.TryAddScoped <TMessageHandler>();
            }

            var properties = typeof(TConfig).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Where(c => c.GetCustomAttributes <AuthenticatedEndpointAttribute>().Any());

            foreach (var property in properties)
            {
                var url = property.GetValue(config) as string;
                if (string.IsNullOrWhiteSpace(url))
                {
                    continue;
                }

                var attribute = property.GetCustomAttribute <AuthenticatedEndpointAttribute>();

                services.AddHttpClient(typeof(TConfig).GetProperty(attribute.ClientNameProperty).GetValue(config) as string, client => client.BaseAddress = new Uri(url))
                .AddHttpMessageHandler <TMessageHandler>(httpHandlerMode);
            }

            return(services);
        }