Added url replacer

This commit is contained in:
Tom Gardham-Pallister
2016-07-21 21:14:27 +01:00
parent dbff2b9530
commit d3b9fddcfd
8 changed files with 250 additions and 38 deletions

View File

@ -0,0 +1,137 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.UrlPathMatcher;
using Ocelot.Library.Infrastructure.UrlPathReplacer;
using Shouldly;
using Xunit;
namespace Ocelot.UnitTests
{
public class UpstreamUrlPathTemplateVariableReplacerTests
{
private string _upstreamUrlPath;
private UrlPathMatch _urlPathMatch;
private string _result;
private IUpstreamUrlPathTemplateVariableReplacer _upstreamUrlPathReplacer;
public UpstreamUrlPathTemplateVariableReplacerTests()
{
_upstreamUrlPathReplacer = new UpstreamUrlPathTemplateVariableReplacer();
}
[Fact]
public void can_replace_no_template_variables()
{
GivenThereIsAnUpstreamUrlPath("");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, new List<TemplateVariableNameAndValue>(), ""));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("");
}
[Fact]
public void can_replace_url_no_slash()
{
GivenThereIsAnUpstreamUrlPath("api");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, new List<TemplateVariableNameAndValue>(), "api"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("api");
}
[Fact]
public void can_replace_url_one_slash()
{
GivenThereIsAnUpstreamUrlPath("api/");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, new List<TemplateVariableNameAndValue>(), "api/"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("api/");
}
[Fact]
public void can_replace_url_multiple_slash()
{
GivenThereIsAnUpstreamUrlPath("api/product/products/");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, new List<TemplateVariableNameAndValue>(), "api/product/products/"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("api/product/products/");
}
[Fact]
public void can_replace_url_one_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1")
};
GivenThereIsAnUpstreamUrlPath("productservice/products/{productId}/");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, templateVariables, "api/products/{productId}/"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("productservice/products/1/");
}
[Fact]
public void can_replace_url_one_template_variable_with_path_after()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1")
};
GivenThereIsAnUpstreamUrlPath("productservice/products/{productId}/variants");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, templateVariables, "api/products/{productId}/"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("productservice/products/1/variants");
}
[Fact]
public void can_replace_url_two_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{variantId}", "12")
};
GivenThereIsAnUpstreamUrlPath("productservice/products/{productId}/variants/{variantId}");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, templateVariables, "api/products/{productId}/{variantId}"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("productservice/products/1/variants/12");
}
[Fact]
public void can_replace_url_three_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{variantId}", "12"),
new TemplateVariableNameAndValue("{categoryId}", "34")
};
GivenThereIsAnUpstreamUrlPath("productservice/category/{categoryId}/products/{productId}/variants/{variantId}");
GivenThereIsAUrlPathMatch(new UrlPathMatch(true, templateVariables, "api/products/{categoryId}/{productId}/{variantId}"));
WhenIReplaceTheTemplateVariables();
ThenTheUpstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12");
}
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
{
_upstreamUrlPath = upstreamUrlPath;
}
private void GivenThereIsAUrlPathMatch(UrlPathMatch urlPathMatch)
{
_urlPathMatch = urlPathMatch;
}
private void WhenIReplaceTheTemplateVariables()
{
_result = _upstreamUrlPathReplacer.ReplaceTemplateVariable(_upstreamUrlPath, _urlPathMatch);
}
private void ThenTheUpstreamUrlPathIsReturned(string expected)
{
_result.ShouldBe(expected);
}
}
}

View File

@ -18,6 +18,50 @@ namespace Ocelot.UnitTests
_urlMapper = new UrlPathToUrlPathTemplateMatcher();
}
[Fact]
public void can_match_down_stream_url()
{
GivenIHaveADownstreamPath("");
GivenIHaveAnDownstreamPathTemplate("");
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(new List<TemplateVariableNameAndValue>());
ThenTheUrlPathTemplateIs("");
}
[Fact]
public void can_match_down_stream_url_with_no_slash()
{
GivenIHaveADownstreamPath("api");
GivenIHaveAnDownstreamPathTemplate("api");
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(new List<TemplateVariableNameAndValue>());
ThenTheUrlPathTemplateIs("api");
}
[Fact]
public void can_match_down_stream_url_with_one_slash()
{
GivenIHaveADownstreamPath("api/");
GivenIHaveAnDownstreamPathTemplate("api/");
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(new List<TemplateVariableNameAndValue>());
ThenTheUrlPathTemplateIs("api/");
}
[Fact]
public void can_match_down_stream_url_with_downstream_template()
{
GivenIHaveADownstreamPath("api/product/products/");
GivenIHaveAnDownstreamPathTemplate("api/product/products/");
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(new List<TemplateVariableNameAndValue>());
ThenTheUrlPathTemplateIs("api/product/products/");
}
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_one_query_string_parameter()
{
@ -34,7 +78,7 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1")
new TemplateVariableNameAndValue("{productId}", "1")
};
GivenIHaveADownstreamPath("api/product/products/1/variants/?soldout=false");
@ -50,7 +94,7 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1")
new TemplateVariableNameAndValue("{productId}", "1")
};
GivenIHaveADownstreamPath("api/product/products/1");
@ -67,8 +111,8 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1"),
new TemplateVariableNameAndValue("{categoryid}", "2")
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
};
GivenIHaveADownstreamPath("api/product/products/1/2");
@ -85,8 +129,8 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1"),
new TemplateVariableNameAndValue("{categoryid}", "2")
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
};
GivenIHaveADownstreamPath("api/product/products/1/categories/2");
@ -103,9 +147,9 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1"),
new TemplateVariableNameAndValue("{categoryid}", "2"),
new TemplateVariableNameAndValue("{variantid}", "123")
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2"),
new TemplateVariableNameAndValue("{variantId}", "123")
};
GivenIHaveADownstreamPath("api/product/products/1/categories/2/variant/123");
@ -122,8 +166,8 @@ namespace Ocelot.UnitTests
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
{
new TemplateVariableNameAndValue("{productid}", "1"),
new TemplateVariableNameAndValue("{categoryid}", "2")
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
};
GivenIHaveADownstreamPath("api/product/products/1/categories/2/variant/");
@ -147,7 +191,7 @@ namespace Ocelot.UnitTests
private void ThenTheUrlPathTemplateIs(string expectedUrlPathTemplate)
{
_result.UrlPathTemplate.ShouldBe(expectedUrlPathTemplate);
_result.DownstreamUrlPathTemplate.ShouldBe(expectedUrlPathTemplate);
}
private void GivenIHaveADownstreamPath(string downstreamPath)
{