示例#1
0
        static IUrlHelper NewUrlHelper(string controller, string action, string version)
        {
            var services = new ServiceCollection();

            services.AddOptions();
            services.AddLogging();
            services.AddRouting();
            services.AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
            .AddSingleton(UrlEncoder.Default);
            services.AddMvcCore();
            services.AddApiVersioning();

            var provider    = services.BuildServiceProvider();
            var httpContext = new DefaultHttpContext()
            {
                RequestServices = provider
            };
            var routeBuilder  = CreateRouteBuilder(provider);
            var actionContext = new ActionContext()
            {
                HttpContext = httpContext
            };
            var constraint = new ApiVersionRouteConstraint();

            httpContext.Features.Set <IApiVersioningFeature>(new ApiVersioningFeature(httpContext));
            routeBuilder.MapRoute("default", "v{version:apiVersion}/{controller}/{action}");

            var router = routeBuilder.Build();

            actionContext.RouteData = new RouteData()
            {
                Values =
                {
                    [nameof(controller)] = controller,
                    [nameof(action)]     = action,
                    [nameof(version)]    = version,
                },
                Routers =
                {
                    router,
                }
            };
            actionContext.RouteData.Routers.Add(router);
            constraint.Match(httpContext, router, nameof(version), actionContext.RouteData.Values, IncomingRequest);

            var factory = provider.GetRequiredService <IUrlHelperFactory>();

            return(factory.GetUrlHelper(actionContext));
        }
        public void match_should_return_false_when_route_key_is_missing()
        {
            // arrange
            var httpContext    = NewHttpContext();
            var route          = new Mock <IRouter>().Object;
            var values         = new RouteValueDictionary();
            var routeDirection = IncomingRequest;
            var constraint     = new ApiVersionRouteConstraint();

            // act
            var matched = constraint.Match(httpContext, route, "version", values, routeDirection);

            // assert
            matched.Should().BeFalse();
        }
        public void match_should_return_false_when_route_parameter_is_invalid(string version)
        {
            // arrange
            var httpContext = NewHttpContext();
            var route       = new Mock <IRouter>().Object;
            var routeKey    = nameof(version);
            var values      = new RouteValueDictionary()
            {
                [routeKey] = version
            };
            var routeDirection = IncomingRequest;
            var constraint     = new ApiVersionRouteConstraint();

            // act
            var matched = constraint.Match(httpContext, route, routeKey, values, routeDirection);

            // assert
            matched.Should().BeFalse();
        }
        public void match_should_return_expected_result_for_url_generation(string key, string value, bool expected)
        {
            // arrange
            var httpContext    = NewHttpContext();
            var route          = new Mock <IRouter>().Object;
            var values         = new RouteValueDictionary();
            var routeDirection = UrlGeneration;
            var constraint     = new ApiVersionRouteConstraint();

            if (!IsNullOrEmpty(key))
            {
                values[key] = value;
            }

            // act
            var matched = constraint.Match(httpContext, route, key, values, routeDirection);

            // assert
            matched.Should().Be(expected);
        }
示例#5
0
        public void match_should_return_true_when_matched()
        {
            // arrange
            var httpContext = new Mock <HttpContext>();
            var route       = new Mock <IRouter>().Object;
            var values      = new RouteValueDictionary()
            {
                ["version"] = "2.0"
            };
            var routeDirection = IncomingRequest;
            var constraint     = new ApiVersionRouteConstraint();

            httpContext.SetupProperty(c => c.Items, new Dictionary <object, object>());

            // act
            var matched = constraint.Match(httpContext.Object, route, "version", values, routeDirection);

            // assert
            matched.Should().BeTrue();
        }