예제 #1
0
        public async Task MatchAsync_MatchesCatchAll_UsesDefaultValue(
            string template,
            string requestPath)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint(template,
                                             new object(),                        Test_Delegate, "Test"),
                },
            };

            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            var context = CreateMatcherContext(requestPath);

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.Same(dataSource.Endpoints[0], context.Endpoint);
            Assert.Equal("default", context.Values["path"]);
        }
예제 #2
0
        public async Task MatchAsync_WithOptionalConstraint(
            string template,
            string request,
            bool expectedResult)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint(template,
                                             new object(),                        Test_Delegate, "Test"),
                },
            };

            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());
            var context = CreateMatcherContext(request);

            // Act
            await matcher.MatchAsync(context);

            // Assert
            if (expectedResult)
            {
                Assert.NotNull(context.Endpoint);
            }
            else
            {
                Assert.Null(context.Endpoint);
            }
        }
예제 #3
0
        public async Task MatchAsync_MatchesConstrainedEndpointsWithDefaults(string url, object[] values)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("{parameter1:int=1}/{parameter2:int=2}/{parameter3:int=3}/{parameter4:int=4}",
                                             new { parameter1 = 1,                                                                          parameter2= 2, parameter3 = 3, parameter4 = 4 }, Test_Delegate, "Test"),
                },
            };

            var valueKeys      = new[] { "parameter1", "parameter2", "parameter3", "parameter4" };
            var expectedValues = new DispatcherValueCollection();

            for (int i = 0; i < valueKeys.Length; i++)
            {
                expectedValues.Add(valueKeys[i], values[i]);
            }

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            foreach (var entry in expectedValues)
            {
                var data = Assert.Single(context.Values, v => v.Key == entry.Key);
                Assert.Equal(entry.Value, data.Value);
            }
        }
예제 #4
0
        public async Task MatchAsync_MultipleOptionalParameters_WithIncrementalOptionalValues(string url)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("{controller?}/{action?}/{id?}", new {}, Test_Delegate, "Test"),
                },
            };

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.NotNull(context.Endpoint);
        }
예제 #5
0
        public async Task MatchAsync_EnsuresStableOrdering(string firstTemplate, string secondTemplate)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint(firstTemplate,  new { }, Test_Delegate, "Test1"),
                    new RoutePatternEndpoint(secondTemplate, new { }, Test_Delegate, "Test2"),
                },
            };

            var context = CreateMatcherContext("/template/5");
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.Same(dataSource.Endpoints[0], context.Endpoint);
        }
예제 #6
0
        public async Task MatchAsync_WithOptionalCompositeParameter_Valid(
            string template,
            string request,
            string p1,
            string p2,
            string p3)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint(template,
                                             new object(),                        Test_Delegate, "Test"),
                },
            };

            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());
            var context = CreateMatcherContext(request);

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.NotNull(context.Endpoint);
            if (p1 != null)
            {
                Assert.Equal(p1, context.Values["p1"]);
            }
            if (p2 != null)
            {
                Assert.Equal(p2, context.Values["p2"]);
            }
            if (p3 != null)
            {
                Assert.Equal(p3, context.Values["p3"]);
            }
        }
예제 #7
0
        public async Task MatchAsync_CatchAllParameters_WithEmptyValues(string url)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("{controller}/{action}/{*id}",
                                             new object(),                        Test_Delegate, "Test"),
                },
            };

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.Null(context.Endpoint);
        }
예제 #8
0
        public async Task MatchAsync_MatchRoutesWithMultipleIntermediateDefaultOrOptionalValues_WhenAllIntermediateValuesAreProvided(string template, string url)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint(template,
                                             new { b = 3 },                        Test_Delegate, "Test"),
                },
            };

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.NotNull(context.Endpoint);
        }
예제 #9
0
        public void MatchAsync_DoesNotMatchShorterUrl()
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("Literal1/Literal2/Literal3",
                                             new object(),                        Test_Delegate, "Test"),
                },
            };

            var routes = new[] {
                "Literal1/Literal2/Literal3",
            };

            var context = CreateMatcherContext("/Literal1");
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Assert
            Assert.Null(context.Endpoint);
        }
예제 #10
0
        public async Task MatchAsync_DoesNotMatchEndpointsWithIntermediateDefaultValues()
        {
            // Arrange
            var url        = "/a/b";
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("a/b/{parameter3=3}/d",
                                             new { parameter3 = 3 },                        Test_Delegate, "Test"),
                },
            };

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.Null(context.Endpoint);
        }
예제 #11
0
        public async Task MatchAsync_MatchesEndpointWithTheRightLength(string url, int index)
        {
            // Arrange
            var dataSource = new DefaultDispatcherDataSource()
            {
                Endpoints =
                {
                    new RoutePatternEndpoint("",                                                                                    Test_Delegate),
                    new RoutePatternEndpoint("Literal1",                                                                            Test_Delegate),
                    new RoutePatternEndpoint("Literal1/Literal2",                                                                   Test_Delegate),
                    new RoutePatternEndpoint("Literal1/Literal2/Literal3",                                                          Test_Delegate),
                    new RoutePatternEndpoint("Literal1/Literal2/Literal3/{*constrainedCatchAll:int}",                               Test_Delegate),
                    new RoutePatternEndpoint("Literal1/Literal2/Literal3/{*catchAll}",                                              Test_Delegate),
                    new RoutePatternEndpoint("{constrained1:int}",                                                                  Test_Delegate),
                    new RoutePatternEndpoint("{constrained1:int}/{constrained2:int}",                                               Test_Delegate),
                    new RoutePatternEndpoint("{constrained1:int}/{constrained2:int}/{constrained3:int}",                            Test_Delegate),
                    new RoutePatternEndpoint("{constrained1:int}/{constrained2:int}/{constrained3:int}/{*constrainedCatchAll:int}", Test_Delegate),
                    new RoutePatternEndpoint("{constrained1:int}/{constrained2:int}/{constrained3:int}/{*catchAll}",                Test_Delegate),
                    new RoutePatternEndpoint("{parameter1}",                                                                        Test_Delegate),
                    new RoutePatternEndpoint("{parameter1}/{parameter2}",                                                           Test_Delegate),
                    new RoutePatternEndpoint("{parameter1}/{parameter2}/{parameter3}",                                              Test_Delegate),
                    new RoutePatternEndpoint("{parameter1}/{parameter2}/{parameter3}/{*constrainedCatchAll:int}",                   Test_Delegate),
                    new RoutePatternEndpoint("{parameter1}/{parameter2}/{parameter3}/{*catchAll}",                                  Test_Delegate),
                },
            };

            var context = CreateMatcherContext(url);
            var factory = new TreeMatcherFactory();
            var matcher = factory.CreateMatcher(dataSource, new List <EndpointSelector>());

            // Act
            await matcher.MatchAsync(context);

            // Assert
            Assert.Same(dataSource.Endpoints[index], context.Endpoint);
        }