Esempio n. 1
0
        /// <summary>
        /// The run.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Generic exception - Route path not valid.
        /// </exception>
        public static ChromelyResponse Run(CefRequest request)
        {
            var    uri  = new Uri(request.Url);
            string path = uri.LocalPath;

            var response = new ChromelyResponse();

            if (string.IsNullOrEmpty(path))
            {
                response.ReadyState = (int)ReadyState.ResponseIsReady;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Bad Request";

                return(response);
            }

            if (path.ToLower().Equals("/info"))
            {
                response = GetInfo();
                return(response);
            }

            var routePath = new RoutePath(request.Method, path);
            var route     = ServiceRouteProvider.GetRoute(routePath);

            if (route == null)
            {
                throw new Exception($"Route for path = {path} is null or invalid.");
            }

            var parameters = request.Url.GetParameters();
            var postData   = GetPostData(request);

            return(ExcuteRoute(string.Empty, routePath, parameters, postData));
        }
Esempio n. 2
0
        public void ScannerShouldInstantiateScannerControllerWithInjectedDependencies()
        {
            const string TestRoute = "/scannercontroller/get2";

            var test = new TestDependency();

            IoC.RegisterInstance <ITestDependency>(nameof(ITestDependency), test);

            var logger = new TestLogger();

            IoC.RegisterInstance <IChromelyLogger>(nameof(IChromelyLogger), logger);

            var scanner       = new RouteScanner(Assembly.GetExecutingAssembly());
            var routeCommands = scanner.Scan();

            foreach (var route in routeCommands.Item1)
            {
                ServiceRouteProvider.AddRoute(route.Key, route.Value);
            }

            foreach (var command in routeCommands.Item2)
            {
                ServiceRouteProvider.AddCommand(command.Key, command.Value);
            }

            var request = new ChromelyRequest(new RoutePath(Method.GET, TestRoute), null, null);

            var routePath   = new RoutePath(Method.GET, TestRoute);
            var get2        = ServiceRouteProvider.GetRoute(routePath);
            var getResponse = get2.Invoke(request);

            Assert.Equal(TestDependency.TestDependencyResponse, getResponse.Data.ToString());
            Assert.Equal(ScannerControllerWithDependencyInjection.Get2Response, logger.Message);
        }
Esempio n. 3
0
        /// <summary>
        /// The run.
        /// </summary>
        /// <param name="requestId">
        /// The request identifier.
        /// </param>
        /// <param name="routePath">
        /// The route path.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <param name="postData">
        /// The post data.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Generic exception - Route path not valid.
        /// </exception>
        public static ChromelyResponse Run(string requestId, RoutePath routePath, object parameters, object postData)
        {
            var response = new ChromelyResponse(requestId);

            if (string.IsNullOrEmpty(routePath.Path))
            {
                response.ReadyState = (int)ReadyState.ResponseIsReady;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Bad Request";

                return(response);
            }

            if (routePath.Path.ToLower().Equals("/info"))
            {
                response = GetInfo();
                return(response);
            }

            var route = ServiceRouteProvider.GetRoute(routePath);

            if (route == null)
            {
                throw new Exception($"Route for path = {routePath} is null or invalid.");
            }

            return(ExcuteRoute(requestId, routePath, parameters, postData));
        }
Esempio n. 4
0
        /// <summary>
        /// The excute route async.
        /// </summary>
        /// <param name="requestId">
        /// The request id.
        /// </param>
        /// <param name="routePath">
        /// The route path.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <param name="postData">
        /// The post data.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        private static async Task <ChromelyResponse> ExcuteRouteAsync(string requestId, RoutePath routePath, object parameters, object postData)
        {
            var route = ServiceRouteProvider.GetRoute(routePath);

            if (route == null)
            {
                throw new Exception($"Route for path = {routePath} is null or invalid.");
            }

            ChromelyResponse response;

            try
            {
                if (route.IsAsync)
                {
                    // ReSharper disable once ConsiderUsingConfigureAwait
                    response = await route.InvokeAsync(
                        requestId,
                        routePath,
                        parameters?.ToObjectDictionary(),
                        postData);
                }
                else
                {
                    response = route.Invoke(
                        requestId,
                        routePath,
                        parameters?.ToObjectDictionary(),
                        postData);
                }

                response.ReadyState = (int)ReadyState.ResponseIsReady;
                response.Status     = (int)System.Net.HttpStatusCode.OK;
                response.StatusText = "OK";
            }
            catch (Exception exception)
            {
                Log.Error(exception);
                response = new ChromelyResponse
                {
                    ReadyState = (int)ReadyState.ResponseIsReady,
                    Status     = (int)System.Net.HttpStatusCode.InternalServerError,
                    StatusText = "Error"
                };
            }

            return(response);
        }
