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);
        }
        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_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);
        }
        public FunctionDescriptor GetHttpFunctionOrNull(HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_httpFunctions == null || _httpFunctions.Count == 0)
            {
                return(null);
            }

            FunctionDescriptor function = null;
            var routeData = _httpRoutes.GetRouteData(request);

            if (routeData != null)
            {
                _httpFunctions.TryGetValue(routeData.Route, out function);
                AddRouteDataToRequest(routeData, request);
            }

            return(function);
        }
        void Run(Type controller, BinderPrecedence.BinderPrecedence expected)
        {
            config.MapCodeRoutes(controller);

            var request   = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
            var routeData = routes.GetRouteData(request);

            var controllerInstance = (ApiController)Activator.CreateInstance(controller);

            var controllerContext = new HttpControllerContext(config, routeData, request)
            {
                ControllerDescriptor = new HttpControllerDescriptor(config, (string)routeData.Values["controller"], controller),
                Controller           = controllerInstance
            };

            string value;

            controllerInstance.ExecuteAsync(controllerContext, CancellationToken.None)
            .Result
            .TryGetContentValue(out value);

            Assert.AreEqual(expected.ToString(), value);
        }
Ejemplo n.º 6
0
        public void SetupBigRouteTable()
        {
            var route = new TreeRoute("");
            route.AddWithPath("x/foo/{controller}/{id}", r => { });
            route.AddWithPath("x/bar/{controller}/{id}", r => { });
            route.AddWithPath("x/baz/{controller}/{id}", r => { });
            route.AddWithPath("y/blip/{controller}/{id}", r => { });
            route.AddWithPath("y/flid/{controller}/{id}", r => { });
            route.AddWithPath("y/flod/{controller}/{id}", r => { });

            var routes = new HttpRouteCollection();
            routes.MapHttpRoute("a", "x/foo/{controller}/{id}");
            routes.MapHttpRoute("b", "x/bar/{controller}/{id}");
            routes.MapHttpRoute("c", "x/baz/{controller}/{id}");
            routes.MapHttpRoute("d", "y/blip/{controller}/{id}");
            routes.MapHttpRoute("e", "y/flid/{controller}/{id}");
            routes.MapHttpRoute("f", "y/flod/{controller}/{id}");

            var httpRequestMessage = new HttpRequestMessage() { RequestUri = new Uri("http://localhost/y/flid/boo/22") };

            var stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 0; i < 1000000; i++)
            {
                var getRouteData = route.GetRouteData("/", httpRequestMessage);
            }

            stopwatch.Stop();
            Debug.WriteLine("That took " + stopwatch.ElapsedMilliseconds);

            stopwatch.Reset();
            stopwatch.Start();

            for (int i = 0; i < 1000000; i++)
            {
                var mvcroute = routes.GetRouteData(httpRequestMessage);
            }
            stopwatch.Stop();
            Debug.WriteLine("That took " + stopwatch.ElapsedMilliseconds);
        }