Ejemplo n.º 1
0
        private static void RunTest(
            string template,
            string path,
            DispatcherValueCollection defaults,
            IDictionary <string, object> expected)
        {
            // Arrange
            var matcher = new RoutePatternMatcher(
                RoutePatternParser.Parse(template),
                defaults ?? new DispatcherValueCollection());

            var values = new DispatcherValueCollection();

            // Act
            var match = matcher.TryMatch(new PathString(path), values);

            // Assert
            if (expected == null)
            {
                Assert.False(match);
            }
            else
            {
                Assert.True(match);
                Assert.Equal(expected.Count, values.Count);
                foreach (string key in values.Keys)
                {
                    Assert.Equal(expected[key], values[key]);
                }
            }
        }
Ejemplo n.º 2
0
        public HttpEndpoint(
            string pattern,
            object values,
            string httpMethod,
            Func <RequestDelegate, RequestDelegate> delegateFactory,
            string displayName,
            params object[] metadata)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            if (delegateFactory == null)
            {
                throw new ArgumentNullException(nameof(delegateFactory));
            }

            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            Pattern        = pattern;
            Values         = new DispatcherValueCollection(values);
            HttpMethod     = httpMethod;
            HandlerFactory = delegateFactory;
            DisplayName    = displayName;
            Metadata       = new MetadataCollection(metadata);
        }
Ejemplo n.º 3
0
        public EndpointSelectorContext(HttpContext httpContext, DispatcherValueCollection values, IList <Endpoint> endpoints, IList <EndpointSelector> selectors)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (endpoints == null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }

            if (selectors == null)
            {
                throw new ArgumentNullException(nameof(selectors));
            }

            HttpContext = httpContext;
            Values      = values;
            Endpoints   = endpoints;
            Selectors   = selectors;
        }
Ejemplo n.º 4
0
        public void TryMatch_OptionalParameter_FollowedByPeriod_3Parameters_Valid(
            string template,
            string path,
            string p1,
            string p2,
            string p3)
        {
            // Arrange
            var matcher = CreateMatcher(template);

            var values = new DispatcherValueCollection();

            // Act
            var match = matcher.TryMatch(path, values);

            // Assert
            Assert.True(match);
            Assert.Equal(p1, values["p1"]);

            if (p2 != null)
            {
                Assert.Equal(p2, values["p2"]);
            }

            if (p3 != null)
            {
                Assert.Equal(p3, values["p3"]);
            }
        }
Ejemplo n.º 5
0
        internal RoutePatternBinder(
            UrlEncoder urlEncoder,
            ObjectPool <UriBuildingContext> pool,
            RoutePattern pattern,
            DispatcherValueCollection defaults)
        {
            if (urlEncoder == null)
            {
                throw new ArgumentNullException(nameof(urlEncoder));
            }

            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            _urlEncoder = urlEncoder;
            _pool       = pool;
            _pattern    = pattern;
            _defaults   = defaults;

            // Any default that doesn't have a corresponding parameter is a 'filter' and if a value
            // is provided for that 'filter' it must match the value in defaults.
            _filters = new DispatcherValueCollection(_defaults);
            foreach (var parameter in _pattern.Parameters)
            {
                _filters.Remove(parameter.Name);
            }
        }
