mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 12:10:50 +08:00 
			
		
		
		
	hacked together load balancing reroutes in fileconfig (#211)
* hacked together load balancing reroutes in fileconfig * some renaming and refactoring * more renames * hacked away the old config json * test for issue 213 * renamed key * dont share ports * oops * updated docs * mvoed docs around * port being used
This commit is contained in:
		@@ -1,218 +1,218 @@
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.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_not_match_slash_becaue_we_need_to_match_something_after_it()
 | 
			
		||||
        {
 | 
			
		||||
            const string RegExForwardSlashAndOnePlaceHolder = "^/[0-9a-zA-Z].*";
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern(RegExForwardSlashAndOnePlaceHolder))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_match_forward_slash_only_regex()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/working/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_match_issue_134()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/api/vacancy/1/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)/vacancy/.*/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_match_forward_slash_only_regex()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsTrue())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [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();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_ignore_case_sensitivity()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
 | 
			
		||||
               .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)api/product/products/.*/categories/.*/variant/$"))
 | 
			
		||||
               .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
               .Then(x => x.ThenTheResultIsTrue())
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_respect_case_sensitivity()
 | 
			
		||||
        {
 | 
			
		||||
            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.ThenTheResultIsFalse())
 | 
			
		||||
              .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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.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_not_match_slash_becaue_we_need_to_match_something_after_it()
 | 
			
		||||
        {
 | 
			
		||||
            const string RegExForwardSlashAndOnePlaceHolder = "^/[0-9a-zA-Z].*";
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern(RegExForwardSlashAndOnePlaceHolder))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_match_forward_slash_only_regex()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/working/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_match_issue_134()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/api/vacancy/1/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)/vacancy/.*/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsFalse())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_match_forward_slash_only_regex()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
              .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
 | 
			
		||||
              .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
              .And(x => x.ThenTheResultIsTrue())
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [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();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_ignore_case_sensitivity()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
 | 
			
		||||
               .And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)api/product/products/.*/categories/.*/variant/$"))
 | 
			
		||||
               .When(x => x.WhenIMatchThePaths())
 | 
			
		||||
               .Then(x => x.ThenTheResultIsTrue())
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_respect_case_sensitivity()
 | 
			
		||||
        {
 | 
			
		||||
            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.ThenTheResultIsFalse())
 | 
			
		||||
              .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,267 +1,267 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
{
 | 
			
		||||
    public class UrlPathPlaceholderNameAndValueFinderTests 
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IPlaceholderNameAndValueFinder _finder;
 | 
			
		||||
        private string _downstreamUrlPath;
 | 
			
		||||
        private string _downstreamPathTemplate;
 | 
			
		||||
        private Response<List<PlaceholderNameAndValue>> _result;
 | 
			
		||||
 | 
			
		||||
        public UrlPathPlaceholderNameAndValueFinderTests()
 | 
			
		||||
        {
 | 
			
		||||
            _finder = new UrlPathPlaceholderNameAndValueFinder();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath(""))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_nothing_then_placeholder_no_value_is_blank()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath(""))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_nothing_then_placeholder_value_is_test()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "test")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/test"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash_then_placeholder_no_value_is_blank()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash_then_placeholder_then_another_value()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "1")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/1/products"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}/products"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_find_anything()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/products"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/products/"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_no_slash()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_one_slash()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
 | 
			
		||||
              .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
 | 
			
		||||
              .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
              .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
 | 
			
		||||
               .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}"))
 | 
			
		||||
               .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
               .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/{categoryId}"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2"),
 | 
			
		||||
                new PlaceholderNameAndValue("{variantId}", "123")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            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.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            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.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue>
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{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<PlaceholderNameAndValue> expectedResults)
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var expectedResult in expectedResults)
 | 
			
		||||
            {
 | 
			
		||||
                var result = _result.Data.First(t => t.Name == expectedResult.Name);
 | 
			
		||||
                result.Value.ShouldBe(expectedResult.Value);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenIHaveAUpstreamPath(string downstreamPath)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamUrlPath = downstreamPath;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenIHaveAnUpstreamUrlTemplate(string downstreamUrlTemplate)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamPathTemplate = downstreamUrlTemplate;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIFindTheUrlVariableNamesAndValues()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _finder.Find(_downstreamUrlPath, _downstreamPathTemplate);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
{
 | 
			
		||||
    public class UrlPathPlaceholderNameAndValueFinderTests 
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IPlaceholderNameAndValueFinder _finder;
 | 
			
		||||
        private string _downstreamUrlPath;
 | 
			
		||||
        private string _downstreamPathTemplate;
 | 
			
		||||
        private Response<List<PlaceholderNameAndValue>> _result;
 | 
			
		||||
 | 
			
		||||
        public UrlPathPlaceholderNameAndValueFinderTests()
 | 
			
		||||
        {
 | 
			
		||||
            _finder = new UrlPathPlaceholderNameAndValueFinder();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath(""))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_nothing_then_placeholder_no_value_is_blank()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath(""))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_nothing_then_placeholder_value_is_test()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "test")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/test"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash_then_placeholder_no_value_is_blank()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_forward_slash_then_placeholder_then_another_value()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{url}", "1")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/1/products"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}/products"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_find_anything()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/products"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/products/"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_no_slash()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_one_slash()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
 | 
			
		||||
              .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
 | 
			
		||||
              .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
              .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
              .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
 | 
			
		||||
               .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}"))
 | 
			
		||||
               .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
               .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/{categoryId}"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2"),
 | 
			
		||||
                new PlaceholderNameAndValue("{variantId}", "123")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            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.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            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.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue>
 | 
			
		||||
            {
 | 
			
		||||
                new PlaceholderNameAndValue("{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<PlaceholderNameAndValue> expectedResults)
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var expectedResult in expectedResults)
 | 
			
		||||
            {
 | 
			
		||||
                var result = _result.Data.First(t => t.Name == expectedResult.Name);
 | 
			
		||||
                result.Value.ShouldBe(expectedResult.Value);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenIHaveAUpstreamPath(string downstreamPath)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamUrlPath = downstreamPath;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenIHaveAnUpstreamUrlTemplate(string downstreamUrlTemplate)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamPathTemplate = downstreamUrlTemplate;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIFindTheUrlVariableNamesAndValues()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _finder.Find(_downstreamUrlPath, _downstreamPathTemplate);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
} 
 | 
			
		||||
		Reference in New Issue
	
	Block a user