示例#1
0
        public void should_return_response_200_with_simple_url_and_hosts_match()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 51879,
                            }
                        },
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        UpstreamHost = "localhost"
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Hello from Laura"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
示例#2
0
        public void should_use_default_request_id_and_forward()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 51873,
                            }
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        RequestIdKey = _steps.RequestIdKey,
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51873"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheRequestIdIsReturned())
            .BDDfy();
        }
示例#3
0
        public void should_return_response_200_authorising_route()
        {
            int port = 52875;

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            }
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        AuthenticationOptions = new FileAuthenticationOptions
                        {
                            AuthenticationProviderKey = "Test"
                        },
                        AddHeadersToRequest =
                        {
                            { "CustomerId", "Claims[CustomerId] > value" },
                            { "LocationId", "Claims[LocationId] > value" },
                            { "UserType",   "Claims[sub] > value[0] > |" },
                            { "UserId",     "Claims[sub] > value[1] > |" }
                        },
                        AddClaimsToRequest =
                        {
                            { "CustomerId", "Claims[CustomerId] > value" },
                            { "UserType",   "Claims[sub] > value[0] > |" },
                            { "UserId",     "Claims[sub] > value[1] > |" }
                        },
                        RouteClaimsRequirement =
                        {
                            { "UserType", "registered" }
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
            .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
            .And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
            .And(x => _steps.GivenIHaveAddedATokenToMyRequest())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
示例#4
0
        public void should_return_401_using_identity_server_access_token()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = _downstreamServicePath,
                        DownstreamPort         = _downstreamServicePort,
                        DownstreamHost         = _downstreamServiceHost,
                        DownstreamScheme       = _downstreamServiceScheme,
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Post"
                        },
                        AuthenticationOptions = new FileAuthenticationOptions
                        {
                            AuthenticationProviderKey = "Test"
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
            .And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
            .And(x => _steps.GivenThePostHasContent("postContent"))
            .When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
            .BDDfy();
        }
示例#5
0
        public void should_throw_exception_if_cannot_start_because_service_discovery_provider_specified_in_config_but_no_service_discovery_provider_registered_with_dynamic_re_routes()
        {
            var invalidConfig = new FileConfiguration
            {
                GlobalConfiguration = new FileGlobalConfiguration
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
                    {
                        Scheme = "https",
                        Host   = "localhost",
                        Type   = "consul",
                        Port   = 8500
                    }
                }
            };

            Exception exception = null;

            _steps.GivenThereIsAConfiguration(invalidConfig);
            try
            {
                _steps.GivenOcelotIsRunning();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            exception.ShouldNotBeNull();
            exception.Message.ShouldBe("One or more errors occurred. (Unable to start Ocelot, errors are: Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?)");
        }
示例#6
0
        public void should_return_bad_gateway_error_if_downstream_service_doesnt_respond()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 53877,
                            },
                        },
                        DownstreamScheme = "http",
                    },
                },
            };

            this.Given(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
            .BDDfy();
        }
示例#7
0
        public void should_call_withratelimiting()
        {
            int port = RandomPortFinder.GetRandomPort();

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/ClientRateLimit",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            }
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/api/ClientRateLimit",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        RequestIdKey     = _steps.RequestIdKey,
                        RateLimitOptions = new FileRateLimitRule()
                        {
                            EnableRateLimiting = true,
                            ClientWhitelist    = new List <string>(),
                            Limit          = 3,
                            Period         = "1s",
                            PeriodTimespan = 1000
                        }
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    RateLimitOptions = new FileRateLimitOptions()
                    {
                        ClientIdHeader          = "ClientId",
                        DisableRateLimitHeaders = false,
                        QuotaExceededMessage    = "",
                        RateLimitCounterPrefix  = "",
                        HttpStatusCode          = 428
                    },
                    RequestIdKey = "oceclientrequest"
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/ClientRateLimit"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 1))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(200))
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 2))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(200))
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 1))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(428))
            .BDDfy();
        }
示例#8
0
        public void should_return_response_200_when_get_converted_to_post()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/{url}",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/{url}",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 53171,
                            },
                        },
                        DownstreamHttpMethod = "POST",
                    },
                },
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53171/", "/", "POST"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .BDDfy();
        }
