public static void TryAddRoute_AmbiguousRoute_FirstRouteIsChosen()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute          route1, route2;

            Assert.True(routeFactory.TryAddRoute("route1", "foo/bar/baz", null, routes, out route1));
            Assert.True(routeFactory.TryAddRoute("route2", "foo/bar/baz", null, routes, out route2));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://host/api/foo/bar/baz");
            var routeData = routes.GetRouteData(request);

            Assert.Same(route1, routeData.Route);
        }
        private void InitializeHttpFunctions(IEnumerable <FunctionDescriptor> functions, HttpExtensionConfiguration httpConfig)
        {
            // we must initialize the route factory here AFTER full configuration
            // has been resolved so we apply any route prefix customizations
            var httpRouteFactory = new HttpRouteFactory(httpConfig.RoutePrefix);

            _httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>();
            _httpRoutes    = new HttpRouteCollection();

            foreach (var function in functions)
            {
                var httpTrigger = function.GetTriggerAttributeOrNull <HttpTriggerAttribute>();
                if (httpTrigger != null)
                {
                    IHttpRoute httpRoute = null;
                    IEnumerable <HttpMethod> httpMethods = null;
                    if (httpTrigger.Methods != null)
                    {
                        httpMethods = httpTrigger.Methods.Select(p => new HttpMethod(p)).ToArray();
                    }
                    if (httpRouteFactory.TryAddRoute(function.Metadata.Name, httpTrigger.Route, httpMethods, _httpRoutes, out httpRoute))
                    {
                        _httpFunctions.Add(httpRoute, function);
                    }
                }
            }
        }
        public static void TryAddRoute_RouteParsingError_ReturnsFalse()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute          route  = null;

            Assert.False(routeFactory.TryAddRoute("route1", "/", new HttpMethod[0], routes, out route));
            Assert.Equal(0, routes.Count);
        }
        public static void TryAddRoute_AppliesHttpMethodConstraint()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute          route1, route2;

            Assert.True(routeFactory.TryAddRoute("route1", "products/{category}/{id?}", new HttpMethod[] { HttpMethod.Get }, routes, out route1));
            Assert.True(routeFactory.TryAddRoute("route2", "products/{category}/{id}", new HttpMethod[] { HttpMethod.Post }, routes, out route2));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://host/api/products/electronics/123");
            var routeData = routes.GetRouteData(request);

            Assert.Same(route1, routeData.Route);

            request   = new HttpRequestMessage(HttpMethod.Get, "http://host/api/products/electronics");
            routeData = routes.GetRouteData(request);
            Assert.Same(route1, routeData.Route);

            request   = new HttpRequestMessage(HttpMethod.Post, "http://host/api/products/electronics/123");
            routeData = routes.GetRouteData(request);
            Assert.Same(route2, routeData.Route);
        }
        public static void TryAddRoute_MethodsCollectionEmpty_AppliesHttpMethodConstraint()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute          route  = null;

            Assert.True(routeFactory.TryAddRoute("route1", "products/{category}/{id?}", new HttpMethod[0], routes, out route));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://host/api/products/electronics/123");
            var routeData = routes.GetRouteData(request);

            Assert.Null(routeData);

            request   = new HttpRequestMessage(HttpMethod.Post, "http://host/api/products/electronics/123");
            routeData = routes.GetRouteData(request);
            Assert.Null(routeData);
        }
Пример #6
0
        internal void InitializeHttpFunctions(IEnumerable <FunctionDescriptor> functions)
        {
            // we must initialize the route factory here AFTER full configuration
            // has been resolved so we apply any route prefix customizations
            var httpRouteFactory = new HttpRouteFactory(_config.HttpConfiguration.RoutePrefix);

            _httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>();
            _httpRoutes    = new HttpRouteCollection();

            foreach (var function in functions)
            {
                var httpTriggerBinding = function.Metadata.InputBindings.OfType <HttpTriggerBindingMetadata>().SingleOrDefault();
                if (httpTriggerBinding != null)
                {
                    IHttpRoute httpRoute = null;
                    if (httpRouteFactory.TryAddRoute(function.Metadata.Name, httpTriggerBinding.Route, httpTriggerBinding.Methods, _httpRoutes, out httpRoute))
                    {
                        _httpFunctions.Add(httpRoute, function);
                    }
                }
            }
        }