public void TestFileRouting(string incomingPath, string expectedRoute)
        {
            IRoute fileRoute = new FileRoute("/a/test.json", "/b/test.json");

            Assert.True(fileRoute.RequestMatchesPath(incomingPath));
            Assert.Equal(expectedRoute, fileRoute.TranslateRequestToRoutedPath(incomingPath));
        }
        public void should_create_security_config()
        {
            var ipAllowedList = new List <string>()
            {
                "127.0.0.1", "192.168.1.1"
            };
            var ipBlockedList = new List <string>()
            {
                "127.0.0.1", "192.168.1.1"
            };
            var fileRoute = new FileRoute
            {
                SecurityOptions = new FileSecurityOptions()
                {
                    IPAllowedList = ipAllowedList,
                    IPBlockedList = ipBlockedList
                }
            };

            var expected = new SecurityOptions(ipAllowedList, ipBlockedList);

            this.Given(x => x.GivenThe(fileRoute))
            .When(x => x.WhenICreate())
            .Then(x => x.ThenTheResultIs(expected))
            .BDDfy();
        }
        public void should_log_errors_and_not_add_headers()
        {
            var route = new FileRoute
            {
                DownstreamHeaderTransform = new Dictionary <string, string>
                {
                    { "Location", "http://www.bbc.co.uk/, {BaseUrl}" },
                },
                UpstreamHeaderTransform = new Dictionary <string, string>
                {
                    { "Location", "http://www.bbc.co.uk/, {BaseUrl}" },
                }
            };

            var expected = new List <HeaderFindAndReplace>
            {
            };

            this.Given(x => GivenTheRoute(route))
            .And(x => GivenTheBaseUrlErrors())
            .When(x => WhenICreate())
            .Then(x => ThenTheFollowingDownstreamIsReturned(expected))
            .And(x => ThenTheFollowingUpstreamIsReturned(expected))
            .And(x => ThenTheLoggerIsCalledCorrectly("Unable to add DownstreamHeaderTransform Location: http://www.bbc.co.uk/, {BaseUrl}"))
            .And(x => ThenTheLoggerIsCalledCorrectly("Unable to add UpstreamHeaderTransform Location: http://www.bbc.co.uk/, {BaseUrl}"))
            .BDDfy();
        }
        public void should_return_re_route_key()
        {
            var route = new FileRoute
            {
                UpstreamPathTemplate = "/api/product",
                UpstreamHttpMethod   = new List <string> {
                    "GET", "POST", "PUT"
                },
                DownstreamHostAndPorts = new List <FileHostAndPort>
                {
                    new FileHostAndPort
                    {
                        Host = "localhost",
                        Port = 123
                    },
                    new FileHostAndPort
                    {
                        Host = "localhost",
                        Port = 123
                    }
                }
            };

            this.Given(_ => GivenThe(route))
            .When(_ => WhenICreate())
            .Then(_ => ThenTheResultIs($"{route.UpstreamPathTemplate}|{string.Join(",", route.UpstreamHttpMethod)}|{string.Join(",", route.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}"))
            .BDDfy();
        }
Exemple #5
0
        public void should_create_downstream_addresses_from_downstream_host_and_ports()
        {
            var route = new FileRoute
            {
                DownstreamHostAndPorts = new List <FileHostAndPort>
                {
                    new FileHostAndPort
                    {
                        Host = "test",
                        Port = 80
                    },
                    new FileHostAndPort
                    {
                        Host = "west",
                        Port = 443
                    }
                }
            };

            var expected = new List <DownstreamHostAndPort>
            {
                new DownstreamHostAndPort("test", 80),
                new DownstreamHostAndPort("west", 443)
            };

            this.Given(x => GivenTheFollowingRoute(route))
            .When(x => WhenICreate())
            .Then(x => TheThenFollowingIsReturned(expected))
            .BDDfy();
        }
        private static bool IsNotDuplicateIn(FileRoute route,
                                             List <FileRoute> routes)
        {
            var matchingRoutes = routes
                                 .Where(r => r.UpstreamPathTemplate == route.UpstreamPathTemplate &&
                                        r.UpstreamHost == route.UpstreamHost)
                                 .ToList();

            if (matchingRoutes.Count == 1)
            {
                return(true);
            }

            var allowAllVerbs = matchingRoutes.Any(x => x.UpstreamHttpMethod.Count == 0);

            var duplicateAllowAllVerbs = matchingRoutes.Count(x => x.UpstreamHttpMethod.Count == 0) > 1;

            var specificVerbs = matchingRoutes.Any(x => x.UpstreamHttpMethod.Count != 0);

            var duplicateSpecificVerbs = matchingRoutes.SelectMany(x => x.UpstreamHttpMethod).GroupBy(x => x.ToLower()).SelectMany(x => x.Skip(1)).Any();

            if (duplicateAllowAllVerbs || duplicateSpecificVerbs || (allowAllVerbs && specificVerbs))
            {
                return(false);
            }

            return(true);
        }
        public void should_create()
        {
            var route = new FileRoute
            {
                UpstreamHeaderTransform = new Dictionary <string, string>
                {
                    { "Test", "Test, Chicken" },
                    { "Moop", "o, a" }
                },
                DownstreamHeaderTransform = new Dictionary <string, string>
                {
                    { "Pop", "West, East" },
                    { "Bop", "e, r" }
                }
            };

            var upstream = new List <HeaderFindAndReplace>
            {
                new HeaderFindAndReplace("Test", "Test", "Chicken", 0),
                new HeaderFindAndReplace("Moop", "o", "a", 0)
            };

            var downstream = new List <HeaderFindAndReplace>
            {
                new HeaderFindAndReplace("Pop", "West", "East", 0),
                new HeaderFindAndReplace("Bop", "e", "r", 0)
            };

            this.Given(x => GivenTheRoute(route))
            .When(x => WhenICreate())
            .Then(x => ThenTheFollowingUpstreamIsReturned(upstream))
            .Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
            .BDDfy();
        }
 public void TestBadPageRouting(string requestedPath, string routedPath)
 {
     Assert.Throws <Exception>(() => {
         IRoute route = new FileRoute(requestedPath, routedPath);
         route.ValidateSetup();
     });
 }
Exemple #9
0
        public void should_be_valid_if_specified_authentication_provider_is_registered()
        {
            const string key = "JwtLads";

            var fileRoute = new FileRoute
            {
                DownstreamPathTemplate = "/test",
                UpstreamPathTemplate   = "/test",
                AuthenticationOptions  = new FileAuthenticationOptions
                {
                    AuthenticationProviderKey = key
                },
                DownstreamHostAndPorts = new List <FileHostAndPort>
                {
                    new FileHostAndPort
                    {
                        Host = "localhost",
                        Port = 5000
                    }
                }
            };

            this.Given(_ => GivenThe(fileRoute))
            .And(_ => GivenAnAuthProvider(key))
            .When(_ => WhenIValidate())
            .Then(_ => ThenTheResultIsValid())
            .BDDfy();
        }
        public string Create(FileRoute fileRoute)
        {
            if (IsStickySession(fileRoute))
            {
                return($"{nameof(CookieStickySessions)}:{fileRoute.LoadBalancerOptions.Key}");
            }

            return($"{fileRoute.UpstreamPathTemplate}|{string.Join(",", fileRoute.UpstreamHttpMethod)}|{string.Join(",", fileRoute.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}");
        }
        private static bool IsNotDuplicateIn(FileRoute route,
                                             List <FileAggregateRoute> aggregateRoutes)
        {
            var duplicate = aggregateRoutes
                            .Any(a => a.UpstreamPathTemplate == route.UpstreamPathTemplate &&
                                 a.UpstreamHost == route.UpstreamHost &&
                                 route.UpstreamHttpMethod.Select(x => x.ToLower()).Contains("get"));

            return(!duplicate);
        }
        public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default()
        {
            var fileRoute       = new FileRoute();
            var expectedOptions = new HttpHandlerOptions(false, false, false, true, int.MaxValue);

            this.Given(x => GivenTheFollowing(fileRoute))
            .When(x => WhenICreateHttpHandlerOptions())
            .Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
            .BDDfy();
        }
Exemple #13
0
        public void downstream_path_template_should_not_be_empty()
        {
            var fileRoute = new FileRoute();

            this.Given(_ => GivenThe(fileRoute))
            .When(_ => WhenIValidate())
            .Then(_ => ThenTheResultIsInvalid())
            .And(_ => ThenTheErrorsContains("Downstream Path Template cannot be empty"))
            .BDDfy();
        }
        public string Create(FileRoute fileRoute, FileGlobalConfiguration globalConfiguration)
        {
            var routeId = !string.IsNullOrEmpty(fileRoute.RequestIdKey);

            var requestIdKey = routeId
               ? fileRoute.RequestIdKey
               : globalConfiguration.RequestIdKey;

            return(requestIdKey);
        }
        private bool IsStickySession(FileRoute fileRoute)
        {
            if (!string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Type) &&
                !string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Key) &&
                fileRoute.LoadBalancerOptions.Type == nameof(CookieStickySessions))
            {
                return(true);
            }

            return(false);
        }
Exemple #16
0
        public void should_create_template_pattern_that_matches_query_string_with_multiple_params()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/api/subscriptions/{subscriptionId}/updates?unitId={unitId}&productId={productId}"
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^(?i)/api/subscriptions/[^/]+/updates\\?unitId=.+&productId=.+$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #17
0
        public void should_create_template_pattern_that_matches_to_end_of_string_when_slash_and_placeholder()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/{url}"
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^/.*"))
            .And(x => ThenThePriorityIs(0))
            .BDDfy();
        }
        private Route SetUpRoute(FileRoute fileRoute, DownstreamRoute downstreamRoutes)
        {
            var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileRoute);

            var route = new RouteBuilder()
                        .WithUpstreamHttpMethod(fileRoute.UpstreamHttpMethod)
                        .WithUpstreamPathTemplate(upstreamTemplatePattern)
                        .WithDownstreamRoute(downstreamRoutes)
                        .WithUpstreamHost(fileRoute.UpstreamHost)
                        .Build();

            return(route);
        }
        public string Create(FileRoute route)
        {
            if (!string.IsNullOrEmpty(route?.FileCacheOptions?.Region))
            {
                return(route?.FileCacheOptions?.Region);
            }

            var methods = string.Join("", route.UpstreamHttpMethod.Select(m => m));

            var region = $"{methods}{route.UpstreamPathTemplate.Replace("/", "")}";

            return(region);
        }
Exemple #20
0
        public void downstream_path_template_should_not_contain_scheme(string downstreamPathTemplate)
        {
            var fileRoute = new FileRoute
            {
                DownstreamPathTemplate = downstreamPathTemplate
            };

            this.Given(_ => GivenThe(fileRoute))
            .When(_ => WhenIValidate())
            .Then(_ => ThenTheResultIsInvalid())
            .And(_ => ThenTheErrorsContains($"Downstream Path Template {downstreamPathTemplate} contains double forward slash, Ocelot does not support this at the moment. Please raise an issue in GitHib if you need this feature."))
            .BDDfy();
        }
Exemple #21
0
        public void downstream_path_template_should_start_with_forward_slash()
        {
            var fileRoute = new FileRoute
            {
                DownstreamPathTemplate = "test"
            };

            this.Given(_ => GivenThe(fileRoute))
            .When(_ => WhenIValidate())
            .Then(_ => ThenTheResultIsInvalid())
            .And(_ => ThenTheErrorsContains("Downstream Path Template test doesnt start with forward slash"))
            .BDDfy();
        }
Exemple #22
0
        public void should_match_forward_slash_or_no_forward_slash_if_template_end_with_forward_slash()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/PRODUCTS/",
                RouteIsCaseSensitive = false
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS(/|)$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #23
0
        public void should_set_upstream_template_pattern_to_ignore_case_sensitivity()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/PRODUCTS/{productId}",
                RouteIsCaseSensitive = false
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS(|/.+|/[\\?&#].+)$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #24
0
        public void should_use_zero_priority()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/{catchAll}",
                Priority             = 1
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^/.*"))
            .And(x => ThenThePriorityIs(0))
            .BDDfy();
        }
Exemple #25
0
        public void should_use_re_route_priority()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/orders/{catchAll}",
                Priority             = 0
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^(?i)/orders(|/.+|/[\\?&#].+)$"))
            .And(x => ThenThePriorityIs(0))
            .BDDfy();
        }
Exemple #26
0
        public void should_match_up_to_next_slash()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/api/v{apiVersion}/cards",
                Priority             = 0
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^(?i)/api/v[^/]+/cards$"))
            .And(x => ThenThePriorityIs(0))
            .BDDfy();
        }
Exemple #27
0
        public void should_create_template_pattern_that_starts_with_placeholder_then_has_another_later()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/{productId}/products/variants/{variantId}/",
                RouteIsCaseSensitive = true
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^/[^/]+/products/variants/[^/]+(/|)$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #28
0
        public void should_create_template_pattern_that_matches_more_than_one_placeholder_with_trailing_slash()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}/",
                RouteIsCaseSensitive = true
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^/api/products/[^/]+/variants/[^/]+(/|)$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #29
0
        public void should_create_template_pattern_that_matches_anything_to_end_of_string()
        {
            var fileRoute = new FileRoute
            {
                UpstreamPathTemplate = "/api/products/{productId}",
                RouteIsCaseSensitive = true
            };

            this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
            .When(x => x.WhenICreateTheTemplatePattern())
            .Then(x => x.ThenTheFollowingIsReturned("^/api/products(|/.+|/[\\?&#].+)$"))
            .And(x => ThenThePriorityIs(1))
            .BDDfy();
        }
Exemple #30
0
        public void should_be_valid_if_using_service_discovery_and_no_host_and_ports()
        {
            var fileRoute = new FileRoute
            {
                DownstreamPathTemplate = "/test",
                UpstreamPathTemplate   = "/test",
                ServiceName            = "Lads"
            };

            this.Given(_ => GivenThe(fileRoute))
            .When(_ => WhenIValidate())
            .Then(_ => ThenTheResultIsValid())
            .BDDfy();
        }