Merge branch 'master' into develop

This commit is contained in:
TomPallister 2017-04-23 15:40:50 +01:00
commit 1a76c9fbd9
3 changed files with 48 additions and 6 deletions

View File

@ -19,7 +19,7 @@ namespace Ocelot.DownstreamRouteFinder.UrlMatcher
{ {
var variableName = GetPlaceholderVariableName(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); var templateVariableNameAndValue = new UrlPathPlaceholderNameAndValue(variableName, variableValue);
@ -40,11 +40,11 @@ namespace Ocelot.DownstreamRouteFinder.UrlMatcher
return new OkResponse<List<UrlPathPlaceholderNameAndValue>>(templateKeysAndValues); return new OkResponse<List<UrlPathPlaceholderNameAndValue>>(templateKeysAndValues);
} }
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl) private string GetPlaceholderVariableValue(string urlPathTemplate, string variableName, string urlPath, int counterForUrl)
{ {
var positionOfNextSlash = urlPath.IndexOf('/', counterForUrl); var positionOfNextSlash = urlPath.IndexOf('/', counterForUrl);
if(positionOfNextSlash == -1) if (positionOfNextSlash == -1 || urlPathTemplate.Trim('/').EndsWith(variableName))
{ {
positionOfNextSlash = urlPath.Length; positionOfNextSlash = urlPath.Length;
} }

View File

@ -46,7 +46,6 @@ namespace Ocelot.AcceptanceTests
DownstreamPort = 51879, DownstreamPort = 51879,
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
UpstreamHttpMethod = "Get", UpstreamHttpMethod = "Get",
} }
} }
}; };
@ -292,6 +291,34 @@ namespace Ocelot.AcceptanceTests
.BDDfy(); .BDDfy();
} }
[Fact]
public void should_return_response_200_with_placeholder_for_final_url_path()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
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) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _builder = new WebHostBuilder()

View File

@ -140,6 +140,21 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
.BDDfy(); .BDDfy();
} }
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
{
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
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<UrlPathPlaceholderNameAndValue> expectedResults) private void ThenTheTemplatesVariablesAre(List<UrlPathPlaceholderNameAndValue> expectedResults)
{ {
foreach (var expectedResult in expectedResults) foreach (var expectedResult in expectedResults)