diff --git a/src/Ocelot/DownstreamRouteFinder/UrlMatcher/UrlPathPlaceholderNameAndValueFinder.cs b/src/Ocelot/DownstreamRouteFinder/UrlMatcher/UrlPathPlaceholderNameAndValueFinder.cs index 249b153e9..0859f4c02 100644 --- a/src/Ocelot/DownstreamRouteFinder/UrlMatcher/UrlPathPlaceholderNameAndValueFinder.cs +++ b/src/Ocelot/DownstreamRouteFinder/UrlMatcher/UrlPathPlaceholderNameAndValueFinder.cs @@ -18,8 +18,8 @@ public Response> Find(string upstreamUrlPat if (IsPlaceholder(upstreamUrlPathTemplate[counterForTemplate])) { var variableName = GetPlaceholderVariableName(upstreamUrlPathTemplate, counterForTemplate); - - var variableValue = GetPlaceholderVariableValue(upstreamUrlPath, counterForUrl); + + var variableValue = GetPlaceholderVariableValue(upstreamUrlPathTemplate, variableName, upstreamUrlPath, counterForUrl); var templateVariableNameAndValue = new UrlPathPlaceholderNameAndValue(variableName, variableValue); @@ -40,15 +40,15 @@ public Response> Find(string upstreamUrlPat return new OkResponse>(templateKeysAndValues); } - private string GetPlaceholderVariableValue(string urlPath, int counterForUrl) + private string GetPlaceholderVariableValue(string urlPathTemplate, string variableName, string urlPath, int counterForUrl) { var positionOfNextSlash = urlPath.IndexOf('/', counterForUrl); - if(positionOfNextSlash == -1) + if (positionOfNextSlash == -1 || urlPathTemplate.Trim('/').EndsWith(variableName)) { positionOfNextSlash = urlPath.Length; } - + var variableValue = urlPath.Substring(counterForUrl, positionOfNextSlash - counterForUrl); return variableValue; diff --git a/test/Ocelot.AcceptanceTests/RoutingTests.cs b/test/Ocelot.AcceptanceTests/RoutingTests.cs index c1a6716c9..99c11877a 100644 --- a/test/Ocelot.AcceptanceTests/RoutingTests.cs +++ b/test/Ocelot.AcceptanceTests/RoutingTests.cs @@ -46,7 +46,6 @@ public void should_return_response_200_with_simple_url() DownstreamPort = 51879, UpstreamPathTemplate = "/", UpstreamHttpMethod = "Get", - } } }; @@ -292,6 +291,34 @@ public void should_return_response_201_with_complex_query_string() .BDDfy(); } + [Fact] + public void should_return_response_200_with_placeholder_for_final_url_path() + { + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/api/{urlPath}", + DownstreamScheme = "http", + DownstreamHost = "localhost", + DownstreamPort = 51879, + UpstreamPathTemplate = "/myApp1Name/api/{urlPath}", + UpstreamHttpMethod = "Get", + } + } + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879/myApp1Name/api/products/1", 200, "Some Product")) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/myApp1Name/api/products/1")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => _steps.ThenTheResponseBodyShouldBe("Some Product")) + .BDDfy(); + } + private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody) { _builder = new WebHostBuilder() diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/UrlMatcher/TemplateVariableNameAndValueFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/UrlMatcher/TemplateVariableNameAndValueFinderTests.cs index cf71c809e..7c73483a7 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/UrlMatcher/TemplateVariableNameAndValueFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/UrlMatcher/TemplateVariableNameAndValueFinderTests.cs @@ -140,6 +140,21 @@ public void can_match_down_stream_url_with_downstream_template_with_three_place_ .BDDfy(); } + [Fact] + public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path() + { + var expectedTemplates = new List + { + new UrlPathPlaceholderNameAndValue("{finalUrlPath}", "product/products/categories/"), + }; + + this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/categories/")) + .And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/{finalUrlPath}/")) + .When(x => x.WhenIFindTheUrlVariableNamesAndValues()) + .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates)) + .BDDfy(); + } + private void ThenTheTemplatesVariablesAre(List expectedResults) { foreach (var expectedResult in expectedResults)