Пример #1
0
        public RouteManager([NotNull] object baseRouter)
        {
            var routerType      = baseRouter.GetType();
            var routerAttribute = routerType.GetCustomAttribute <RouterAttribute>();

            if (routerAttribute == null)
            {
                throw new ArgumentException("Invalid router class, requires a RouterAttribute.", nameof(baseRouter));
            }

            var path        = new HttpPath(routerAttribute.Path);
            var routerRoute = baseRoute.CreateRoute(path);

            foreach (var method in routerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                     .Where(x => x.GetCustomAttribute <MethodAttribute>() != null))
            {
                //TODO: Maybe have any return type and convert?
                if (method.ReturnType != typeof(HttpResponse))
                {
                    throw new InvalidOperationException();
                }

                foreach (var methodAttribute in method.GetCustomAttributes <MethodAttribute>())
                {
                    var routePath   = new HttpRoutePath(methodAttribute.Path);
                    var methodRoute = routerRoute.CreateRoute(routePath);
                    var routeMethod = new RouteMethod(baseRouter, method);
                    methodRoute.SetMethod(methodAttribute.Method, routeMethod);
                }
            }

            badRoute = new RouteMethod(this, GetType().GetMethod("BadRoute", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
        }
Пример #2
0
        public void TestEmptyPath()
        {
            var path = new HttpPath("/");

            Assert.Equal(0, path.Segments.Count);
        }