public void should_set_scheme() { var downstreamRoute = new DownstreamRouteBuilder() .WithUpstreamHttpMethod(new List <string> { "Get" }) .Build(); var serviceProviderConfig = new ServiceProviderConfigurationBuilder() .Build(); this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123")) .And(x => GivenTheConfigurationIs(serviceProviderConfig)) .And(x => x.GivenTheDownStreamRouteIs(downstreamRoute, new List <Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>())) .And(x => x.GivenTheLoadBalancerHouseReturns()) .And(x => x.GivenTheLoadBalancerReturnsOk()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenAnHostAndPortIsSetOnPipeline()) .BDDfy(); }
public void should_store_load_balancers_by_key() { var route = new DownstreamRouteBuilder() .WithLoadBalancerOptions(new LoadBalancerOptions("FakeLoadBalancer", "", 0)) .WithLoadBalancerKey("test") .Build(); var routeTwo = new DownstreamRouteBuilder() .WithLoadBalancerOptions(new LoadBalancerOptions("FakeRoundRobinLoadBalancer", "", 0)) .WithLoadBalancerKey("testtwo") .Build(); this.Given(x => x.GivenThereIsALoadBalancer(route, new FakeLoadBalancer())) .And(x => x.GivenThereIsALoadBalancer(routeTwo, new FakeRoundRobinLoadBalancer())) .When(x => x.WhenWeGetTheLoadBalancer(route)) .Then(x => x.ThenTheLoadBalancerIs <FakeLoadBalancer>()) .When(x => x.WhenWeGetTheLoadBalancer(routeTwo)) .Then(x => x.ThenTheLoadBalancerIs <FakeRoundRobinLoadBalancer>()) .BDDfy(); }
public void should_aggregate_n_responses_and_set_response_content_on_upstream_context() { var billDownstreamRoute = new DownstreamRouteBuilder().WithKey("Bill").Build(); var georgeDownstreamRoute = new DownstreamRouteBuilder().WithKey("George").Build(); var downstreamRoutes = new List <DownstreamRoute> { billDownstreamRoute, georgeDownstreamRoute }; var route = new RouteBuilder() .WithDownstreamRoutes(downstreamRoutes) .Build(); var billDownstreamContext = new DefaultHttpContext(); billDownstreamContext.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new EditableList <KeyValuePair <string, IEnumerable <string> > >(), "some reason")); billDownstreamContext.Items.UpsertDownstreamRoute(billDownstreamRoute); var georgeDownstreamContext = new DefaultHttpContext(); georgeDownstreamContext.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("George says hi"), HttpStatusCode.OK, new List <KeyValuePair <string, IEnumerable <string> > >(), "some reason")); georgeDownstreamContext.Items.UpsertDownstreamRoute(georgeDownstreamRoute); var downstreamContexts = new List <HttpContext> { billDownstreamContext, georgeDownstreamContext }; var expected = "{\"Bill\":Bill says hi,\"George\":George says hi}"; this.Given(x => GivenTheUpstreamContext(new DefaultHttpContext())) .And(x => GivenTheRoute(route)) .And(x => GivenTheDownstreamContext(downstreamContexts)) .When(x => WhenIAggregate()) .Then(x => ThenTheContentIs(expected)) .And(x => ThenTheContentTypeIs("application/json")) .And(x => ThenTheReasonPhraseIs("cannot return from aggregate..which reason phrase would you use?")) .BDDfy(); }
public void should_all_from_all_routes_provider_and_qos() { var qosOptions = new QoSOptionsBuilder() .WithTimeoutValue(1) .WithDurationOfBreak(1) .WithExceptionsAllowedBeforeBreaking(1) .Build(); var route = new DownstreamRouteBuilder() .WithQosOptions(qosOptions) .WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false, true, int.MaxValue)).WithLoadBalancerKey("").Build(); this.Given(x => GivenTheFollowingRequest(route)) .And(x => GivenTheQosFactoryReturns(new FakeQoSHandler())) .And(x => GivenTheServiceProviderReturnsGlobalDelegatingHandlers <FakeDelegatingHandler, FakeDelegatingHandlerTwo>()) .When(x => WhenIGet()) .Then(x => ThenThereIsDelegatesInProvider(3)) .And(x => ThenTheDelegatesAreAddedCorrectly()) .And(x => ThenItIsQosHandler(2)) .BDDfy(); }
public void should_log_if_ignoring_ssl_errors() { var qosOptions = new QoSOptionsBuilder() .Build(); var route = new DownstreamRouteBuilder() .WithQosOptions(qosOptions) .WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false, true, int.MaxValue)) .WithLoadBalancerKey("") .WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().WithOriginalValue("").Build()) .WithQosOptions(new QoSOptionsBuilder().Build()) .WithDangerousAcceptAnyServerCertificateValidator(true) .Build(); this.Given(x => GivenTheFactoryReturns()) .And(x => GivenARequest(route)) .When(x => WhenIBuild()) .Then(x => ThenTheHttpClientShouldNotBeNull()) .Then(x => ThenTheDangerousAcceptAnyServerCertificateValidatorWarningIsLogged()) .BDDfy(); }
public void should_apply_re_route_specific() { var qosOptions = new QoSOptionsBuilder() .Build(); var route = new DownstreamRouteBuilder() .WithQosOptions(qosOptions) .WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false, true, int.MaxValue)) .WithDelegatingHandlers(new List <string> { "FakeDelegatingHandler", "FakeDelegatingHandlerTwo" }) .WithLoadBalancerKey("") .Build(); this.Given(x => GivenTheFollowingRequest(route)) .And(x => GivenTheServiceProviderReturnsSpecificDelegatingHandlers <FakeDelegatingHandler, FakeDelegatingHandlerTwo>()) .When(x => WhenIGet()) .Then(x => ThenThereIsDelegatesInProvider(2)) .And(x => ThenTheDelegatesAreAddedCorrectly()) .BDDfy(); }
public void should_get_from_cache() { var qosOptions = new QoSOptionsBuilder() .Build(); var route = new DownstreamRouteBuilder() .WithQosOptions(qosOptions) .WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false, true, int.MaxValue)) .WithLoadBalancerKey("") .WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().WithOriginalValue("").Build()) .WithQosOptions(new QoSOptionsBuilder().Build()) .Build(); this.Given(x => GivenARealCache()) .And(x => GivenTheFactoryReturns()) .And(x => GivenARequest(route)) .And(x => WhenIBuildTheFirstTime()) .And(x => WhenISave()) .And(x => WhenIBuildAgain()) .And(x => WhenISave()) .When(x => WhenIBuildAgain()) .Then(x => ThenTheHttpClientIsFromTheCache()) .BDDfy(); }
public void should_add_verb_to_cache_key(string verb) { var downstreamUrl = "http://localhost:5012/"; var method = new HttpMethod(verb); var qosOptions = new QoSOptionsBuilder() .Build(); var route = new DownstreamRouteBuilder() .WithQosOptions(qosOptions) .WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false, true, int.MaxValue)) .WithLoadBalancerKey("") .WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().WithOriginalValue("").Build()) .WithQosOptions(new QoSOptionsBuilder().Build()) .Build(); this.Given(_ => GivenADownstreamService()) .And(_ => GivenARequestWithAUrlAndMethod(route, downstreamUrl, method)) .And(_ => GivenTheFactoryReturnsNothing()) .And(_ => WhenIBuild()) .And(_ => GivenCacheIsCalledWithExpectedKey($"{method.ToString()}:{downstreamUrl}")) .BDDfy(); }
public Response <DownstreamRouteHolder> Get(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost) { var serviceName = IsSystemApi(upstreamUrlPath) ? "baseService" : GetServiceName(upstreamUrlPath); //var downstreamPath = IsSystemApi(upstreamUrlPath) ? upstreamUrlPath : GetDownstreamPath(upstreamUrlPath); var downstreamPath = upstreamUrlPath; if (HasQueryString(downstreamPath)) { downstreamPath = RemoveQueryString(downstreamPath); } var downstreamPathForKeys = $"/{serviceName}{downstreamPath}"; var loadBalancerKey = CreateLoadBalancerKey(downstreamPathForKeys, upstreamHttpMethod, configuration.LoadBalancerOptions); if (_cache.TryGetValue(loadBalancerKey, out var downstreamRouteHolder)) { return(downstreamRouteHolder); } var qosOptions = _qoSOptionsCreator.Create(configuration.QoSOptions, downstreamPathForKeys, new List <string> { upstreamHttpMethod }); var upstreamPathTemplate = new UpstreamPathTemplateBuilder().WithOriginalValue(upstreamUrlPath).Build(); var downstreamRouteBuilder = new DownstreamRouteBuilder() .WithServiceName(serviceName) .WithLoadBalancerKey(loadBalancerKey) .WithDownstreamPathTemplate(downstreamPath) .WithUseServiceDiscovery(true) .WithHttpHandlerOptions(configuration.HttpHandlerOptions) .WithQosOptions(qosOptions) .WithDownstreamScheme(configuration.DownstreamScheme) .WithLoadBalancerOptions(configuration.LoadBalancerOptions) .WithDownstreamHttpVersion(configuration.DownstreamHttpVersion) .WithUpstreamPathTemplate(upstreamPathTemplate); var rateLimitOptions = configuration.Routes != null ? configuration.Routes.SelectMany(t => t.DownstreamRoute).FirstOrDefault(t => t.ServiceName == serviceName) : null; if (rateLimitOptions != null) { downstreamRouteBuilder .WithRateLimitOptions(rateLimitOptions.RateLimitOptions) .WithEnableRateLimiting(true); } var downstreamRoute = downstreamRouteBuilder.Build(); var route = new RouteBuilder() .WithDownstreamRoute(downstreamRoute) .WithUpstreamHttpMethod(new List <string>() { upstreamHttpMethod }) .WithUpstreamPathTemplate(upstreamPathTemplate) .Build(); downstreamRouteHolder = new OkResponse <DownstreamRouteHolder>(new DownstreamRouteHolder(new List <PlaceholderNameAndValue>(), route)); _cache.AddOrUpdate(loadBalancerKey, downstreamRouteHolder, (x, y) => downstreamRouteHolder); return(downstreamRouteHolder); }
private DownstreamRoute SetUpDownstreamRoute(FileRoute fileRoute, FileGlobalConfiguration globalConfiguration) { var fileRouteOptions = _fileRouteOptionsCreator.Create(fileRoute); var requestIdKey = _requestIdKeyCreator.Create(fileRoute, globalConfiguration); var routeKey = _routeKeyCreator.Create(fileRoute); var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileRoute); var authOptionsForRoute = _authOptionsCreator.Create(fileRoute); var claimsToHeaders = _claimsToThingCreator.Create(fileRoute.AddHeadersToRequest); var claimsToClaims = _claimsToThingCreator.Create(fileRoute.AddClaimsToRequest); var claimsToQueries = _claimsToThingCreator.Create(fileRoute.AddQueriesToRequest); var claimsToDownstreamPath = _claimsToThingCreator.Create(fileRoute.ChangeDownstreamPathTemplate); var qosOptions = _qosOptionsCreator.Create(fileRoute.QoSOptions, fileRoute.UpstreamPathTemplate, fileRoute.UpstreamHttpMethod); var rateLimitOption = _rateLimitOptionsCreator.Create(fileRoute.RateLimitOptions, globalConfiguration); var region = _regionCreator.Create(fileRoute); var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileRoute.HttpHandlerOptions); var hAndRs = _headerFAndRCreator.Create(fileRoute); var downstreamAddresses = _downstreamAddressesCreator.Create(fileRoute); var lbOptions = _loadBalancerOptionsCreator.Create(fileRoute.LoadBalancerOptions); var securityOptions = _securityOptionsCreator.Create(fileRoute.SecurityOptions); var downstreamHttpVersion = _versionCreator.Create(fileRoute.DownstreamHttpVersion); var route = new DownstreamRouteBuilder() .WithKey(fileRoute.Key) .WithDownstreamPathTemplate(fileRoute.DownstreamPathTemplate) .WithUpstreamHttpMethod(fileRoute.UpstreamHttpMethod) .WithUpstreamPathTemplate(upstreamTemplatePattern) .WithIsAuthenticated(fileRouteOptions.IsAuthenticated) .WithAuthenticationOptions(authOptionsForRoute) .WithClaimsToHeaders(claimsToHeaders) .WithClaimsToClaims(claimsToClaims) .WithRouteClaimsRequirement(fileRoute.RouteClaimsRequirement) .WithIsAuthorised(fileRouteOptions.IsAuthorised) .WithClaimsToQueries(claimsToQueries) .WithClaimsToDownstreamPath(claimsToDownstreamPath) .WithRequestIdKey(requestIdKey) .WithIsCached(fileRouteOptions.IsCached) .WithCacheOptions(new CacheOptions(fileRoute.FileCacheOptions.TtlSeconds, region)) .WithDownstreamScheme(fileRoute.DownstreamScheme) .WithLoadBalancerOptions(lbOptions) .WithDownstreamAddresses(downstreamAddresses) .WithLoadBalancerKey(routeKey) .WithQosOptions(qosOptions) .WithEnableRateLimiting(fileRouteOptions.EnableRateLimiting) .WithRateLimitOptions(rateLimitOption) .WithHttpHandlerOptions(httpHandlerOptions) .WithServiceName(fileRoute.ServiceName) .WithServiceNamespace(fileRoute.ServiceNamespace) .WithUseServiceDiscovery(fileRouteOptions.UseServiceDiscovery) .WithUpstreamHeaderFindAndReplace(hAndRs.Upstream) .WithDownstreamHeaderFindAndReplace(hAndRs.Downstream) .WithDelegatingHandlers(fileRoute.DelegatingHandlers) .WithAddHeadersToDownstream(hAndRs.AddHeadersToDownstream) .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) .WithDownstreamHttpVersion(downstreamHttpVersion) .WithDownStreamHttpMethod(fileRoute.DownstreamHttpMethod) .Build(); return(route); }