Esempio n. 5
0
        /// <summary>
        /// The excute route.
        /// </summary>
        /// <param name="requestId">
        /// The request identifier.
        /// </param>
        /// <param name="routePath">
        /// The route path.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <param name="postData">
        /// The post data.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Generic exception - Route path not valid.
        /// </exception>
        private static ChromelyResponse ExcuteRoute(string requestId, RoutePath routePath, object parameters, object postData)
        {
            var route = ServiceRouteProvider.GetRoute(routePath);

            if (route == null)
            {
                throw new Exception($"Route for path = {routePath} is null or invalid.");
            }

            var response = route.Invoke(requestId: requestId, routePath: routePath, parameters: parameters?.ToObjectDictionary(), postData: postData);

            response.ReadyState = (int)ReadyState.ResponseIsReady;
            response.Status     = (int)System.Net.HttpStatusCode.OK;
            response.StatusText = "OK";

            return(response);
        }
        public void RouteProviderTest()
        {
            var routeDict = BaseTest();

            Assert.Equal(3, routeDict.Count);

            foreach (var item in routeDict)
            {
                ServiceRouteProvider.AddRoute(item.Key, item.Value);
            }

            var getRoute1 = ServiceRouteProvider.GetRoute(new RoutePath(Method.GET, "/testcontroller/get1"));
            var getRoute2 = ServiceRouteProvider.GetRoute(new RoutePath(Method.GET, "/testcontroller/get2"));
            var postRoute = ServiceRouteProvider.GetRoute(new RoutePath(Method.POST, "/testcontroller/save"));

            Assert.True((getRoute1 != null) || (getRoute2 != null) || (postRoute != null));
        }
Esempio n. 7
0
        public void RouteProviderTest()
        {
            var routeDict = BaseTest();

            Assert.Equal(3, routeDict.Count);

            foreach (var item in routeDict)
            {
                ServiceRouteProvider.AddRoute(item.Key, item.Value);
            }

            foreach (var item in routeDict)
            {
                var route = ServiceRouteProvider.GetRoute(item.Key);
                Assert.NotNull(route);
            }
        }
Esempio n. 8
0
        private static ChromelyResponse ExcuteRoute(string routePath, object parameters, object postData)
        {
            ChromelyResponse response = new ChromelyResponse();

            Route route = ServiceRouteProvider.GetRoute(routePath);

            if (route == null)
            {
                throw new Exception(string.Format("Route for path = {0} is null or invalid.", routePath));
            }

            response            = route.Invoke(routePath, parameters: parameters?.ToObjectDictionary(), postData: postData);
            response.ReadyState = (int)ReadyState.ResponseIsReady;
            response.Status     = (int)System.Net.HttpStatusCode.OK;
            response.StatusText = "OK";

            return(response);
        }
        public void RouteProviderTest()
        {
            var routeDict = BaseTest();

            Assert.Equal(3, routeDict.Count);

            foreach (var item in routeDict)
            {
                ServiceRouteProvider.AddRoute(item.Key, item.Value);
            }

            foreach (var item in routeDict)
            {
                var getRoute  = ServiceRouteProvider.GetRoute(new RoutePath(Method.GET, item.Key));
                var postRoute = ServiceRouteProvider.GetRoute(new RoutePath(Method.GET, item.Key));
                Assert.True((getRoute != null) || (postRoute != null));
            }
        }
Esempio n. 10
0
        /// <summary>
        /// The run.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Generic exception - Route path not valid.
        /// </exception>
        public static ChromelyResponse Run(ChromelyRequest request)
        {
            var response = new ChromelyResponse(request.Id);

            if (request.RoutePath == null)
            {
                response.ReadyState = (int)ReadyState.ResponseIsReady;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Bad Request";

                return(response);
            }

            if (string.IsNullOrEmpty(request.RoutePath.Path))
            {
                response.ReadyState = (int)ReadyState.ResponseIsReady;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Bad Request";

                return(response);
            }

            if (request.RoutePath.Path.ToLower().Equals("/info"))
            {
                response = GetInfo();
                return(response);
            }

            var route = ServiceRouteProvider.GetRoute(request.RoutePath);

            if (route == null)
            {
                throw new Exception($"Route for path = {request.RoutePath} is null or invalid.");
            }

            var parameters = request.Parameters ?? request.RoutePath.Path.GetParameters()?.ToObjectDictionary();
            var postData   = request.PostData;

            return(ExcuteRoute(request.Id, request.RoutePath, parameters, postData, request.RawJson));
        }