mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 12:10:50 +08:00 
			
		
		
		
	Changed routing to support a catch all style (#187)
* Changed routing to support a catch all style * refactoring placeholder tuff * implemented simple priority in the routing
This commit is contained in:
		@@ -18,6 +18,18 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            _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()
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,14 +8,14 @@ using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
{
 | 
			
		||||
    public class UrlPathToUrlTemplateMatcherTests 
 | 
			
		||||
    public class UrlPathPlaceholderNameAndValueFinderTests 
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IUrlPathPlaceholderNameAndValueFinder _finder;
 | 
			
		||||
        private readonly IPlaceholderNameAndValueFinder _finder;
 | 
			
		||||
        private string _downstreamUrlPath;
 | 
			
		||||
        private string _downstreamPathTemplate;
 | 
			
		||||
        private Response<List<UrlPathPlaceholderNameAndValue>> _result;
 | 
			
		||||
        private Response<List<PlaceholderNameAndValue>> _result;
 | 
			
		||||
 | 
			
		||||
        public UrlPathToUrlTemplateMatcherTests()
 | 
			
		||||
        public UrlPathPlaceholderNameAndValueFinderTests()
 | 
			
		||||
        {
 | 
			
		||||
            _finder = new UrlPathPlaceholderNameAndValueFinder();
 | 
			
		||||
        }
 | 
			
		||||
@@ -26,7 +26,82 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath(""))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
 | 
			
		||||
                .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();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -36,7 +111,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("/products"))
 | 
			
		||||
                .And(x => x.GivenIHaveAnUpstreamUrlTemplate("/products/"))
 | 
			
		||||
                .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
 | 
			
		||||
                .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -46,7 +121,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -56,7 +131,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
 | 
			
		||||
                 .Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
 | 
			
		||||
                 .When(x => x.WhenIFindTheUrlVariableNamesAndValues())
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
 | 
			
		||||
                 .And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -66,16 +141,16 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
            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<UrlPathPlaceholderNameAndValue>()))
 | 
			
		||||
              .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<UrlPathPlaceholderNameAndValue> 
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{productId}", "1")
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
 | 
			
		||||
@@ -88,10 +163,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<UrlPathPlaceholderNameAndValue> 
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
 | 
			
		||||
@@ -104,10 +179,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<UrlPathPlaceholderNameAndValue> 
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
 | 
			
		||||
@@ -120,11 +195,11 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<UrlPathPlaceholderNameAndValue> 
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{categoryId}", "2"),
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{variantId}", "123")
 | 
			
		||||
                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"))
 | 
			
		||||
@@ -137,10 +212,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<UrlPathPlaceholderNameAndValue> 
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue> 
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
                new PlaceholderNameAndValue("{productId}", "1"),
 | 
			
		||||
                new PlaceholderNameAndValue("{categoryId}", "2")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
 | 
			
		||||
@@ -153,9 +228,9 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
 | 
			
		||||
        {
 | 
			
		||||
            var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
 | 
			
		||||
            var expectedTemplates = new List<PlaceholderNameAndValue>
 | 
			
		||||
            {
 | 
			
		||||
                new UrlPathPlaceholderNameAndValue("{finalUrlPath}", "product/products/categories/"),
 | 
			
		||||
                new PlaceholderNameAndValue("{finalUrlPath}", "product/products/categories/"),
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/categories/"))
 | 
			
		||||
@@ -165,13 +240,12 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
 | 
			
		||||
                 .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheTemplatesVariablesAre(List<UrlPathPlaceholderNameAndValue> expectedResults)
 | 
			
		||||
        private void ThenTheTemplatesVariablesAre(List<PlaceholderNameAndValue> expectedResults)
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var expectedResult in expectedResults)
 | 
			
		||||
            {
 | 
			
		||||
                var result = _result.Data
 | 
			
		||||
                    .First(t => t.TemplateVariableName == expectedResult.TemplateVariableName);
 | 
			
		||||
                result.TemplateVariableValue.ShouldBe(expectedResult.TemplateVariableValue);
 | 
			
		||||
                var result = _result.Data.First(t => t.Name == expectedResult.Name);
 | 
			
		||||
                result.Value.ShouldBe(expectedResult.Value);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user