Example #1
0
        private static void RunTest(
            string template,
            string path,
            RouteValueDictionary defaults,
            IDictionary <string, object> expected)
        {
            // Arrange
            var matcher = new TemplateMatcher(
                TemplateParser.Parse(template),
                defaults ?? new RouteValueDictionary());

            // Act
            var match = matcher.Match(new PathString(path));

            // Assert
            if (expected == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.NotNull(match);
                Assert.Equal(expected.Count, match.Values.Count);
                foreach (string key in match.Keys)
                {
                    Assert.Equal(expected[key], match[key]);
                }
            }
        }
Example #2
0
        public void ShouldReturnFalseIfQueriesParametersNotEquals()
        {
            //Arrange
            var templateUri = new Uri("http://localhost/api/products?x={xValue}&p1=1&p2=2");
            var uri         = new Uri("http://localhost/api/products?x={xValue}&p3=1&p4=2");

            Assert.IsFalse(TemplateMatcher.Match(templateUri, uri));
        }
Example #3
0
        public void ShouldReturnFalseIfSchemesAreDifferents()
        {
            //Arrange
            var templateUri = new Uri("http://localhost/api/products");
            var uri         = new Uri("https://localhost/api/products");

            Assert.IsFalse(TemplateMatcher.Match(templateUri, uri));
        }
Example #4
0
        private bool RequestingSwaggerUi(HttpRequest request)
        {
            if (request.Method != "GET")
            {
                return(false);
            }

            var routeValues = _requestMatcher.Match(request.Path.ToUriComponent().Trim('/'));

            return(routeValues != null);
        }
Example #5
0
        private bool RequestingSwaggerUi(HttpRequest request)
        {
            if (request.Method != "GET")
            {
                return(false);
            }

            var routeValues = _requestMatcher.Match(request.Path);

            return(routeValues != null);
        }
Example #6
0
        /// <inheritdoc />
        public virtual Task RouteAsync(RouteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            EnsureMatcher();
            EnsureLoggers(context.HttpContext);

            var requestPath = context.HttpContext.Request.Path;
            var values      = _matcher.Match(requestPath);

            if (values == null)
            {
                // If we got back a null value set, that means the URI did not match
                return(TaskCache.CompletedTask);
            }

            // Perf: Avoid accessing dictionaries if you don't need to write to them, these dictionaries are all
            // created lazily.
            if (DataTokens.Count > 0)
            {
                MergeValues(context.RouteData.DataTokens, DataTokens);
            }

            if (values.Count > 0)
            {
                MergeValues(context.RouteData.Values, values);
            }

            if (!RouteConstraintMatcher.Match(
                    Constraints,
                    context.RouteData.Values,
                    context.HttpContext,
                    this,
                    RouteDirection.IncomingRequest,
                    _constraintLogger))
            {
                return(TaskCache.CompletedTask);
            }
            _logger.MatchedRoute(Name, ParsedTemplate.TemplateText);

            return(OnRouteMatched(context));
        }
Example #7
0
        private bool RequestingSwaggerDocs(HttpRequest request, out string apiVersion)
        {
            apiVersion = null;
            if (request.Method != "GET")
            {
                return(false);
            }

            var routeValues = _requestMatcher.Match(request.Path);

            if (routeValues == null || !routeValues.ContainsKey("apiVersion"))
            {
                return(false);
            }

            apiVersion = routeValues["apiVersion"].ToString();
            return(true);
        }
Example #8
0
        private static void RunTest(string template, string path, IDictionary <string, object> defaults, IDictionary <string, object> expected)
        {
            // Arrange
            var matcher = new TemplateMatcher(TemplateParser.Parse(template, _inlineConstraintResolver));

            // Act
            var match = matcher.Match(path, defaults);

            // Assert
            if (expected == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.NotNull(match);
                Assert.Equal(expected.Count, match.Values.Count);
                foreach (string key in match.Keys)
                {
                    Assert.Equal(expected[key], match[key]);
                }
            }
        }
Example #9
0
        private static void RunTest(
            string template,
            string path,
            IReadOnlyDictionary<string, object> defaults,
            IDictionary<string, object> expected)
        {
            // Arrange
            var matcher = new TemplateMatcher(TemplateParser.Parse(template), defaults);

            // Act
            var match = matcher.Match(path);

            // Assert
            if (expected == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.NotNull(match);
                Assert.Equal(expected.Count, match.Values.Count);
                foreach (string key in match.Keys)
                {
                    Assert.Equal(expected[key], match[key]);
                }
            }
        }
Example #10
0
 public void ShouldReturnFalseIfUriIsNull()
 {
     Assert.IsFalse(TemplateMatcher.Match(new Uri("http://localhost/api"), null));
     Assert.IsFalse(TemplateMatcher.Match(null, new Uri("http://localhost/api")));
 }
Example #11
0
 public void ShouldReturnFalseWhenUrisNotMatch(string template, string uri)
 {
     Assert.IsFalse(TemplateMatcher.Match(new Uri(template), new Uri(uri)));
 }
Example #12
0
 public void ShouldReturnTrueWhenUrisMatch(string template, string uri)
 {
     Assert.IsTrue(TemplateMatcher.Match(new Uri(template), new Uri(uri)));
 }