public void Init()
        {
            mocks = new MockRepository();
            factory = new SparkViewFactory();
            engineContext = mocks.CreateMock<IEngineContext>();
            server = new MockServerUtility();
            request = mocks.CreateMock<IRequest>();
            response = mocks.CreateMock<IResponse>();

            controller = mocks.CreateMock<IController>();
            controllerContext = mocks.CreateMock<IControllerContext>();
            routingEngine = mocks.CreateMock<IRoutingEngine>();
            output = new StringWriter();
            helpers = new HelperDictionary();

            propertyBag = new Dictionary<string, object>();
            flash = new Flash();
            session = new Dictionary<string, object>();
            requestParams = new NameValueCollection();
            contextItems = new Dictionary<string, object>();


            SetupResult.For(engineContext.Server).Return(server);
            SetupResult.For(engineContext.Request).Return(request);
            SetupResult.For(engineContext.Response).Return(response);
            SetupResult.For(engineContext.CurrentController).Return(controller);
            SetupResult.For(engineContext.CurrentControllerContext).Return(controllerContext);
            SetupResult.For(engineContext.Flash).Return(flash);
            SetupResult.For(engineContext.Session).Return(session);
            SetupResult.For(engineContext.Items).Return(contextItems);

            SetupResult.For(request.Params).Return(requestParams);

            SetupResult.For(controllerContext.LayoutNames).Return(new[] { "default" });
            SetupResult.For(controllerContext.Helpers).Return(helpers);
            SetupResult.For(controllerContext.PropertyBag).Return(propertyBag);

            SetupResult.For(routingEngine.IsEmpty).Return(true);

            var urlBuilder = new DefaultUrlBuilder(server, routingEngine);

            var serviceProvider = mocks.CreateMock<IServiceProvider>();
            var viewSourceLoader = new FileAssemblyViewSourceLoader("Views");
            SetupResult.For(serviceProvider.GetService(typeof(IViewSourceLoader))).Return(viewSourceLoader);
            SetupResult.For(serviceProvider.GetService(typeof(ILoggerFactory))).Return(new NullLogFactory());
            SetupResult.For(serviceProvider.GetService(typeof(ISparkViewEngine))).Return(null);
            SetupResult.For(serviceProvider.GetService(typeof(IUrlBuilder))).Return(urlBuilder);
            SetupResult.For(serviceProvider.GetService(typeof(IViewComponentFactory))).Return(null);
            mocks.Replay(serviceProvider);

            SetupResult.For(engineContext.GetService(null)).IgnoreArguments().Do(
                new Func<Type, object>(serviceProvider.GetService));

            factory.Service(serviceProvider);


            manager = new DefaultViewEngineManager();
            manager.RegisterEngineForExtesionLookup(factory);
            manager.RegisterEngineForView(factory);
        }
Exemple #2
0
        private static UrlParts CreateForRelativePath(IServerUtility serverUtility, string url)
        {
            var    path     = url;
            string qs       = null;
            string pathInfo = null;

            var queryStringStartIndex = url.IndexOf('?');

            if (queryStringStartIndex != -1)
            {
                qs   = url.Substring(queryStringStartIndex);
                path = url.Substring(0, queryStringStartIndex);
            }

            var fileExtIndex = path.IndexOf('.');

            if (fileExtIndex != -1)
            {
                var pathInfoStartIndex = path.IndexOf('/', fileExtIndex);

                if (pathInfoStartIndex != -1)
                {
                    pathInfo = path.Substring(pathInfoStartIndex);
                    path     = path.Substring(0, pathInfoStartIndex);
                }
            }

            var parts = new UrlParts(serverUtility, path);

            parts.SetQueryString(qs);
            parts.PathInfoDict.Parse(pathInfo);

            return(parts);
        }
