private void GivenTheDownStreamRouteFinderReturns(Ocelot.DownstreamRouteFinder.DownstreamRouteHolder downstreamRoute)
 {
     _downstreamRoute = new OkResponse <Ocelot.DownstreamRouteFinder.DownstreamRouteHolder>(downstreamRoute);
     _finder
     .Setup(x => x.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IInternalConfiguration>(), It.IsAny <string>()))
     .Returns(_downstreamRoute);
 }
Exemplo n.º 2
0
        public void should_call_middleware_and_ratelimiting()
        {
            var upstreamTemplate = new UpstreamPathTemplateBuilder().Build();

            var downstreamRoute = new DownstreamRouteBuilder()
                                  .WithEnableRateLimiting(true)
                                  .WithRateLimitOptions(new RateLimitOptions(true, "ClientId", () => new List <string>(), false, "", "", new RateLimitRule("1s", 100, 3), 429))
                                  .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                  .WithUpstreamPathTemplate(upstreamTemplate)
                                  .Build();

            var route = new RouteBuilder()
                        .WithDownstreamRoute(downstreamRoute)
                        .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                        .Build();

            var downstreamRouteHolder = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>(), route);

            this.Given(x => x.WhenICallTheMiddlewareMultipleTimes(2, downstreamRouteHolder))
            .Then(x => x.ThenThereIsNoDownstreamResponse())
            .When(x => x.WhenICallTheMiddlewareMultipleTimes(3, downstreamRouteHolder))
            .Then(x => x.ThenTheResponseIs429())
            .BDDfy();
        }
Exemplo n.º 3
0
        public void should_call_add_queries_correctly()
        {
            var downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <PlaceholderNameAndValue>(),
                                                                                         new RouteBuilder()
                                                                                         .WithDownstreamRoute(new DownstreamRouteBuilder()
                                                                                                              .WithDownstreamPathTemplate("any old string")
                                                                                                              .WithClaimsToQueries(new List <ClaimToThing>
            {
                new ClaimToThing("UserId", "Subject", "", 0)
            })
                                                                                                              .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                                              .Build())
                                                                                         .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                         .Build());

            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
            .And(x => x.GivenTheAddHeadersToRequestReturnsOk())
            .When(x => x.WhenICallTheMiddleware())
            .Then(x => x.ThenTheAddQueriesToRequestIsCalledCorrectly())
            .BDDfy();
        }
Exemplo n.º 4
0
        private void WhenICallTheMiddlewareMultipleTimes(int times, Ocelot.DownstreamRouteFinder.DownstreamRouteHolder downstreamRoute)
        {
            var httpContexts = new List <HttpContext>();

            for (int i = 0; i < times; i++)
            {
                var httpContext = new DefaultHttpContext();
                httpContext.Response.Body = new FakeStream();
                httpContext.Items.UpsertDownstreamRoute(downstreamRoute.Route.DownstreamRoute[0]);
                httpContext.Items.UpsertTemplatePlaceholderNameAndValues(downstreamRoute.TemplatePlaceholderNameAndValues);
                httpContext.Items.UpsertDownstreamRoute(downstreamRoute);
                var clientId = "ocelotclient1";
                var request  = new HttpRequestMessage(new HttpMethod("GET"), _url);
                httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(request));
                httpContext.Request.Headers.TryAdd("ClientId", clientId);
                httpContexts.Add(httpContext);
            }

            foreach (var httpContext in httpContexts)
            {
                _middleware.Invoke(httpContext).GetAwaiter().GetResult();
                var ds = httpContext.Items.DownstreamResponse();
                _downstreamResponse = ds;
            }
        }
Exemplo n.º 5
0
        public void should_not_replace_by_empty_scheme()
        {
            var downstreamRoute = new DownstreamRouteBuilder()
                                  .WithDownstreamScheme("")
                                  .WithServiceName("Ocelot/OcelotApp")
                                  .WithUseServiceDiscovery(true)
                                  .Build();

            var downstreamRouteHolder = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(
                new List <PlaceholderNameAndValue>(),
                new RouteBuilder()
                .WithDownstreamRoute(downstreamRoute)
                .Build());

            var config = new ServiceProviderConfigurationBuilder()
                         .WithType("ServiceFabric")
                         .WithHost("localhost")
                         .WithPort(19081)
                         .Build();

            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRouteHolder))
            .And(x => GivenTheServiceProviderConfigIs(config))
            .And(x => x.GivenTheDownstreamRequestUriIs("https://localhost:19081?PartitionKind=test&PartitionKey=1"))
            .And(x => x.GivenTheUrlReplacerWillReturnSequence("/api/products/1", "Ocelot/OcelotApp"))
            .When(x => x.WhenICallTheMiddleware())
            .Then(x => x.ThenTheDownstreamRequestUriIs("https://localhost:19081/Ocelot/OcelotApp/api/products/1?PartitionKind=test&PartitionKey=1"))
            .BDDfy();
        }
