private static List <HttpOperationParameterInfo> GetInputParameters(IHttpRoute httpRoute)
        {
            var parameters       = new List <HttpOperationParameterInfo>();
            var httpRouteFactory = new HttpRouteFactory();
            var pathParameters   = httpRouteFactory.GetRouteParameters(httpRoute.RouteTemplate).ToList();

            foreach (var constraint in httpRoute.Constraints)
            {
                var             httpConstraint  = constraint.Value as IHttpRouteConstraint;
                SwaggerDataType?swaggerDataType = httpConstraint.ToSwaggerDataType();
                if (swaggerDataType != null && httpConstraint.GetType() != typeof(HttpMethodConstraint))
                {
                    var parameter = new HttpOperationParameterInfo()
                    {
                        DataType = swaggerDataType.ToString().ToLowerInvariant(),
                        Name     = constraint.Key
                    };
                    parameters.Add(parameter);
                    pathParameters.Remove(constraint.Key);
                }
            }

            parameters.AddRange(pathParameters.Select(p =>
                                                      new HttpOperationParameterInfo()
            {
                DataType = SwaggerDataType.String.ToString().ToLowerInvariant(),
                Name     = p
            }));

            return(parameters);
        }
Exemplo n.º 2
0
        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 functionHttpRouteFactory = new HttpRouteFactory(httpConfig.RoutePrefix);

            // Proxies do not honor the route prefix defined in host.json
            var proxyHttpRouteFactory = new HttpRouteFactory(string.Empty);

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

            // Proxy routes will take precedence over http trigger functions and http trigger
            // routes so they will be added first to the list of http routes.
            var orderdFunctions = functions.OrderBy(f => f.Metadata.IsProxy ? 0 : 1);

            foreach (var function in orderdFunctions)
            {
                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();
                    }
                    var httpRouteFactory = function.Metadata.IsProxy ? proxyHttpRouteFactory : functionHttpRouteFactory;
                    if (httpRouteFactory.TryAddRoute(function.Metadata.Name, httpTrigger.Route, httpMethods, _httpRoutes, out httpRoute))
                    {
                        _httpFunctions.Add(httpRoute, function);
                    }
                }
            }
        }
        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 GetRouteParameters_ReturnsExpectedResult(string routeTemplate, string expected)
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");
            var parameters = routeFactory.GetRouteParameters(routeTemplate);
            var result     = string.Join(",", parameters);

            Assert.Equal(expected, result);
        }
        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 AddRoute_AmbiguousRoute_FirstRouteIsChosen()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            var route1 = routeFactory.AddRoute("route1", "foo/bar/baz", null, routes);
            var route2 = routeFactory.AddRoute("route2", "foo/bar/baz", null, routes);

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

            Assert.Same(route1, routeData.Route);
        }
        public static void AddRoute_MethodsCollectionEmpty_AppliesHttpMethodConstraint()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            var route = routeFactory.AddRoute("route1", "products/{category}/{id?}", new HttpMethod[0], routes);

            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);
        }
        public static void TryAddRoute_MethodsCollectionNull_DoesNotApplyHttpMethodConstraint()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

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

            Assert.True(routeFactory.TryAddRoute("route1", "products/{category}/{id?}", null, routes, out route));

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

            Assert.Same(route, routeData.Route);

            request   = new HttpRequestMessage(HttpMethod.Post, "http://host/api/products/electronics/123");
            routeData = routes.GetRouteData(request);
            Assert.Same(route, routeData.Route);
        }
        internal void InitializeHttpFunctions(Collection <FunctionDescriptor> functions)
        {
            // we must initialize the route factory here AFTER full configuration
            // has been resolved so we apply any route prefix customizations
            HttpRouteFactory httpRouteFactory = new HttpRouteFactory(_config.HttpRoutePrefix);

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

            foreach (var function in functions)
            {
                var httpTriggerBinding = function.Metadata.InputBindings.OfType <HttpTriggerBindingMetadata>().SingleOrDefault();
                if (httpTriggerBinding != null)
                {
                    var httpRoute = httpRouteFactory.AddRoute(function.Metadata.Name, httpTriggerBinding.Route, httpTriggerBinding.Methods, _httpRoutes);
                    _httpFunctions.Add(httpRoute, function);
                }
            }
        }
        public static void AddRoute_AppliesHttpMethodConstraint()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            var route1 = routeFactory.AddRoute("route1", "products/{category}/{id?}", new HttpMethod[] { HttpMethod.Get }, routes);
            var route2 = routeFactory.AddRoute("route2", "products/{category}/{id}", new HttpMethod[] { HttpMethod.Post }, routes);

            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);
        }