Exemple #3
0
		/// <summary>
		/// Initializes a new instance of the <see cref="UrlParts"/> class.
		/// </summary>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="pathPieces">The path pieces.</param>
		public UrlParts(IServerUtility serverUtility, params string[] pathPieces)
		{
			url = new StringBuilder();
			this.serverUtility = serverUtility;

			AppendPaths(pathPieces);
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="ResponseAdapter"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 /// <param name="currentUrl">The current URL.</param>
 /// <param name="urlBuilder">The URL builder.</param>
 /// <param name="serverUtility">The server utility.</param>
 /// <param name="routeMatch">The route match.</param>
 /// <param name="referrer">The referrer.</param>
 public ResponseAdapter(HttpResponse response, UrlInfo currentUrl,
                        IUrlBuilder urlBuilder, IServerUtility serverUtility,
                        RouteMatch routeMatch, string referrer)
     : base(currentUrl, urlBuilder, serverUtility, routeMatch, referrer)
 {
     this.response = response;
 }
        public void Init()
        {
            mocks           = new MockRepository();
            serviceProvider = new StubMonoRailServices();

            var viewSourceLoader = new FileAssemblyViewSourceLoader("MonoRail.Tests.Views");

            viewSourceLoader.Service(this.serviceProvider);
            serviceProvider.ViewSourceLoader = viewSourceLoader;
            serviceProvider.AddService(typeof(IViewSourceLoader), viewSourceLoader);

            Configure();

            controllerContext = new ControllerContext();
            propertyBag       = controllerContext.PropertyBag;

            controllerContext.LayoutNames = new [] { "default" };
            output = new StringWriter();

            server        = new StubServerUtility();
            routingEngine = MockRepository.GenerateMock <IRoutingEngine>();
            var urlBuilder = new DefaultUrlBuilder(server, routingEngine);

            serviceProvider.UrlBuilder = urlBuilder;
            serviceProvider.AddService(typeof(IUrlBuilder), urlBuilder);

            InitUrlInfo("", "home", "index");

            response = engineContext.Response;
        }
        public void Init()
        {
            mocks = new MockRepository();
            serviceProvider = new StubMonoRailServices();

            var viewSourceLoader = new FileAssemblyViewSourceLoader("MonoRail.Tests.Views");
            viewSourceLoader.Service(this.serviceProvider);
            serviceProvider.ViewSourceLoader = viewSourceLoader;
            serviceProvider.AddService(typeof(IViewSourceLoader), viewSourceLoader);

            Configure();

            controllerContext = new ControllerContext();
            propertyBag = controllerContext.PropertyBag;

            controllerContext.LayoutNames = new []{"default"};
            output = new StringWriter();

            server = new StubServerUtility();
            routingEngine = MockRepository.GenerateMock<IRoutingEngine>();
            var urlBuilder = new DefaultUrlBuilder(server, routingEngine);
            serviceProvider.UrlBuilder = urlBuilder;
            serviceProvider.AddService(typeof(IUrlBuilder), urlBuilder);

            InitUrlInfo("", "home", "index");

            response = engineContext.Response;
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="ResponseAdapter"/> class.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <param name="currentUrl">The current URL.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="routeMatch">The route match.</param>
		/// <param name="referrer">The referrer.</param>
		public ResponseAdapter(HttpResponse response, UrlInfo currentUrl, 
			IUrlBuilder urlBuilder, IServerUtility serverUtility,
			RouteMatch routeMatch, string referrer)
			: base(currentUrl, urlBuilder, serverUtility, routeMatch, referrer)
		{
			this.response = response;
		}
		public virtual void Setup()
		{
			response = new StubResponse();
			var url = new UrlInfo("eleutian.com", "www", virtualDirectory, "http", 80, 
				Path.Combine(Path.Combine("Area", "Controller"), "Action"), "Area", "Controller", "Action", "rails", "");
			
			var stubEngineContext = new StubEngineContext(new StubRequest(), response, new StubMonoRailServices(), url)
			{
				Server = MockRepository.GenerateMock<IServerUtility>()
			};

			railsContext = stubEngineContext;
			serverUtility = railsContext.Server;

			argumentConversionService = MockRepository.GenerateMock<IArgumentConversionService>();

			controller = new TestController();
			controller.Contextualize(railsContext, MockRepository.GenerateStub<IControllerContext>());
			
			parameters = new Hashtable();
			
			services = MockRepository.GenerateMock<ICodeGeneratorServices>();
			services.Expect(s => s.ArgumentConversionService).Return(argumentConversionService).Repeat.Any();
			services.Expect(s => s.Controller).Return(controller).Repeat.Any();
			services.Expect(s => s.RailsContext).Return(railsContext).Repeat.Any();
			argumentConversionService.Expect(s => s.CreateParameters()).Return(parameters).Repeat.Any();
		}
Exemple #9
0
        public virtual void Setup()
        {
            response = new StubResponse();
            var url = new UrlInfo("eleutian.com", "www", virtualDirectory, "http", 80,
                                  Path.Combine(Path.Combine("Area", "Controller"), "Action"), "Area", "Controller", "Action", "rails", "");

            var stubEngineContext = new StubEngineContext(new StubRequest(), response, new StubMonoRailServices(), url)
            {
                Server = MockRepository.GenerateMock <IServerUtility>()
            };

            railsContext  = stubEngineContext;
            serverUtility = railsContext.Server;

            argumentConversionService = MockRepository.GenerateMock <IArgumentConversionService>();

            controller = new TestController();
            controller.Contextualize(railsContext, MockRepository.GenerateStub <IControllerContext>());

            parameters = new Hashtable();

            services = MockRepository.GenerateMock <ICodeGeneratorServices>();
            services.Expect(s => s.ArgumentConversionService).Return(argumentConversionService).Repeat.Any();
            services.Expect(s => s.Controller).Return(controller).Repeat.Any();
            services.Expect(s => s.RailsContext).Return(railsContext).Repeat.Any();
            argumentConversionService.Expect(s => s.CreateParameters()).Return(parameters).Repeat.Any();
        }
Exemple #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UrlParts"/> class.
        /// </summary>
        /// <param name="serverUtility">The server utility.</param>
        /// <param name="pathPieces">The path pieces.</param>
        public UrlParts(IServerUtility serverUtility, params string[] pathPieces)
        {
            url = new StringBuilder();
            this.serverUtility = serverUtility;

            AppendPaths(pathPieces);
        }
        /// <summary>
        /// Builds a query string.
        /// </summary>
        /// <remarks>
        /// Supports multi-value query strings, using any
        /// <see cref="IEnumerable"/> as a value.
        /// </remarks>
        /// <param name="parameters">The parameters</param>
        /// <param name="serverUtil">The server utility instance</param>
        /// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
        public static string BuildQueryString(IServerUtility serverUtil, NameValueCollection parameters, bool encodeAmp)
        {
            if (parameters == null || parameters.Count == 0)
            {
                return(string.Empty);
            }
            if (serverUtil == null)
            {
                throw new ArgumentNullException("serverUtil");
            }

            var sb = new StringBuilder();

            var    useSeparator = false;
            string anchor       = null;

            foreach (string key in parameters.Keys)
            {
                if (key == null)
                {
                    continue;
                }

                if (key.StartsWith("#") && String.IsNullOrEmpty(parameters[key]))
                {
                    anchor = key;
                    continue;
                }

                foreach (var value in parameters.GetValues(key))
                {
                    if (useSeparator)
                    {
                        if (encodeAmp)
                        {
                            sb.Append("&amp;");
                        }
                        else
                        {
                            sb.Append("&");
                        }
                    }
                    else
                    {
                        useSeparator = true;
                    }

                    sb.Append(serverUtil.UrlEncode(key))
                    .Append('=')
                    .Append(serverUtil.UrlEncode(value));
                }
            }

            if (anchor != null)
            {
                sb.Append(anchor);
            }

            return(sb.ToString());
        }
Exemple #12
0
        public SearchService(IServerUtility utility, string searchDataPath)
        {
            this.LoadData = LoadJsonDataFromDisk;

            this.SearchDataPath = searchDataPath;

            this.ServerUtility = utility;
        }
Exemple #13
0
		/// <summary>
		/// Initializes a new instance of the <see cref="BaseResponse"/> class.
		/// </summary>
		/// <param name="currentUrl">The current URL.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="routeMatch">The route match.</param>
		/// <param name="referrer">The referrer.</param>
		protected BaseResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
		{
			this.currentUrl = currentUrl;
			this.urlBuilder = urlBuilder;
			this.serverUtility = serverUtility;
			this.routeMatch = routeMatch;
			this.referrer = referrer;
		}
Exemple #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseResponse"/> class.
 /// </summary>
 /// <param name="currentUrl">The current URL.</param>
 /// <param name="urlBuilder">The URL builder.</param>
 /// <param name="serverUtility">The server utility.</param>
 /// <param name="routeMatch">The route match.</param>
 /// <param name="referrer">The referrer.</param>
 protected BaseResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
 {
     this.currentUrl    = currentUrl;
     this.urlBuilder    = urlBuilder;
     this.serverUtility = serverUtility;
     this.routeMatch    = routeMatch;
     this.referrer      = referrer;
 }
Exemple #15
0
        /// <summary>
        /// Services the specified provider.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public void Service(IServiceProvider provider)
        {
            var config = (IMonoRailConfiguration)provider.GetService(typeof(IMonoRailConfiguration));

            useExtensions = config.UrlConfig.UseExtensions;

            serverUtil = (IServerUtility)provider.GetService(typeof(IServerUtility));
            routingEng = (IRoutingEngine)provider.GetService(typeof(IRoutingEngine));
        }
		/// <summary>
		/// Sets the controller.
		/// </summary>
		/// <param name="controller">Current view's <see cref="Controller"/>.</param>
		public virtual void SetController(Controller controller)
		{
			this.controller = controller;

			if (controller.Context != null) // It will be null when invoked from test cases
			{
				serverUtility = controller.Context.Server;
			}
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="JSCodeGenerator"/> class.
		/// </summary>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="viewEngineManager">The view engine manager.</param>
		/// <param name="engineContext">The engine context.</param>
		/// <param name="controller">The controller.</param>
		/// <param name="context">The context.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		public JSCodeGenerator(IServerUtility serverUtility, IViewEngineManager viewEngineManager,
		                       IEngineContext engineContext, IController controller, IControllerContext context,
		                       IUrlBuilder urlBuilder)
		{
			this.serverUtility = serverUtility;
			this.viewEngineManager = viewEngineManager;
			this.engineContext = engineContext;
			this.controller = controller;
			this.context = context;
			this.urlBuilder = urlBuilder;
		}
Exemple #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JSCodeGenerator"/> class.
 /// </summary>
 /// <param name="serverUtility">The server utility.</param>
 /// <param name="viewEngineManager">The view engine manager.</param>
 /// <param name="engineContext">The engine context.</param>
 /// <param name="controller">The controller.</param>
 /// <param name="context">The context.</param>
 /// <param name="urlBuilder">The URL builder.</param>
 public JSCodeGenerator(IServerUtility serverUtility, IViewEngineManager viewEngineManager,
                        IEngineContext engineContext, IController controller, IControllerContext context,
                        IUrlBuilder urlBuilder)
 {
     this.ServerUtility     = serverUtility;
     this.viewEngineManager = viewEngineManager;
     this.engineContext     = engineContext;
     this.controller        = controller;
     this.context           = context;
     this.UrlBuilder        = urlBuilder;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultEngineContext"/> class.
		/// </summary>
		/// <param name="container">The container.</param>
		/// <param name="urlInfo">Url information</param>
		/// <param name="context">The context.</param>
		/// <param name="server">The server.</param>
		/// <param name="request">The request.</param>
		/// <param name="response">The response.</param>
		/// <param name="trace">The trace.</param>
		/// <param name="session">The session.</param>
		public DefaultEngineContext(IMonoRailContainer container, UrlInfo urlInfo, HttpContext context, IServerUtility server,  IRequest request, IResponse response, ITrace trace,  IDictionary session)
			: base(container)
		{
			this.container = container;
			this.UnderlyingContext = context;
			this.UrlInfo = urlInfo;
			this.Request = request;
			this.Response = response;
			this.Session = session;
			this.Server = server;
			this.Trace = trace;
		}
Exemple #20
0
        private static UrlParts CreateForAbsolutePath(IServerUtility serverUtility, Uri uri)
        {
            var host = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.PathAndQuery.Length);

            var parts = new UrlParts(serverUtility, host);

            foreach (var segment in uri.Segments)
            {
                parts.AppendPath(segment);
            }

            parts.ConvertPathInfoToDict();
            parts.SetQueryString(uri.Query);

            return(parts);
        }
Exemple #21
0
        /// <summary>
        /// Pendent
        /// </summary>
        /// <param name="serverUtility">The server utility.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static UrlParts Parse(IServerUtility serverUtility, string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            var uri = new Uri(url, UriKind.RelativeOrAbsolute);

            if (uri.IsAbsoluteUri)
            {
                return(CreateForAbsolutePath(serverUtility, uri));
            }
            else
            {
                return(CreateForRelativePath(serverUtility, url));
            }
        }
Exemple #22
0
		/// <summary>
		/// Pendent
		/// </summary>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="url">The URL.</param>
		/// <returns></returns>
		public static UrlParts Parse(IServerUtility serverUtility, string url)
		{
			if (url == null)
			{
				throw new ArgumentNullException("url");
			}

			Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

			if (uri.IsAbsoluteUri)
			{
				return CreateForAbsolutePath(serverUtility, uri);
			}
			else
			{
				return CreateForRelativePath(serverUtility, url);
			}
		}
Exemple #23
0
        public virtual void Setup()
        {
            _mocks    = new MockRepository();
            _services = _mocks.DynamicMock <ICodeGeneratorServices>();
            _response = new StubResponse();
            UrlInfo url =
                new UrlInfo("eleutian.com", "www", _virtualDirectory, "http", 80,
                            Path.Combine(Path.Combine("Area", "Controller"), "Action"), "Area", "Controller", "Action", "rails",
                            "");

            _railsContext =
                new StubEngineContext(new StubRequest(), _response, new StubMonoRailServices(), url);

            ((StubEngineContext)_railsContext).Server = _mocks.DynamicMock <IServerUtility>();
            _serverUtility = _railsContext.Server;

            _argumentConversionService = _mocks.DynamicMock <IArgumentConversionService>();
            _controller = new TestController();
        }
Exemple #24
0
        private static string BuildQueryString(IServerUtility serverUtil, NameValueCollection parameters, bool encodeAmp)
        {
            string queryString = CommonUtils.BuildQueryString(serverUtil, parameters, encodeAmp);

            if (encodeAmp)
            {
                if (queryString.EndsWith(encodedAmp))
                {
                    queryString = queryString.Substring(0, queryString.Length - 5);
                }
            }
            else
            {
                if (queryString.EndsWith(amp))
                {
                    queryString = queryString.Substring(0, queryString.Length - 1);
                }
            }
            return(queryString);
        }
		/// <summary>
		/// Sets the context.
		/// </summary>
		/// <param name="context">The context.</param>
		public virtual void SetContext(IEngineContext context)
		{
			this.context = context;
			serverUtility = context.Server;
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="StubResponse"/> class.
 /// </summary>
 /// <param name="currentUrl">The current URL.</param>
 /// <param name="urlBuilder">The URL builder.</param>
 /// <param name="serverUtility">The server utility.</param>
 /// <param name="routeMatch">The route match.</param>
 /// <param name="referrer">The referrer.</param>
 public StubResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
     : base(currentUrl, urlBuilder, serverUtility, routeMatch, referrer)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StubResponse"/> class.
 /// </summary>
 /// <param name="currentUrl">The current URL.</param>
 /// <param name="urlBuilder">The URL builder.</param>
 /// <param name="serverUtility">The server utility.</param>
 /// <param name="routeMatch">The route match.</param>
 public StubResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch)
     : this(currentUrl, urlBuilder, serverUtility, routeMatch, null)
 {
 }
		public virtual void Setup()
		{
			_mocks = new MockRepository();
			_services = _mocks.DynamicMock<ICodeGeneratorServices>();
			_response = new StubResponse();
			UrlInfo url =
				new UrlInfo("eleutian.com", "www", _virtualDirectory, "http", 80,
				            Path.Combine(Path.Combine("Area", "Controller"), "Action"), "Area", "Controller", "Action", "rails",
				            "");
			_railsContext =
				new StubEngineContext(new StubRequest(), _response, new StubMonoRailServices(), url);

			((StubEngineContext)_railsContext).Server = _mocks.DynamicMock<IServerUtility>();
			_serverUtility = _railsContext.Server;

			_argumentConversionService = _mocks.DynamicMock<IArgumentConversionService>();
			_controller = new TestController();
		}
Exemple #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultEngineContext"/> class.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="urlInfo">Url information</param>
 /// <param name="context">The context.</param>
 /// <param name="server">The server.</param>
 /// <param name="request">The request.</param>
 /// <param name="response">The response.</param>
 /// <param name="trace">The trace.</param>
 /// <param name="session">The session.</param>
 public DefaultEngineContext(IMonoRailContainer container, UrlInfo urlInfo, HttpContext context, IServerUtility server, IRequest request, IResponse response, ITrace trace, IDictionary session)
     : base(container)
 {
     this.container         = container;
     this.UnderlyingContext = context;
     this.UrlInfo           = urlInfo;
     this.Request           = request;
     this.Response          = response;
     this.Session           = session;
     this.Server            = server;
     this.Trace             = trace;
 }
        /// <summary>
        /// Builds a query string.
        /// </summary>
        /// <remarks>
        /// Supports multi-value query strings, using any
        /// <see cref="IEnumerable"/> as a value.
        /// <example>
        ///	<code>
        /// IDictionary dict = new Hashtable();
        /// dict.Add("id", 5);
        /// dict.Add("selectedItem", new int[] { 2, 4, 99 });
        /// string querystring = BuildQueryString(dict);
        /// // should result in: "id=5&amp;selectedItem=2&amp;selectedItem=4&amp;selectedItem=99&amp;"
        /// </code>
        /// </example>
        /// </remarks>
        /// <param name="parameters">The parameters</param>
        /// <param name="serverUtil">The server utility instance</param>
        /// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
        public static string BuildQueryString(IServerUtility serverUtil, IDictionary parameters, bool encodeAmp)
        {
            if (parameters == null || parameters.Count == 0)
            {
                return(string.Empty);
            }
            if (serverUtil == null)
            {
                throw new ArgumentNullException("serverUtil");
            }

            var singleValueEntry = new Object[1];
            var sb = new StringBuilder();

            var    useSeparator = false;
            string anchor       = null;

            foreach (DictionaryEntry entry in parameters)
            {
                if (entry.Value == null)
                {
                    continue;
                }

                if (entry.Key.ToString().StartsWith("#") && entry.Value.ToString() == String.Empty)
                {
                    anchor = entry.Key.ToString();
                    continue;
                }

                IEnumerable values = singleValueEntry;

                if (!(entry.Value is String) && (entry.Value is IEnumerable))
                {
                    values = (IEnumerable)entry.Value;
                }
                else
                {
                    singleValueEntry[0] = entry.Value;
                }

                foreach (var value in values)
                {
                    if (useSeparator)
                    {
                        if (encodeAmp)
                        {
                            sb.Append("&amp;");
                        }
                        else
                        {
                            sb.Append("&");
                        }
                    }
                    else
                    {
                        useSeparator = true;
                    }

                    var encoded = serverUtil.UrlEncode(Convert.ToString(value, CultureInfo.CurrentCulture));

                    sb.Append(serverUtil.UrlEncode(entry.Key.ToString())).Append('=').Append(encoded);
                }
            }

            if (anchor != null)
            {
                sb.Append(anchor);
            }

            return(sb.ToString());
        }
Exemple #31
0
		private static UrlParts CreateForAbsolutePath(IServerUtility serverUtility, Uri uri)
		{
			string host = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.PathAndQuery.Length);

			UrlParts parts = new UrlParts(serverUtility, host);

			foreach (string segment in uri.Segments)
			{
				parts.AppendPath(segment);
			}

			parts.ConvertPathInfoToDict();
			parts.SetQueryString(uri.Query);

			return parts;
		}
		/// <summary>
		/// Services the specified provider.
		/// </summary>
		/// <param name="provider">The provider.</param>
		public void Service(IServiceProvider provider)
		{
			serverUtil = (IServerUtility) provider.GetService(typeof(IServerUtility));
		}