Exemplo n.º 6
0
        public void should_create_service_fabric_url_with_version_from_upstream_path_template()
        {
            var downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(
                new List <PlaceholderNameAndValue>(),
                new RouteBuilder().WithDownstreamRoute(
                    new DownstreamRouteBuilder()
                    .WithDownstreamScheme("http")
                    .WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().WithOriginalValue("/products").Build())
                    .WithUseServiceDiscovery(true)
                    .Build()
                    ).Build());

            var config = new ServiceProviderConfigurationBuilder()
                         .WithType("ServiceFabric")
                         .WithHost("localhost")
                         .WithPort(19081)
                         .Build();

            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
            .And(x => GivenTheServiceProviderConfigIs(config))
            .And(x => x.GivenTheDownstreamRequestUriIs("http://localhost:19081?PartitionKind=test&PartitionKey=1"))
            .And(x => x.GivenTheUrlReplacerWillReturnSequence("/products", "Service_1.0/Api"))
            .When(x => x.WhenICallTheMiddleware())
            .Then(x => x.ThenTheDownstreamRequestUriIs("http://localhost:19081/Service_1.0/Api/products?PartitionKind=test&PartitionKey=1"))
            .BDDfy();
        }
Exemplo n.º 7
0
        public void should_call_claims_to_request_correctly()
        {
            var downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <PlaceholderNameAndValue>(),
                                                                                         new RouteBuilder()
                                                                                         .WithDownstreamRoute(new DownstreamRouteBuilder()
                                                                                                              .WithDownstreamPathTemplate("any old string")
                                                                                                              .WithClaimsToClaims(new List <ClaimToThing>
            {
                new ClaimToThing("sub", "UserType", "|", 0)
            })
                                                                                                              .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                                              .Build())
                                                                                         .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                         .Build());

            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
            .And(x => x.GivenTheAddClaimsToRequestReturns())
            .When(x => x.WhenICallTheMiddleware())
            .Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
            .BDDfy();
        }
Exemplo n.º 8
0
        private void GivenTheDownStreamRouteIs(Ocelot.DownstreamRouteFinder.DownstreamRouteHolder downstreamRoute)
        {
            _downstreamRoute = new OkResponse <Ocelot.DownstreamRouteFinder.DownstreamRouteHolder>(downstreamRoute);

            _httpContext.Items.UpsertTemplatePlaceholderNameAndValues(downstreamRoute.TemplatePlaceholderNameAndValues);

            _httpContext.Items.UpsertDownstreamRoute(downstreamRoute.Route.DownstreamRoute[0]);
        }
        private void GivenTheRouteHasPreFindAndReplaceSetUp()
        {
            var fAndRs = new List <HeaderFindAndReplace>();
            var route  = new RouteBuilder()
                         .WithDownstreamRoute(new DownstreamRouteBuilder().WithUpstreamHeaderFindAndReplace(fAndRs)
                                              .WithDownstreamHeaderFindAndReplace(fAndRs).Build())
                         .Build();

            var dR = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(null, route);

            _httpContext.Items.UpsertTemplatePlaceholderNameAndValues(dR.TemplatePlaceholderNameAndValues);
            _httpContext.Items.UpsertDownstreamRoute(dR.Route.DownstreamRoute[0]);
        }
Exemplo n.º 10
0
        private void GivenTheDownstreamRouteIs()
        {
            var route = new RouteBuilder()
                        .WithDownstreamRoute(new DownstreamRouteBuilder()
                                             .WithIsCached(true)
                                             .WithCacheOptions(new CacheOptions(100, "kanken"))
                                             .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                             .Build())
                        .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                        .Build();

            var downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <PlaceholderNameAndValue>(), route);

            _httpContext.Items.UpsertTemplatePlaceholderNameAndValues(downstreamRoute.TemplatePlaceholderNameAndValues);

            _httpContext.Items.UpsertDownstreamRoute(downstreamRoute.Route.DownstreamRoute[0]);
        }
Exemplo n.º 11
0
        public void should_call_middleware_withWhitelistClient()
        {
            var downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>(),
                                                                                         new RouteBuilder()
                                                                                         .WithDownstreamRoute(new DownstreamRouteBuilder()
                                                                                                              .WithEnableRateLimiting(true)
                                                                                                              .WithRateLimitOptions(
                                                                                                                  new Ocelot.Configuration.RateLimitOptions(true, "ClientId", () => new List <string>()
            {
                "ocelotclient2"
            }, false, "", "", new RateLimitRule("1s", 100, 3), 429))
                                                                                                              .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                                              .Build())
                                                                                         .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                                                                         .Build());

            this.Given(x => x.WhenICallTheMiddlewareWithWhiteClient(downstreamRoute))
            .Then(x => x.ThenThereIsNoDownstreamResponse())
            .BDDfy();
        }
Exemplo n.º 12
0
 private void GivenTheFollowing(Route route)
 {
     _downstreamRoute = new Ocelot.DownstreamRouteFinder.DownstreamRouteHolder(new List <PlaceholderNameAndValue>(), route);
     _httpContext.Items.UpsertDownstreamRoute(_downstreamRoute);
 }