mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:48:14 +08:00
regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment
This commit is contained in:
156
test/Ocelot.UnitTests/UrlMatcher/RegExUrlMatcherTests.cs
Normal file
156
test/Ocelot.UnitTests/UrlMatcher/RegExUrlMatcherTests.cs
Normal file
@ -0,0 +1,156 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcherTests
|
||||
{
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private Response<UrlMatch> _result;
|
||||
|
||||
public RegExUrlMatcherTests()
|
||||
{
|
||||
_urlMatcher = new RegExUrlMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_find_match_when_template_smaller_than_valid_path()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/products/2354325435624623464235"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("/api/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_match()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/values"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/.*/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/.*/categories/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/.*/categories/.*/variant/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
}
|
||||
|
||||
private void GivenIHaveAnUpstreamUrlTemplatePattern(string downstreamUrlTemplate)
|
||||
{
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIMatchThePaths()
|
||||
{
|
||||
_result = _urlMatcher.Match(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsTrue()
|
||||
{
|
||||
_result.Data.Match.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheResultIsFalse()
|
||||
{
|
||||
_result.Data.Match.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
@ -9,33 +10,14 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
public class UrlPathToUrlTemplateMatcherTests
|
||||
{
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||
private readonly ITemplateVariableNameAndValueFinder _finder;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private UrlMatch _result;
|
||||
private Response<List<TemplateVariableNameAndValue>> _result;
|
||||
|
||||
public UrlPathToUrlTemplateMatcherTests()
|
||||
{
|
||||
_urlMatcher = new UrlPathToUrlTemplateMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_find_match_when_template_smaller_than_valid_path()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/products/2354325435624623464235"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/api/products/{productId}"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_match()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/values"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
_finder = new TemplateVariableNameAndValueFinder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -43,10 +25,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs(""))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -55,10 +35,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -67,10 +45,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -79,10 +55,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -96,10 +70,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/{productId}"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -114,10 +86,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/{categoryId}"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/{productId}/{categoryId}"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -132,10 +102,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/{productId}/categories/{categoryId}"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -151,10 +119,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/{variantId}"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/{productId}/categories/{categoryId}/variant/{variantId}"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -169,10 +135,8 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.And(x => x.ThenTheDownstreamUrlTemplateIs("api/product/products/{productId}/categories/{categoryId}/variant/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -180,16 +144,12 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
foreach (var expectedResult in expectedResults)
|
||||
{
|
||||
var result = _result.TemplateVariableNameAndValues
|
||||
var result = _result.Data
|
||||
.First(t => t.TemplateVariableName == expectedResult.TemplateVariableName);
|
||||
result.TemplateVariableValue.ShouldBe(expectedResult.TemplateVariableValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamUrlTemplateIs(string expectedDownstreamUrlTemplate)
|
||||
{
|
||||
_result.DownstreamUrlTemplate.ShouldBe(expectedDownstreamUrlTemplate);
|
||||
}
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
@ -200,19 +160,9 @@ namespace Ocelot.UnitTests.UrlMatcher
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIMatchThePaths()
|
||||
private void WhenIFindTheUrlVariableNamesAndValues()
|
||||
{
|
||||
_result = _urlMatcher.Match(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsTrue()
|
||||
{
|
||||
_result.Match.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheResultIsFalse()
|
||||
{
|
||||
_result.Match.ShouldBeFalse();
|
||||
_result = _finder.Find(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user