Exemple #33
0
        public void Init()
        {
            mocks         = new MockRepository();
            factory       = new SparkViewFactory();
            engineContext = mocks.CreateMock <IEngineContext>();
            server        = new MockServerUtility();
            request       = mocks.CreateMock <IRequest>();
            response      = mocks.CreateMock <IResponse>();

            controller        = mocks.CreateMock <IController>();
            controllerContext = mocks.CreateMock <IControllerContext>();
            routingEngine     = mocks.CreateMock <IRoutingEngine>();
            output            = new StringWriter();
            helpers           = new HelperDictionary();

            propertyBag   = new Dictionary <string, object>();
            flash         = new Flash();
            session       = new Dictionary <string, object>();
            requestParams = new NameValueCollection();
            contextItems  = new Dictionary <string, object>();


            SetupResult.For(engineContext.Server).Return(server);
            SetupResult.For(engineContext.Request).Return(request);
            SetupResult.For(engineContext.Response).Return(response);
            SetupResult.For(engineContext.CurrentController).Return(controller);
            SetupResult.For(engineContext.CurrentControllerContext).Return(controllerContext);
            SetupResult.For(engineContext.Flash).Return(flash);
            SetupResult.For(engineContext.Session).Return(session);
            SetupResult.For(engineContext.Items).Return(contextItems);

            SetupResult.For(request.Params).Return(requestParams);

            SetupResult.For(controllerContext.LayoutNames).Return(new[] { "default" });
            SetupResult.For(controllerContext.Helpers).Return(helpers);
            SetupResult.For(controllerContext.PropertyBag).Return(propertyBag);

            SetupResult.For(routingEngine.IsEmpty).Return(true);

            var urlBuilder = new DefaultUrlBuilder(server, routingEngine);

            var serviceProvider  = mocks.CreateMock <IServiceProvider>();
            var viewSourceLoader = new FileAssemblyViewSourceLoader("Views");

            SetupResult.For(serviceProvider.GetService(typeof(IViewSourceLoader))).Return(viewSourceLoader);
            SetupResult.For(serviceProvider.GetService(typeof(ILoggerFactory))).Return(new NullLogFactory());
            SetupResult.For(serviceProvider.GetService(typeof(ISparkViewEngine))).Return(null);
            SetupResult.For(serviceProvider.GetService(typeof(IUrlBuilder))).Return(urlBuilder);
            SetupResult.For(serviceProvider.GetService(typeof(IViewComponentFactory))).Return(null);
            mocks.Replay(serviceProvider);

            SetupResult.For(engineContext.GetService(null)).IgnoreArguments().Do(
                new Func <Type, object>(serviceProvider.GetService));

            factory.Service(serviceProvider);


            manager = new DefaultViewEngineManager();
            manager.RegisterEngineForExtesionLookup(factory);
            manager.RegisterEngineForView(factory);
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="StubResponse"/> class.
		/// </summary>
		/// <param name="currentUrl">The current URL.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="routeMatch">The route match.</param>
		/// <param name="referrer">The referrer.</param>
		public StubResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
			: base(currentUrl, urlBuilder, serverUtility, routeMatch, referrer)
		{
		}
		/// <summary>
		/// Builds a query string.
		/// </summary>
		/// <remarks>
		/// Supports multi-value query strings, using any
		/// <see cref="IEnumerable"/> as a value.
		/// <example>
		///	<code>
		/// IDictionary dict = new Hashtable();
		/// dict.Add("id", 5);
		/// dict.Add("selectedItem", new int[] { 2, 4, 99 });
		/// string querystring = BuildQueryString(dict);
		/// // should result in: "id=5&amp;selectedItem=2&amp;selectedItem=4&amp;selectedItem=99&amp;"
		/// </code>
		/// </example>
		/// </remarks>
		/// <param name="parameters">The parameters</param>
		/// <param name="serverUtil">The server utility instance</param>
		/// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
		public static string BuildQueryString(IServerUtility serverUtil, IDictionary parameters, bool encodeAmp)
		{
			if (parameters == null || parameters.Count == 0) return string.Empty;
			if (serverUtil == null) throw new ArgumentNullException("serverUtil");

			Object[] singleValueEntry = new Object[1];
			StringBuilder sb = new StringBuilder();

			bool useSeparator = false;
			string anchor = null;

			foreach(DictionaryEntry entry in parameters)
			{
				if (entry.Value == null) continue;

				if (entry.Key.ToString().StartsWith("#") && entry.Value.ToString() == String.Empty)
				{
					anchor = entry.Key.ToString();
					continue;
				}

				IEnumerable values = singleValueEntry;

				if (!(entry.Value is String) && (entry.Value is IEnumerable))
				{
					values = (IEnumerable) entry.Value;
				}
				else
				{
					singleValueEntry[0] = entry.Value;
				}

				foreach(object value in values)
				{
					if (useSeparator)
					{
						if (encodeAmp)
						{
							sb.Append("&amp;");
						}
						else
						{
							sb.Append("&");
						}
					}
					else
					{
						useSeparator = true;
					}

					string encoded = serverUtil.UrlEncode(Convert.ToString(value, CultureInfo.CurrentCulture));

					sb.Append(serverUtil.UrlEncode(entry.Key.ToString())).Append('=').Append(encoded);
				}
			}

			if (anchor != null)
			{
				sb.Append(anchor);
			}

			return sb.ToString();
		}
		/// <summary>
		/// Builds a query string.
		/// </summary>
		/// <remarks>
		/// Supports multi-value query strings, using any
		/// <see cref="IEnumerable"/> as a value.
		/// </remarks>
		/// <param name="parameters">The parameters</param>
		/// <param name="serverUtil">The server utility instance</param>
		/// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
		public static string BuildQueryString(IServerUtility serverUtil, NameValueCollection parameters, bool encodeAmp)
		{
			if (parameters == null || parameters.Count == 0) return string.Empty;
			if (serverUtil == null) throw new ArgumentNullException("serverUtil");

			StringBuilder sb = new StringBuilder();

			bool useSeparator = false;
			string anchor = null;

			foreach (string key in parameters.Keys)
			{
				if (key == null) continue;

				if (key.StartsWith("#") && String.IsNullOrEmpty(parameters[key]))
				{
					anchor = key;
					continue;
				}

				foreach (string value in parameters.GetValues(key))
				{
					if (useSeparator)
					{
						if (encodeAmp)
						{
							sb.Append("&amp;");
						}
						else
						{
							sb.Append("&");
						}
					}
					else
					{
						useSeparator = true;
					}

					sb.Append(serverUtil.UrlEncode(key))
						.Append('=')
						.Append(serverUtil.UrlEncode(value));
				}
			}

			if (anchor != null)
			{
				sb.Append(anchor);
			}

			return sb.ToString();
		}
Exemple #37
0
		/// <summary>
		/// Builds the path.
		/// </summary>
		/// <returns></returns>
		public string BuildPathForLink(IServerUtility serverUtiliy)
		{
			StringBuilder sb = new StringBuilder(url.ToString());

			BuildPathInfo(sb);

			if (queryStringDict != null && queryStringDict.Count != 0)
			{
				sb.Append('?');
				sb.Append(CommonUtils.BuildQueryString(serverUtiliy, QueryString, true));
			}
			else if (!string.IsNullOrEmpty(queryString))
			{
				sb.Append('?');
				sb.Append(serverUtiliy.HtmlEncode(queryString));
			}

			return sb.ToString();
		}
Exemple #38
0
		private static UrlParts CreateForRelativePath(IServerUtility serverUtility, string url)
		{
			string path = url;
			string qs = null;
			string pathInfo = null;

			int queryStringStartIndex = url.IndexOf('?');

			if (queryStringStartIndex != -1)
			{
				qs = url.Substring(queryStringStartIndex);
				path = url.Substring(0, queryStringStartIndex);
			}

			int fileExtIndex = path.IndexOf('.');

			if (fileExtIndex != -1)
			{
				int pathInfoStartIndex = path.IndexOf('/', fileExtIndex);

				if (pathInfoStartIndex != -1)
				{
					pathInfo = path.Substring(pathInfoStartIndex);
					path = path.Substring(0, pathInfoStartIndex);
				}
			}

			UrlParts parts = new UrlParts(serverUtility, path);
			parts.SetQueryString(qs);
			parts.PathInfoDict.Parse(pathInfo);

			return parts;
		}
 private static string BuildQueryString(IServerUtility serverUtil, NameValueCollection parameters, bool encodeAmp)
 {
     string queryString = CommonUtils.BuildQueryString(serverUtil, parameters, encodeAmp);
     if (encodeAmp)
     {
         if (queryString.EndsWith(encodedAmp))
         {
             queryString = queryString.Substring(0, queryString.Length - 5);
         }
     }
     else
     {
         if (queryString.EndsWith(amp))
         {
             queryString = queryString.Substring(0, queryString.Length - 1);
         }
     }
     return queryString;
 }
Exemple #40
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultUrlBuilder"/> class.
 /// </summary>
 /// <param name="serverUtil">The server util.</param>
 /// <param name="routingEng">The routing eng.</param>
 public DefaultUrlBuilder(IServerUtility serverUtil, IRoutingEngine routingEng)
 {
     this.serverUtil = serverUtil;
     this.routingEng = routingEng;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="StubResponse"/> class.
		/// </summary>
		/// <param name="currentUrl">The current URL.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		/// <param name="serverUtility">The server utility.</param>
		/// <param name="routeMatch">The route match.</param>
		public StubResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch)
			: this(currentUrl, urlBuilder, serverUtility, routeMatch, null)
		{
		}
Exemple #42
0
 /// <summary>
 /// Sets the context.
 /// </summary>
 /// <param name="context">The context.</param>
 public virtual void SetContext(IEngineContext context)
 {
     this.context  = context;
     serverUtility = context.Server;
 }