Ejemplo n.º 6
0
        public void Binding_WithEmptyAndNull_DefaultValues(
            string pattern,
            DispatcherValueCollection defaults,
            DispatcherValueCollection values,
            string expected)
        {
            // Arrange
            var binder = BinderFactory.Create(pattern, defaults);

            // Act & Assert
            (var acceptedValues, var combinedValues) = binder.GetValues(ambientValues: null, values: values);
            if (acceptedValues == null)
            {
                if (expected == null)
                {
                    return;
                }
                else
                {
                    Assert.NotNull(acceptedValues);
                }
            }

            var result = binder.BindValues(acceptedValues);

            if (expected == null)
            {
                Assert.Null(result);
            }
            else
            {
                Assert.NotNull(result);
                Assert.Equal(expected, result);
            }
        }
        public void CreateFromObject_CopiesPropertiesFromRegularType_IncludesInherited()
        {
            // Arrange
            var obj = new Derived()
            {
                TotallySweetProperty = true, DerivedProperty = false
            };

            // Act
            var dict = new DispatcherValueCollection(obj);

            // Assert
            Assert.IsType <DispatcherValueCollection.PropertyStorage>(dict._storage);
            Assert.Collection(
                dict.OrderBy(kvp => kvp.Key),
                kvp =>
            {
                Assert.Equal("DerivedProperty", kvp.Key);
                var value = Assert.IsType <bool>(kvp.Value);
                Assert.False(value);
            },
                kvp =>
            {
                Assert.Equal("TotallySweetProperty", kvp.Key);
                var value = Assert.IsType <bool>(kvp.Value);
                Assert.True(value);
            });
        }
        public void CreateFromObject_CopiesPropertiesFromRegularType()
        {
            // Arrange
            var obj = new RegularType()
            {
                CoolnessFactor = 73
            };

            // Act
            var dict = new DispatcherValueCollection(obj);

            // Assert
            Assert.IsType <DispatcherValueCollection.PropertyStorage>(dict._storage);
            Assert.Collection(
                dict.OrderBy(kvp => kvp.Key),
                kvp =>
            {
                Assert.Equal("CoolnessFactor", kvp.Key);
                Assert.Equal(73, kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("IsAwesome", kvp.Key);
                var value = Assert.IsType <bool>(kvp.Value);
                Assert.False(value);
            });
        }
        public void ListStorage_RemoveAt_RearrangesInnerArray()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            dict.Add("key", "value");
            dict.Add("key2", "value2");
            dict.Add("key3", "value3");

            // Assert 1
            var storage = Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);

            Assert.Equal(3, storage.Count);

            // Act
            dict.Remove("key2");

            // Assert 2
            Assert.Equal(2, storage.Count);
            Assert.Equal("key", storage[0].Key);
            Assert.Equal("value", storage[0].Value);
            Assert.Equal("key3", storage[1].Key);
            Assert.Equal("value3", storage[1].Value);

            Assert.Throws <ArgumentOutOfRangeException>(() => storage[2]);
        }
Ejemplo n.º 10
0
        public RoutePatternEndpoint(
            string pattern,
            object values,
            string httpMethod,
            RequestDelegate requestDelegate,
            string displayName,
            params object[] metadata)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            if (requestDelegate == null)
            {
                throw new ArgumentNullException(nameof(requestDelegate));
            }

            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            Pattern        = pattern;
            Values         = new DispatcherValueCollection(values);
            HttpMethod     = httpMethod;
            HandlerFactory = (next) => requestDelegate;
            DisplayName    = displayName;
            Metadata       = new MetadataCollection(metadata);
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
        // Step 2: If the route is a match generate the appropriate URI
        public string BindValues(DispatcherValueCollection acceptedValues)
        {
            var context = _pool.Get();
            var result  = BindValues(context, acceptedValues);

            _pool.Return(context);
            return(result);
        }
Ejemplo n.º 13
0
        private static void RouteFormatHelper(string routeUrl, string requestUrl)
        {
            var defaults = new DispatcherValueCollection(new { route = "matched" });
            var r        = CreateRoute(routeUrl, defaults, null);

            GetRouteDataHelper(r, requestUrl, defaults);
            GetVirtualPathHelper(r, new DispatcherValueCollection(), null, Uri.EscapeUriString(requestUrl));
        }
Ejemplo n.º 14
0
        private void RunTest(
            string pattern,
            DispatcherValueCollection defaults,
            DispatcherValueCollection ambientValues,
            DispatcherValueCollection values,
            string expected,
            UrlEncoder encoder = null)
        {
            // Arrange
            var binderFactory = encoder == null ? BinderFactory : new RoutePatternBinderFactory(encoder, new DefaultObjectPoolProvider());
            var binder        = binderFactory.Create(pattern, defaults ?? new DispatcherValueCollection());

            // Act & Assert
            (var acceptedValues, var combinedValues) = binder.GetValues(ambientValues, values);
            if (acceptedValues == null)
            {
                if (expected == null)
                {
                    return;
                }
                else
                {
                    Assert.NotNull(acceptedValues);
                }
            }

            var result = binder.BindValues(acceptedValues);

            if (expected == null)
            {
                Assert.Null(result);
            }
            else
            {
                Assert.NotNull(result);

                // We want to chop off the query string and compare that using an unordered comparison
                var expectedParts = new PathAndQuery(expected);
                var actualParts   = new PathAndQuery(result);

                Assert.Equal(expectedParts.Path, actualParts.Path);

                if (expectedParts.Parameters == null)
                {
                    Assert.Null(actualParts.Parameters);
                }
                else
                {
                    Assert.Equal(expectedParts.Parameters.Count, actualParts.Parameters.Count);

                    foreach (var kvp in expectedParts.Parameters)
                    {
                        Assert.True(actualParts.Parameters.TryGetValue(kvp.Key, out var value));
                        Assert.Equal(kvp.Value, value);
                    }
                }
            }
        }
        public void Comparer_IsOrdinalIgnoreCase()
        {
            // Arrange
            // Act
            var dict = new DispatcherValueCollection();

            // Assert
            Assert.Same(StringComparer.OrdinalIgnoreCase, dict.Comparer);
        }
        public static RouteValueDictionary AsRouteValueDictionary(this DispatcherValueCollection values)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            return(values as RouteValueDictionary ?? new RouteValueDictionary(values));
        }
