示例#1
0
        public void Request_CreatesSessionfulHandler()
        {
            StringWriter writer = new StringWriter();

            HttpResponse res = new HttpResponse(writer);
            HttpRequest  req = new HttpRequest(Path.Combine(
                                                   AppDomain.CurrentDomain.BaseDirectory, "Handlers/Files/simplerequest.txt"),
                                               "http://localhost:1333/home/something", "");
            RouteMatch  routeMatch = new RouteMatch();
            HttpContext httpCtx    = new HttpContext(req, res);

            httpCtx.Items[RouteMatch.RouteMatchKey] = routeMatch;

            using (_MockRepository.Record())
            {
                ControllerMetaDescriptor controllerDesc = new ControllerMetaDescriptor();
                controllerDesc.ControllerDescriptor = new ControllerDescriptor(typeof(Controller), "home", "", false);

                Expect.Call(_ControllerFactoryMock.CreateController("", "home")).IgnoreArguments().Return(_ControllerMock);
                Expect.Call(_ControllerDescriptorProviderMock.BuildDescriptor(_ControllerMock)).Return(controllerDesc);
                Expect.Call(_ControllerContextFactoryMock.Create("", "home", "something", controllerDesc, routeMatch)).
                Return(new ControllerContext());
            }

            using (_MockRepository.Playback())
            {
                IHttpHandler handler = _HandlerFactory.GetHandler(httpCtx, "GET", "", "");

                Assert.IsNotNull(handler);
                Assert.IsInstanceOfType(typeof(MonoRailHttpHandler), handler);
            }
        }
示例#2
0
        /// <summary>
        /// Generates an AJAX JavaScript proxy for a given controller.
        /// </summary>
        /// <param name="context">The context of the current request</param>
        /// <param name="proxyName">Name of the javascript proxy object</param>
        /// <param name="controller">Controller which will be target of the proxy</param>
        /// <param name="area">area which the controller belongs to</param>
        public string GenerateJSProxy(IEngineContext context, string proxyName, string area, string controller)
        {
            var cacheKey = (area + "|" + controller).ToLower(CultureInfo.InvariantCulture);
            var result   = (String)ajaxProxyCache[cacheKey];

            if (result != null)
            {
                return(WrapProxyIfNeeded(result));
            }

            logger.Debug("Ajax Proxy for area: '{0}', controller: '{1}' was not found. Generating a new one", area, controller);

            var controllerType = controllerTree.GetController(area, controller);

            if (controllerType == null)
            {
                logger.Fatal("Controller not found for the area and controller specified");
                throw new MonoRailException("Controller not found with Area: '{0}', Name: '{1}'", area, controller);
            }

            var baseUrl = GetBaseUrl(area, controller, context);

            // TODO: develop a smarter function generation, inspecting the return
            // value of the action and generating a proxy that does the same.
            // also, consider a proxy pattern for the Ajax.Updater.
            var metaDescriptor = controllerDescriptorBuilder.BuildDescriptor(controllerType);

            var functions = new StringBuilder();

            functions.AppendLine("{");

            for (var i = 0; i < metaDescriptor.AjaxActions.Count; i++)
            {
                var ajaxActionMethod = metaDescriptor.AjaxActions[i];
                var ajaxActionAtt    = GetSingleAttribute <AjaxActionAttribute>(ajaxActionMethod, true);

                var httpRequestMethod = GetHTTPRequestMethod(ajaxActionMethod);
                var extension         = GetURLExtension(context);
                var url          = baseUrl + ajaxActionMethod.Name + extension;
                var functionName = ToCamelCase(ajaxActionAtt.Name ?? ajaxActionMethod.Name);

                var scriptFunctionParameters = GetScriptFunctionParameters(ajaxActionMethod.GetParameters());

                functions.Append(
                    GenerateJavascriptFunction(url, functionName, httpRequestMethod, scriptFunctionParameters)
                    );

                // js functions are seperated by a comma
                if (metaDescriptor.AjaxActions.Count > 0 && i < metaDescriptor.AjaxActions.Count - 1)
                {
                    functions.AppendLine(",");
                }
            }

            functions.AppendLine("};");
            ajaxProxyCache[cacheKey] = "var " + proxyName + " =" + Environment.NewLine + functions;

            return(GenerateJSProxy(context, proxyName, area, controller));
        }
示例#3
0
        public IList <SparkViewDescriptor> CreateDescriptors(SparkBatchEntry entry)
        {
            var descriptors = new List <SparkViewDescriptor>();

            var metaDesc = _controllerDescriptorProvider.BuildDescriptor(entry.ControllerType);

            var controllerName = metaDesc.ControllerDescriptor.Name;
            var controllerPath = controllerName;

            if (!string.IsNullOrEmpty(metaDesc.ControllerDescriptor.Area))
            {
                controllerPath = metaDesc.ControllerDescriptor.Area + "\\" + controllerName;
            }

            var viewNames    = new List <string>();
            var includeViews = entry.IncludeViews;

            if (includeViews.Count == 0)
            {
                includeViews = new[] { "*" }
            }
            ;

            var accessors = new List <SparkViewDescriptor.Accessor>();

            foreach (var helper in metaDesc.Helpers)
            {
                var typeName     = helper.HelperType.FullName;
                var propertyName = helper.Name ?? helper.HelperType.Name;
                accessors.Add(new SparkViewDescriptor.Accessor
                {
                    Property = typeName + " " + propertyName,
                    GetValue = "Helper<" + typeName + ">(\"" + propertyName + "\")"
                });
            }

            foreach (var include in includeViews)
            {
                if (include.EndsWith("*"))
                {
                    foreach (var fileName in ViewSourceLoader.ListViews(controllerPath))
                    {
                        // ignore files which are not spark extension
                        if (!string.Equals(Path.GetExtension(fileName), ".spark", StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        var potentialMatch = Path.GetFileNameWithoutExtension(fileName);
                        if (!TestMatch(potentialMatch, include))
                        {
                            continue;
                        }

                        var isExcluded = false;
                        foreach (var exclude in entry.ExcludeViews)
                        {
                            if (!TestMatch(potentialMatch, RemoveSuffix(exclude, ".spark")))
                            {
                                continue;
                            }

                            isExcluded = true;
                            break;
                        }
                        if (!isExcluded)
                        {
                            viewNames.Add(potentialMatch);
                        }
                    }
                }
                else
                {
                    // explicitly included views don't test for exclusion
                    viewNames.Add(RemoveSuffix(include, ".spark"));
                }
            }

            foreach (var viewName in viewNames)
            {
                var layoutNamesList = entry.LayoutNames;
                if (layoutNamesList.Count == 0)
                {
                    var action = metaDesc.Actions[viewName];
                    if (action != null)
                    {
                        var actionDesc = metaDesc.ActionDescriptors[action];
                        if (actionDesc != null && actionDesc.Layout != null)
                        {
                            layoutNamesList = new[] { actionDesc.Layout.LayoutNames }
                        }
                        ;
                    }
                }
                if (layoutNamesList.Count == 0)
                {
                    if (metaDesc.Layout != null)
                    {
                        layoutNamesList = new[] { metaDesc.Layout.LayoutNames }
                    }
                    ;
                    else
                    {
                        layoutNamesList = new[] { new string[0] }
                    };
                }

                foreach (var layoutNames in layoutNamesList)
                {
                    descriptors.Add(CreateDescriptor(
                                        entry.ControllerType.Namespace,
                                        controllerPath,
                                        viewName,
                                        layoutNames,
                                        accessors));
                }
            }

            return(descriptors);
        }