예제 #1
0
파일: Steps.cs 프로젝트: zhikecore/Ocelot
        public void GivenOcelotIsRunningWithGlobalHandlersRegisteredInDi <TOne>(FakeDependency dependency)
            where TOne : DelegatingHandler
        {
            _webHostBuilder = new WebHostBuilder();

            _webHostBuilder
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
                var env = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
                config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: false);
                config.AddEnvironmentVariables();
            })
            .ConfigureServices(s =>
            {
                s.AddSingleton(_webHostBuilder);
                s.AddSingleton <FakeDependency>(dependency);
                s.AddOcelot()
                .AddDelegatingHandler <TOne>(true);
            })
            .Configure(a =>
            {
                a.UseOcelot().Wait();
            });

            _ocelotServer = new TestServer(_webHostBuilder);

            _ocelotClient = _ocelotServer.CreateClient();
        }
예제 #2
0
            public void Should_Inject_Dependency_Using_A_Given_Registrar()
            {
                // Given
                var dependency = new FakeDependency();
                var registrar  = new FakeTypeRegistrar {
                    TypeResolverFactory = FakeTypeResolver.Factory
                };

                registrar.RegisterInstance(typeof(FakeDependency), dependency);
                var app = new CommandAppTester(registrar);

                app.SetDefaultCommand <GenericCommand <InjectSettings> >();
                app.Configure(config => config.PropagateExceptions());

                // When
                var result = app.Run(new[]
                {
                    "--name", "foo",
                    "--age", "35",
                });

                // Then
                result.ExitCode.ShouldBe(0);
                result.Settings.ShouldBeOfType <InjectSettings>().And(injected =>
                {
                    injected.ShouldNotBeNull();
                    injected.Fake.ShouldBeSameAs(dependency);
                    injected.Name.ShouldBe("Hello foo");
                    injected.Age.ShouldBe(35);
                });
            }
            public void Should_Inject_Parameters()
            {
                // Given
                var app        = new CommandAppFixture();
                var dependency = new FakeDependency();

                app.WithDefaultCommand <GenericCommand <InjectSettings> >();
                app.Configure(config =>
                {
                    config.Settings.Registrar.RegisterInstance(dependency);
                    config.PropagateExceptions();
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "--name", "foo",
                    "--age", "35",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <InjectSettings>().And(injected =>
                {
                    injected.ShouldNotBeNull();
                    injected.Fake.ShouldBeSameAs(dependency);
                    injected.Name.ShouldBe("Hello foo");
                    injected.Age.ShouldBe(35);
                });
            }
예제 #4
0
        public object GetDependency(Type type)
        {
            if (_typeCache.Contains(type))
            {
                return(_typeCache[type]);
            }

            if (type == typeof(IDependency))
            {
                var result = new FakeDependency();
                _typeCache.Add(
                    type,
                    result);
                return(result);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDependency <>))
            {
                var dependencyType     = type.GetGenericArguments()[0];
                var fakeDependencyType = typeof(FakeDependency <>).MakeGenericType(dependencyType);

                var result = Activator.CreateInstance(fakeDependencyType);

                _typeCache.Add(
                    type,
                    result);

                return(result);
            }

            return(null);
        }
예제 #5
0
        public void should_call_global_di_handlers_with_dependency()
        {
            var port = RandomPortFinder.GetRandomPort();

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            }
                        },
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    }
                }
            };

            var dependency = new FakeDependency();

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunningWithGlobalHandlersRegisteredInDi <FakeHandlerWithDependency>(dependency))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .And(x => ThenTheDependencyIsCalled(dependency))
            .BDDfy();
        }
 public InjectSettings(FakeDependency fake, string name)
 {
     Fake = fake;
     Name = "Hello " + name;
 }
 public FakeHandlerWithDependency(FakeDependency dependency)
 {
     _dependency = dependency;
 }
 private void ThenTheDependencyIsCalled(FakeDependency dependency)
 {
     dependency.Called.ShouldBeTrue();
 }