示例#9
0
        public void should_return_cached_response()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamPort         = 51879,
                        DownstreamScheme       = "http",
                        DownstreamHost         = "localhost",
                        UpstreamTemplate       = "/",
                        UpstreamHttpMethod     = "Get",
                        FileCacheOptions       = new FileCacheOptions
                        {
                            TtlSeconds = 100
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
示例#10
0
        public void should_transform_upstream_header()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        DownstreamHost         = "localhost",
                        DownstreamPort         = 51879,
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        UpstreamHeaderTransform = new Dictionary <string, string>
                        {
                            { "Laz", "D, GP" }
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Laz"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .And(x => _steps.GivenIAddAHeader("Laz", "D"))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("GP"))
            .BDDfy();
        }
示例#11
0
        public void should_fix_issue_555()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List<FileReRoute>
                    {
                        new FileReRoute
                        {
                            DownstreamPathTemplate = "/{everything}",
                            DownstreamScheme = "http",
                            UpstreamPathTemplate = "/{everything}",
                            UpstreamHttpMethod = new List<string> { "Get" },
                            ServiceName = "OcelotServiceApplication/OcelotApplicationService"
                        }
                    },
                GlobalConfiguration = new FileGlobalConfiguration
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = 19081,
                        Type = "ServiceFabric"
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:19081", "/OcelotServiceApplication/OcelotApplicationService/a", 200, "Hello from Laura", "b=c"))
                .And(x => _steps.GivenThereIsAConfiguration(configuration))
                .And(x => _steps.GivenOcelotIsRunning())
                .When(x => _steps.WhenIGetUrlOnTheApiGateway("/a?b=c"))
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
                .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
                .BDDfy();
        }
示例#12
0
        public void should_return_response_200_authorising_route()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamPort         = 51876,
                        DownstreamScheme       = "http",
                        DownstreamHost         = "localhost",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        AuthenticationOptions = new FileAuthenticationOptions
                        {
                            AllowedScopes        = new List <string>(),
                            Provider             = "IdentityServer",
                            IdentityServerConfig = new FileIdentityServerConfig {
                                ProviderRootUrl = "http://localhost:51888",
                                RequireHttps    = false,
                                ApiName         = "api",
                                ApiSecret       = "secret"
                            }
                        },
                        AddHeadersToRequest =
                        {
                            { "CustomerId", "Claims[CustomerId] > value" },
                            { "LocationId", "Claims[LocationId] > value" },
                            { "UserType",   "Claims[sub] > value[0] > |" },
                            { "UserId",     "Claims[sub] > value[1] > |" }
                        },
                        AddClaimsToRequest =
                        {
                            { "CustomerId", "Claims[CustomerId] > value" },
                            { "UserType",   "Claims[sub] > value[0] > |" },
                            { "UserId",     "Claims[sub] > value[1] > |" }
                        },
                        RouteClaimsRequirement =
                        {
                            { "UserType", "registered" }
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
            .And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
            .And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .And(x => _steps.GivenIHaveAddedATokenToMyRequest())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
示例#13
0
 public void should_return_response_404_when_no_configuration_at_all()
 {
     this.Given(x => _steps.GivenThereIsAConfiguration(new FileConfiguration()))
     .And(x => _steps.GivenOcelotIsRunning())
     .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
     .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
     .BDDfy();
 }
示例#14
0
        public void should_load_balance_request_with_least_connection()
        {
            int portOne = RandomPortFinder.GetRandomPort();
            int portTwo = RandomPortFinder.GetRandomPort();

            var downstreamServiceOneUrl = $"http://localhost:{portOne}";
            var downstreamServiceTwoUrl = $"http://localhost:{portTwo}";

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        LoadBalancerOptions = new FileLoadBalancerOptions {
                            Type = nameof(LeastConnection)
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = portOne
                            },
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = portTwo
                            }
                        }
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                }
            };

            this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
            .And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimes("/", 50))
            .Then(x => x.ThenTheTwoServicesShouldHaveBeenCalledTimes(50))
            .And(x => x.ThenBothServicesCalledRealisticAmountOfTimes(24, 26))
            .BDDfy();
        }
示例#15
0
        public void should_use_same_downstream_host()
        {
            var downstreamPortOne       = 51375;
            var downstreamPortTwo       = 51892;
            var downstreamServiceOneUrl = $"http://localhost:{downstreamPortOne}";
            var downstreamServiceTwoUrl = $"http://localhost:{downstreamPortTwo}";

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        LoadBalancerOptions = new FileLoadBalancerOptions
                        {
                            Type   = "CookieStickySessions",
                            Key    = "sessionid",
                            Expiry = 300000
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = downstreamPortOne
                            },
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = downstreamPortTwo
                            }
                        }
                    }
                }
            };

            this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
            .And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimes("/", 10, "sessionid", "123"))
            .Then(x => x.ThenTheFirstServiceIsCalled(10))
            .Then(x => x.ThenTheSecondServiceIsCalled(0))
            .BDDfy();
        }
