Example #1
0
        public void should_handle_request_to_consul_for_downstream_service_and_make_request()
        {
            const int    consulPort                    = 8505;
            const string serviceName                   = "web";
            const string downstreamServiceOneUrl       = "http://localhost:8080";
            var          fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
            var          serviceEntryOne               = new ServiceEntry()
            {
                Service = new AgentService()
                {
                    Service = serviceName,
                    Address = "localhost",
                    Port    = 8080,
                    ID      = "web_90_0_2_224_8080",
                    Tags    = new[] { "version-v1" }
                },
            };

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/home",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/home",
                        UpstreamHttpMethod     = new List <string> {
                            "Get", "Options"
                        },
                        ServiceName         = serviceName,
                        LoadBalancerOptions = new FileLoadBalancerOptions {
                            Type = "LeastConnection"
                        },
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = consulPort
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn(downstreamServiceOneUrl, "/api/home", 200, "Hello from Laura"))
            .And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
            .And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/home"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
        public void should_fix_issue_194()
        {
            var consulPort = 8503;
            var downstreamServiceOneUrl       = "http://localhost:8362";
            var downstreamServiceTwoUrl       = "http://localhost:8330";
            var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/user/{user}",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 8362,
                            }
                        },
                        UpstreamPathTemplate = "/api/user/{user}",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    },
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/product/{product}",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 8330,
                            }
                        },
                        UpstreamPathTemplate = "/api/product/{product}",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = consulPort
                    }
                }
            };

            this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, "/api/user/info", 200, "user"))
            .And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, "/api/product/info", 200, "product"))
            .And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/user/info?id=1"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("user"))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/product/info?id=1"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("product"))
            .BDDfy();
        }