Ejemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.InvokeMethod(
                _startup,
                "ConfigureServices",
                new Dictionary <Type, object>()
            {
                [typeof(IServiceCollection)] = services
            });

            var assembly = typeof(TOriginalStartup).Assembly;

            services.AddApplicationPart(assembly);

            var serverFixtureConfiguration = serviceProvider.GetRequiredService <ServerFixtureConfiguration>();

            if (serverFixtureConfiguration.EnableTestAuthentication)
            {
                services.AddTestAuthentication();
            }

            var httpHandler = new Mock <HttpMessageHandler>();

            services.AddSingleton(httpHandler);
            var httpClientFactory = httpHandler.CreateClientFactory();

            services.AddSingleton(typeof(IHttpClientFactory), httpClientFactory);
            var mockHttpClientFactory = Mock.Get(httpClientFactory);

            foreach (var dependencyService in serverFixtureConfiguration.DependencyServices)
            {
                var baseUrl = $"https://{dependencyService.Name}.com";
                mockHttpClientFactory
                .Setup(x => x.CreateClient(dependencyService.Name))
                .Returns(() =>
                {
                    var client         = httpHandler.CreateClient();
                    client.BaseAddress = new Uri(baseUrl);
                    return(client);
                });
                if (dependencyService.Setup != null)
                {
                    var handler = new DependencyServiceHttpHandler(httpHandler, baseUrl);
                    dependencyService.Setup(handler);
                }
            }

            services.ReplaceSingleton <IDateTimeService, MockDateTimeService>();

            serverFixtureConfiguration.MainServicePostConfigureServices?.Invoke(services);
        }
 public static ISetup <HttpMessageHandler, Task <HttpResponseMessage> > SetupRequest(this DependencyServiceHttpHandler handler, HttpMethod method, string requestUrl, Func <HttpRequestMessage, Task <bool> > match)
 {
     return(handler.HttpHandler.SetupRequest(method, $"{handler.BaseUrl}/{requestUrl}", match));
 }