示例#16
0
        public void should_open_circuit_breaker_then_close()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 51892,
                            }
                        },
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        QoSOptions = new FileQoSOptions
                        {
                            ExceptionsAllowedBeforeBreaking = 1,
                            TimeoutValue    = 500,
                            DurationOfBreak = 1000
                        },
                    }
                }
            };

            this.Given(x => x.GivenThereIsAPossiblyBrokenServiceRunningOn("http://localhost:51892", "Hello from Laura"))
            .Given(x => _steps.GivenThereIsAConfiguration(configuration))
            .Given(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
            .Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
            .Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
            .Given(x => x.GivenIWaitMilliseconds(3000))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
示例#17
0
        public void should_use_eureka_service_discovery_and_make_request()
        {
            var eurekaPort                    = 8761;
            var serviceName                   = "product";
            var downstreamServicePort         = 50371;
            var downstreamServiceOneUrl       = $"http://localhost:{downstreamServicePort}";
            var fakeEurekaServiceDiscoveryUrl = $"http://localhost:{eurekaPort}";

            var instanceOne = new FakeEurekaService(serviceName, "localhost", downstreamServicePort, false,
                                                    new Uri($"http://localhost:{downstreamServicePort}"), new Dictionary <string, string>());

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        ServiceName         = serviceName,
                        LoadBalancerOptions = new FileLoadBalancerOptions {
                            Type = "LeastConnection"
                        },
                        UseServiceDiscovery = true,
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Type = "Eureka"
                    }
                }
            };

            this.Given(x => x.GivenEurekaProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
            .And(x => x.GivenThereIsAFakeEurekaServiceDiscoveryProvider(fakeEurekaServiceDiscoveryUrl, serviceName))
            .And(x => x.GivenTheServicesAreRegisteredWithEureka(instanceOne))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(_ => _steps.ThenTheResponseBodyShouldBe(nameof(ServiceDiscoveryTests)))
            .BDDfy();
        }
示例#18
0
        public void should_return_response_304_when_service_returns_304()
        {
            var port = RandomPortFinder.GetRandomPort();

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

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/inline.132.bundle.js", 304))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/inline.132.bundle.js"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotModified))
            .BDDfy();
        }
示例#19
0
        public void should_return_internal_server_error_if_downstream_service_returns_internal_server_error()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 53876,
                            }
                        },
                        DownstreamScheme = "http",
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
            .BDDfy();
        }
示例#20
0
        public void should_return_reason_phrase()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 51339,
                            }
                        },
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51339", "/", "some reason"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .And(_ => _steps.ThenTheReasonPhraseIs("some reason"))
            .BDDfy();
        }
        public void should_throw_exception_if_cannot_start()
        {
            var invalidConfig = new FileConfiguration()
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        UpstreamPathTemplate   = "api",
                        DownstreamPathTemplate = "test"
                    }
                }
            };

            Exception exception = null;

            _steps.GivenThereIsAConfiguration(invalidConfig);
            try
            {
                _steps.GivenOcelotIsRunning();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            exception.ShouldNotBeNull();
        }