Ejemplo n.º 17
0
        public override string GetUrl(DispatcherValueCollection values)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            return(GetUrl(null, values));
        }
        public void DefaultCtor_UsesEmptyStorage()
        {
            // Arrange
            // Act
            var dict = new DispatcherValueCollection();

            // Assert
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.EmptyStorage>(dict._storage);
        }
        public void CreateFromNull_UsesEmptyStorage()
        {
            // Arrange
            // Act
            var dict = new DispatcherValueCollection(null);

            // Assert
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.EmptyStorage>(dict._storage);
        }
        public void IsReadOnly_False()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            // Act
            var result = ((ICollection <KeyValuePair <string, object> >)dict).IsReadOnly;

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 21
0
        public void RegexConstraintConstructedWithRegex_SimpleFailedMatch()
        {
            // Arrange
            var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$"));
            var values     = new DispatcherValueCollection(new { controller = "Abc" });

            // Act
            var match = TestConstraint(constraint, values, "controller");

            // Assert
            Assert.False(match);
        }
Ejemplo n.º 22
0
            public Enumerator(DispatcherValueCollection collection)
            {
                if (collection == null)
                {
                    throw new ArgumentNullException();
                }

                _storage = collection._storage;

                Current = default(KeyValuePair <string, object>);
                _index  = -1;
            }
        public void Add_EmptyStorage()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            // Act
            dict.Add("key", "value");

            // Assert
            Assert.Collection(dict, kvp => { Assert.Equal("key", kvp.Key); Assert.Equal("value", kvp.Value); });
            Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);
        }
        public void Clear_EmptyStorage()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            // Act
            dict.Clear();

            // Assert
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.EmptyStorage>(dict._storage);
        }
        public void Clear_PropertyStorage_AlreadyEmpty()
        {
            // Arrange
            var dict = new DispatcherValueCollection(new { });

            // Act
            dict.Clear();

            // Assert
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.PropertyStorage>(dict._storage);
        }
Ejemplo n.º 26
0
        public void TryMatch_RouteWithComplexSegment_Success(string template, string path)
        {
            var matcher = CreateMatcher(template);

            var values = new DispatcherValueCollection();

            // Act
            var match = matcher.TryMatch(path, values);

            // Assert
            Assert.True(match);
        }
        public void Clear_PropertyStorage()
        {
            // Arrange
            var dict = new DispatcherValueCollection(new { key = "value" });

            // Act
            dict.Clear();

            // Assert
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);
        }
Ejemplo n.º 28
0
        private static bool TestConstraint(IDispatcherValueConstraint constraint, DispatcherValueCollection values, string routeKey)
        {
            var httpContext       = new DefaultHttpContext();
            var constraintPurpose = ConstraintPurpose.IncomingRequest;

            var dispatcherValueConstraintContext = new DispatcherValueConstraintContext(httpContext, values, constraintPurpose)
            {
                Key = routeKey
            };

            return(constraint.Match(dispatcherValueConstraintContext));
        }
Ejemplo n.º 29
0
        public void RegexConstraint_TakesRegexAsInput_SimpleMatch()
        {
            // Arrange
            var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$"));
            var values     = new DispatcherValueCollection(new { controller = "abc" });

            // Act
            var match = TestConstraint(constraint, values, "controller");

            // Assert
            Assert.True(match);
        }
Ejemplo n.º 30
0
        public void RegexConstraintFailsIfKeyIsNotFoundInRouteValues()
        {
            // Arrange
            var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$"));
            var values     = new DispatcherValueCollection(new { action = "abc" });

            // Act
            var match = TestConstraint(constraint, values, "controller");

            // Assert
            Assert.False(match);
        }