private string CreateReRouteKey(FileReRoute fileReRoute) { if (!string.IsNullOrEmpty(fileReRoute.LoadBalancerOptions.Type) && !string.IsNullOrEmpty(fileReRoute.LoadBalancerOptions.Key) && fileReRoute.LoadBalancerOptions.Type == nameof(CookieStickySessions)) { return($"{nameof(CookieStickySessions)}:{fileReRoute.LoadBalancerOptions.Key}"); } return($"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}|{string.Join(",", fileReRoute.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}"); }
private ReRoute SetUpReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration) { var fileReRouteOptions = _fileReRouteOptionsCreator.Create(fileReRoute); var requestIdKey = _requestIdKeyCreator.Create(fileReRoute, globalConfiguration); var reRouteKey = CreateReRouteKey(fileReRoute); var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute); var authOptionsForRoute = _authOptionsCreator.Create(fileReRoute); var claimsToHeaders = _claimsToThingCreator.Create(fileReRoute.AddHeadersToRequest); var claimsToClaims = _claimsToThingCreator.Create(fileReRoute.AddClaimsToRequest); var claimsToQueries = _claimsToThingCreator.Create(fileReRoute.AddQueriesToRequest); var qosOptions = _qosOptionsCreator.Create(fileReRoute); var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting); var region = _regionCreator.Create(fileReRoute); var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute); var reRoute = new ReRouteBuilder() .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate) .WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod) .WithUpstreamTemplatePattern(upstreamTemplatePattern) .WithIsAuthenticated(fileReRouteOptions.IsAuthenticated) .WithAuthenticationOptions(authOptionsForRoute) .WithClaimsToHeaders(claimsToHeaders) .WithClaimsToClaims(claimsToClaims) .WithRouteClaimsRequirement(fileReRoute.RouteClaimsRequirement) .WithIsAuthorised(fileReRouteOptions.IsAuthorised) .WithClaimsToQueries(claimsToQueries) .WithRequestIdKey(requestIdKey) .WithIsCached(fileReRouteOptions.IsCached) .WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region)) .WithDownstreamScheme(fileReRoute.DownstreamScheme) .WithLoadBalancer(fileReRoute.LoadBalancer) .WithDownstreamHost(fileReRoute.DownstreamHost) .WithDownstreamPort(fileReRoute.DownstreamPort) .WithReRouteKey(reRouteKey) .WithIsQos(fileReRouteOptions.IsQos) .WithQosOptions(qosOptions) .WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting) .WithRateLimitOptions(rateLimitOption) .WithHttpHandlerOptions(httpHandlerOptions) .WithServiceName(fileReRoute.ServiceName) .WithUseServiceDiscovery(fileReRoute.UseServiceDiscovery) .Build(); return(reRoute); }
private static bool IsNotDuplicateIn(FileReRoute reRoute, List <FileAggregateReRoute> aggregateReRoutes) { var duplicate = aggregateReRoutes .Any(a => a.UpstreamPathTemplate == reRoute.UpstreamPathTemplate && a.UpstreamHost == reRoute.UpstreamHost && reRoute.UpstreamHttpMethod.Select(x => x.ToLower()).Contains("get")); return(!duplicate); }
public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default() { var fileReRoute = new FileReRoute(); var expectedOptions = new HttpHandlerOptions(false, false, false); this.Given(x => GivenTheFollowing(fileReRoute)) .When(x => WhenICreateHttpHandlerOptions()) .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) .BDDfy(); }
public static string GetId(this FileReRoute fileReRoute) { return(fileReRoute == null ? string.Empty : $"{fileReRoute.DownstreamScheme}" + $"{GetHost(fileReRoute.DownstreamHostAndPorts)}" + $"{GetPort(fileReRoute.DownstreamHostAndPorts)}" + $"{fileReRoute.DownstreamPathTemplate}" .Replace('/', '_')); }
public string Create(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration) { var globalRequestIdConfiguration = !string.IsNullOrEmpty(globalConfiguration?.RequestIdKey); var requestIdKey = globalRequestIdConfiguration ? globalConfiguration.RequestIdKey : fileReRoute.RequestIdKey; return(requestIdKey); }
public string Create(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration) { var reRouteId = !string.IsNullOrEmpty(fileReRoute.RequestIdKey); var requestIdKey = reRouteId ? fileReRoute.RequestIdKey : globalConfiguration.RequestIdKey; return(requestIdKey); }
public void downstream_path_template_should_not_be_empty() { var fileReRoute = new FileReRoute(); this.Given(_ => GivenThe(fileReRoute)) .When(_ => WhenIValidate()) .Then(_ => ThenTheResultIsInvalid()) .And(_ => ThenTheErrorsContains("Downstream Path Template cannot be empty")) .BDDfy(); }
public AuthenticationOptions Create(FileReRoute fileReRoute) { var authenticationConfig = _creator.Create(fileReRoute.AuthenticationOptions); return(new AuthenticationOptionsBuilder() .WithProvider(fileReRoute.AuthenticationOptions?.Provider) .WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes) .WithConfig(authenticationConfig) .Build()); }
public AuthenticationOptions Create(FileReRoute fileReRoute) { return(new AuthenticationOptionsBuilder() .WithProvider(fileReRoute.AuthenticationOptions?.Provider) .WithProviderRootUrl(fileReRoute.AuthenticationOptions?.ProviderRootUrl) .WithScopeName(fileReRoute.AuthenticationOptions?.ScopeName) .WithRequireHttps(fileReRoute.AuthenticationOptions.RequireHttps) .WithAdditionalScopes(fileReRoute.AuthenticationOptions?.AdditionalScopes) .WithScopeSecret(fileReRoute.AuthenticationOptions?.ScopeSecret) .Build()); }
private bool IsStickySession(FileReRoute fileReRoute) { if (!string.IsNullOrEmpty(fileReRoute.LoadBalancerOptions.Type) && !string.IsNullOrEmpty(fileReRoute.LoadBalancerOptions.Key) && fileReRoute.LoadBalancerOptions.Type == nameof(CookieStickySessions)) { return(true); } return(false); }
public void should_create_template_pattern_that_matches_to_end_of_string() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/" }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^/$")) .BDDfy(); }
public void should_set_upstream_template_pattern_to_ignore_case_sensitivity() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/PRODUCTS/{productId}", ReRouteIsCaseSensitive = false }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS/[0-9a-zA-Z].*$")) .BDDfy(); }
public void downstream_path_template_should_start_with_forward_slash() { var fileReRoute = new FileReRoute { DownstreamPathTemplate = "test" }; this.Given(_ => GivenThe(fileReRoute)) .When(_ => WhenIValidate()) .Then(_ => ThenTheResultIsInvalid()) .And(_ => ThenTheErrorsContains("Downstream Path Template test doesnt start with forward slash")) .BDDfy(); }
public void downstream_path_template_should_not_contain_scheme(string downstreamPathTemplate) { var fileReRoute = new FileReRoute { DownstreamPathTemplate = downstreamPathTemplate }; this.Given(_ => GivenThe(fileReRoute)) .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(); }
public void should_create_template_pattern_that_matches_anything_to_end_of_string() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/api/products/{productId}", ReRouteIsCaseSensitive = true }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("/api/products/.*/$")) .BDDfy(); }
public void should_create_template_pattern_that_matches_more_than_one_placeholder_with_trailing_slash() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}/", ReRouteIsCaseSensitive = true }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("/api/products/.*/variants/.*/$")) .BDDfy(); }
public void should_create_template_pattern_that_starts_with_placeholder_then_has_another_later() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/{productId}/products/variants/{variantId}/", ReRouteIsCaseSensitive = true }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^/[0-9a-zA-Z].*/products/variants/[0-9a-zA-Z].*(/|)$")) .BDDfy(); }
private ReRoute SetUpReRoute(FileReRoute fileReRoute, DownstreamReRoute downstreamReRoutes) { var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute); var reRoute = new ReRouteBuilder() .WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod) .WithUpstreamPathTemplate(upstreamTemplatePattern) .WithDownstreamReRoute(downstreamReRoutes) .WithUpstreamHost(fileReRoute.UpstreamHost) .Build(); return(reRoute); }
public void should_match_forward_slash_or_no_forward_slash_if_template_end_with_forward_slash() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/PRODUCTS/", ReRouteIsCaseSensitive = false }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS(/|)$")) .BDDfy(); }
public void should_create_template_pattern_that_matches_query_string_with_multiple_params() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/api/subscriptions/{subscriptionId}/updates?unitId={unitId}&productId={productId}" }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^(?i)/api/subscriptions/[^/]+/updates\\?unitId=.+&productId=.+$")) .And(x => ThenThePriorityIs(1)) .BDDfy(); }
public void should_create_template_pattern_that_matches_to_end_of_string_when_slash_and_placeholder() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/{url}" }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^/.*")) .And(x => ThenThePriorityIs(0)) .BDDfy(); }
public void should_set_upstream_template_pattern_to_respect_case_sensitivity() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/PRODUCTS/{productId}", ReRouteIsCaseSensitive = true }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("/PRODUCTS/.*/$")) .BDDfy(); }
public string Create(FileReRoute reRoute) { if (!string.IsNullOrEmpty(reRoute?.FileCacheOptions?.Region)) { return(reRoute?.FileCacheOptions?.Region); } var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m)); var region = $"{methods}{reRoute.UpstreamPathTemplate.Replace("/", "")}"; return(region); }
public void WhenMissingPortInFileReRoute_Return0Port() { var fileReRoute = new FileReRoute { DownstreamScheme = "http", DownstreamHost = "localhost", DownstreamPathTemplate = "/test" }; var id = fileReRoute.GetId(); Assert.Equal("httplocalhost0_test", id); }
public void should_match_up_to_next_slash() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/api/v{apiVersion}/cards", Priority = 0 }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^(?i)/api/v[^/]+/cards$")) .And(x => ThenThePriorityIs(0)) .BDDfy(); }
private bool HaveServiceDiscoveryProviderRegistered(FileReRoute reRoute, FileServiceDiscoveryProvider serviceDiscoveryProvider) { if (string.IsNullOrEmpty(reRoute.ServiceName)) { return(true); } if (serviceDiscoveryProvider?.Type?.ToLower() == "servicefabric") { return(true); } return(_serviceDiscoveryFinderDelegates.Any()); }
public void should_create_template_pattern_that_matches_more_than_one_placeholder() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}", ReRouteIsCaseSensitive = true }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^/api/products/.+/variants/.+$")) .And(x => ThenThePriorityIs(1)) .BDDfy(); }
public void should_create_options_with_useproxy_true_as_default() { var fileReRoute = new FileReRoute { HttpHandlerOptions = new FileHttpHandlerOptions() }; var expectedOptions = new HttpHandlerOptions(false, false, false, true); this.Given(x => GivenTheFollowing(fileReRoute)) .When(x => WhenICreateHttpHandlerOptions()) .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) .BDDfy(); }
public void should_use_zero_priority() { var fileReRoute = new FileReRoute { UpstreamPathTemplate = "/{catchAll}", Priority = 1 }; this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute)) .When(x => x.WhenICreateTheTemplatePattern()) .Then(x => x.ThenTheFollowingIsReturned("^/.*")) .And(x => ThenThePriorityIs(0)) .BDDfy(); }