示例#22
0
        public void should_return_response_200_and_foward_claim_as_header()
        {
            var user = new TestUser()
            {
                Username  = "******",
                Password  = "******",
                SubjectId = "registered|1231231",
                Claims    = new List <Claim>
                {
                    new Claim("CustomerId", "123"),
                    new Claim("LocationId", "1")
                }
            };

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamPort         = 52876,
                        DownstreamScheme       = "http",
                        DownstreamHost         = "localhost",
                        UpstreamTemplate       = "/",
                        UpstreamHttpMethod     = "Get",
                        AuthenticationOptions  = new FileAuthenticationOptions
                        {
                            AdditionalScopes = new List <string>
                            {
                                "openid", "offline_access"
                            },
                            Provider        = "IdentityServer",
                            ProviderRootUrl = "http://localhost:52888",
                            RequireHttps    = false,
                            ScopeName       = "api",
                            ScopeSecret     = "secret",
                        },
                        AddHeadersToRequest =
                        {
                            { "CustomerId", "Claims[CustomerId] > value" },
                            { "LocationId", "Claims[LocationId] > value" },
                            { "UserType",   "Claims[sub] > value[0] > |" },
                            { "UserId",     "Claims[sub] > value[1] > |" }
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:52888", "api", AccessTokenType.Jwt, user))
            .And(x => x.GivenThereIsAServiceRunningOn("http://localhost:52876", 200))
            .And(x => _steps.GivenIHaveAToken("http://localhost:52888"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .And(x => _steps.GivenIHaveAddedATokenToMyRequest())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
            .BDDfy();
        }
        public void should_return_200_and_change_downstream_path()
        {
            var user = new TestUser()
            {
                Username  = "******",
                Password  = "******",
                SubjectId = "registered|1231231",
            };

            int port = RandomPortFinder.GetRandomPort();

            var configuration = new FileConfiguration
            {
                Routes = new List <FileRoute>
                {
                    new FileRoute
                    {
                        DownstreamPathTemplate = "/users/{userId}",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            },
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/users",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        AuthenticationOptions = new FileAuthenticationOptions
                        {
                            AuthenticationProviderKey = "Test",
                            AllowedScopes             = new List <string>
                            {
                                "openid", "offline_access", "api",
                            },
                        },
                        ChangeDownstreamPathTemplate =
                        {
                            { "userId", "Claims[sub] > value[1] > |" },
                        },
                    },
                },
            };

            this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt, user))
            .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200))
            .And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
            .And(x => _steps.GivenIHaveAddedATokenToMyRequest())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/users"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("UserId: 1231231"))
            .And(x => _downstreamFinalPath.ShouldBe("/users/1231231"))
            .BDDfy();
        }
示例#24
0
        public void should_return_cached_response()
        {
            int port = RandomPortFinder.GetRandomPort();

            var configuration = new FileConfiguration
            {
                Routes = new List <FileRoute>
                {
                    new FileRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            }
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                        FileCacheOptions = new FileCacheOptions
                        {
                            TtlSeconds = 100
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura", null, null))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .Given(x => x.GivenTheServiceNowReturns($"http://localhost:{port}", 200, "Hello from Tom"))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .And(x => _steps.ThenTheContentLengthIs(16))
            .BDDfy();
        }
示例#25
0
        public void should_return_response_200_when_global_ignore_case_sensitivity_set()
        {
            int port = RandomPortFinder.GetRandomPort();

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

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .BDDfy();
        }
示例#26
0
        public void should_call_pre_query_string_builder_middleware()
        {
            var configuration = new OcelotMiddlewareConfiguration
            {
                AuthorisationMiddleware = async(ctx, next) =>
                {
                    _counter++;
                    await next.Invoke();
                }
            };

            var fileConfiguration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamPort         = 41879,
                        DownstreamScheme       = "http",
                        DownstreamHost         = "localhost",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = "Get",
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
            .And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
            .And(x => _steps.GivenOcelotIsRunning(configuration))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => x.ThenTheCounterIs(1))
            .BDDfy();
        }
示例#27
0
        public void should_not_add_content_type_or_content_length_headers()
        {
            var port = RandomPortFinder.GetRandomPort();

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

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .And(x => ThenTheContentTypeShouldBeEmpty())
            .And(x => ThenTheContentLengthShouldBeZero())
            .BDDfy();
        }
示例#28
0
        public void should_return_response_200_when_using_http_one()
        {
            var port = RandomPortFinder.GetRandomPort();

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/{url}",
                        DownstreamScheme       = "https",
                        UpstreamPathTemplate   = "/{url}",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = port,
                            },
                        },
                        DownstreamHttpMethod  = "POST",
                        DownstreamHttpVersion = "1.0",
                        DangerousAcceptAnyServerCertificateValidator = true
                    },
                },
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .BDDfy();
        }
示例#29
0
        public void should_not_timeout()
        {
            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 51569,
                            }
                        },
                        DownstreamScheme     = "http",
                        UpstreamPathTemplate = "/",
                        UpstreamHttpMethod   = new List <string> {
                            "Post"
                        },
                        QoSOptions = new FileQoSOptions
                        {
                            TimeoutValue = 1000,
                        }
                    }
                }
            };

            this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51569", 200, string.Empty, 10))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .And(x => _steps.GivenThePostHasContent("postContent"))
            .When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .BDDfy();
        }
示例#30
0
        public void should_dangerous_accept_any_server_certificate_validator()
        {
            int port = 51129;

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

            this.Given(x => x.GivenThereIsAServiceRunningOn($"https://localhost:{port}", "/", 200, "Hello from Laura", port))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }