コード例 #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>();

            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(typeof(IDataTable <,>), typeof(InMemoryDataTable <,>));

            serverFixtureConfiguration.MainServicePostConfigureServices?.Invoke(services);
        }
コード例 #2
0
        private void ConfigureDependencyService(
            string serviceName,
            DependencyServiceConfiguration dependencyService,
            Mock <HttpMessageHandler> mockHttpHandler,
            Mock <IHttpClientFactory> mockHttpClientFactory,
            ILogger logger)
        {
            var baseUrl = $"https://{serviceName}.com";

            mockHttpClientFactory
            .Setup(x => x.CreateClient(serviceName))
            .Returns(() =>
            {
                var client         = mockHttpHandler.CreateClient();
                client.BaseAddress = new Uri(baseUrl);
                return(client);
            });
            if (dependencyService.Setup != null)
            {
                var handler = new DependencyServiceHttpHandler(mockHttpHandler, baseUrl);
                dependencyService.Setup(handler);
            }
        }
        public static ISetup <HttpMessageHandler, Task <HttpResponseMessage> > SetupRequestWithExpectedFileContent(this DependencyServiceHttpHandler handler, HttpMethod method, string requestUrl, string fileParameterName, byte[] expectedFileBytes)
        {
            return(handler.SetupRequest(method, requestUrl, async(message) =>
            {
                message.Content.GetType().Should().Be(typeof(MultipartFormDataContent));
                var multipartContent = (MultipartFormDataContent)message.Content;
                var filePart = multipartContent.FirstOrDefault(part => part.Headers.ContentDisposition.Name == fileParameterName);
                filePart.Should().NotBeNull();
                var bytes = await filePart.ReadAsByteArrayAsync();
                bytes.Should().BeEquivalentTo(expectedFileBytes);

                return true;
            }));
        }
        public static ISetup <HttpMessageHandler, Task <HttpResponseMessage> > SetupRequestWithExpectedBody <TRequestJsonType>(this DependencyServiceHttpHandler handler, HttpMethod method, string requestUrl, TRequestJsonType expectedRequestBody)
        {
            return(handler.SetupRequest(method, requestUrl, async(message) =>
            {
                var stringContent = await message.Content.ReadAsStringAsync();
                var requestBody = JsonSerializer.Deserialize <TRequestJsonType>(stringContent);
                requestBody.Should().BeEquivalentTo(expectedRequestBody);

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