public void MatchUrlGeneration_DoesNotLogData()
        {
            // Arrange
            var sink   = new TestSink();
            var logger = new TestLogger(_name, sink, enabled: true);

            var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
            var constraints          = new Dictionary <string, IRouteConstraint>
            {
                { "a", new PassConstraint() },
                { "b", new FailConstraint() }
            };

            // Act
            RouteConstraintMatcher.Match(
                constraints: constraints,
                routeValues: routeValueDictionary,
                httpContext: new Mock <HttpContext>().Object,
                route: new Mock <IRouter>().Object,
                routeDirection: RouteDirection.UrlGeneration,
                logger: logger);

            // Assert
            // There are no BeginScopes called.
            Assert.Empty(sink.Scopes);

            // There are no WriteCores called.
            Assert.Empty(sink.Writes);
        }
Exemple #2
0
        public override Task RouteAsync(RouteContext context)
        {
            var host = context.HttpContext.Request.Host.Value;

            string foundHostname = GetHostname(host);

            if (foundHostname == null && Subdomain != null)
            {
                return(Task.CompletedTask);
            }

            if (Subdomain == null)
            {
                if (foundHostname != null)
                {
                    return(Task.CompletedTask);
                }

                return(base.RouteAsync(context));
            }

            var subdomain = host.Substring(0, host.IndexOf(GetHostname(host)) - 1);

            if (!IsParameterName(Subdomain) && subdomain.ToLower() != Subdomain.ToLower())
            {
                return(Task.CompletedTask);
            }

            var parsedTemplate = TemplateParser.Parse(Subdomain);

            //that's for overriding default for subdomain
            if (IsParameterName(Subdomain) &&
                Defaults.ContainsKey(ParameterNameFrom(Subdomain)) &&
                !context.RouteData.Values.ContainsKey(ParameterNameFrom(Subdomain)))
            {
                context.RouteData.Values.Add(ParameterNameFrom(Subdomain), subdomain);
            }

            if (IsParameterName(Subdomain) &&
                constraintsWithSubdomainConstraint.ContainsKey(ParameterNameFrom(Subdomain)))
            {
                if (!RouteConstraintMatcher.Match(
                        constraintsWithSubdomainConstraint,
                        new RouteValueDictionary
                {
                    { ParameterNameFrom(Subdomain), subdomain }
                },
                        context.HttpContext,
                        this,
                        RouteDirection.IncomingRequest,
                        context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>().CreateLogger(typeof(RouteConstraintMatcher).FullName)))
                {
                    return(Task.CompletedTask);
                }
            }


            return(base.RouteAsync(context));
        }
Exemple #3
0
        /// <inheritdoc />
        public virtual VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            EnsureBinder(context.HttpContext);
            EnsureLoggers(context.HttpContext);

            var values = _binder.GetValues(context.AmbientValues, context.Values);

            if (values == null)
            {
                // We're missing one of the required values for this route.
                return(null);
            }

            if (!RouteConstraintMatcher.Match(
                    Constraints,
                    values.CombinedValues,
                    context.HttpContext,
                    this,
                    RouteDirection.UrlGeneration,
                    _constraintLogger))
            {
                return(null);
            }

            context.Values = values.CombinedValues;

            var pathData = OnVirtualPathGenerated(context);

            if (pathData != null)
            {
                // If the target generates a value then that can short circuit.
                return(pathData);
            }

            // If we can produce a value go ahead and do it, the caller can check context.IsBound
            // to see if the values were validated.

            // When we still cannot produce a value, this should return null.
            var virtualPath = _binder.BindValues(values.AcceptedValues);

            if (virtualPath == null)
            {
                return(null);
            }

            pathData = new VirtualPathData(this, virtualPath);
            if (DataTokens != null)
            {
                foreach (var dataToken in DataTokens)
                {
                    pathData.DataTokens.Add(dataToken.Key, dataToken.Value);
                }
            }

            return(pathData);
        }
 public void ReturnsTrueOnNullInput()
 {
     Assert.True(RouteConstraintMatcher.Match(
                     constraints: null,
                     routeValues: new RouteValueDictionary(),
                     httpContext: new Mock <HttpContext>().Object,
                     route: new Mock <IRouter>().Object,
                     routeDirection: RouteDirection.IncomingRequest,
                     logger: NullLogger.Instance));
 }
Exemple #5
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));
        }
        public void ReturnsTrueOnValidConstraints()
        {
            var constraints = new Dictionary <string, IRouteConstraint>
            {
                { "a", new PassConstraint() },
                { "b", new PassConstraint() }
            };

            var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });

            Assert.True(RouteConstraintMatcher.Match(
                            constraints: constraints,
                            routeValues: routeValueDictionary,
                            httpContext: new Mock <HttpContext>().Object,
                            route: new Mock <IRouter>().Object,
                            routeDirection: RouteDirection.IncomingRequest,
                            logger: NullLogger.Instance));
        }
        private TestSink SetUpMatch(Dictionary <string, IRouteConstraint> constraints, bool loggerEnabled)
        {
            // Arrange
            var sink   = new TestSink();
            var logger = new TestLogger(_name, sink, loggerEnabled);

            var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });

            // Act
            RouteConstraintMatcher.Match(
                constraints: constraints,
                routeValues: routeValueDictionary,
                httpContext: new Mock <HttpContext>().Object,
                route: new Mock <IRouter>().Object,
                routeDirection: RouteDirection.IncomingRequest,
                logger: logger);
            return(sink);
        }