public void SetUpSessionStateThrowsIfMultipleSessionStateValueIsInvalid()
        {
            // Arrange
            var page = new PageWithMultipleSesionStateAttributes();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);

            // Act
            Assert.Throws<InvalidOperationException>(() => SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary<Type, SessionStateBehavior?>()),
                "At most one SessionState value can be declared per page.");
        }
        public void SetUpSessionStateThrowsIfSessionStateValueIsInvalid()
        {
            // Arrange
            var page = new Mock<WebPage>(MockBehavior.Strict);
            var startPage = new InvalidSessionState();
            var webPageHttpHandler = new WebPageHttpHandler(page.Object, startPage: new Lazy<WebPageRenderingBase>(() => startPage));
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);

            // Act
            Assert.Throws<ArgumentException>(() => SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary<Type, SessionStateBehavior?>()),
                "Value \"jabberwocky\" specified in \"~/_Invalid.cshtml\" is an invalid value for the SessionState directive. Possible values are: \"Default, Required, ReadOnly, Disabled\".");
        }
Esempio n. 3
0
        public void VersionHeaderTest()
        {
            bool headerSet = false;
            Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>();

            mockResponse.Setup(response => response.AppendHeader("X-AspNetWebPages-Version", "2.0")).Callback(() => headerSet = true);

            Mock <HttpContextBase> mockContext = new Mock <HttpContextBase>();

            mockContext.SetupGet(context => context.Response).Returns(mockResponse.Object);

            WebPageHttpHandler.AddVersionHeader(mockContext.Object);
            Assert.True(headerSet);
        }
        public void SetUpSessionStateUsesSessionStateValueFromRequestingPageIfAvailable()
        {
            // Arrange
            var page = new DisabledSessionWebPage();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);
            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.Disabled)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary<Type, SessionStateBehavior?>());

            // Assert
            context.Verify();
        }
        public void SetUpSessionStateDoesNotInvokeSessionStateBehaviorIfNoPageHasDirective()
        {
            // Arrange
            var page = new Mock<WebPage>(MockBehavior.Strict);
            var startPage = new Mock<StartPage>(MockBehavior.Strict);
            var webPageHttpHandler = new WebPageHttpHandler(page.Object, startPage: new Lazy<WebPageRenderingBase>(() => startPage.Object));
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary<Type, SessionStateBehavior?>());

            // Assert
            context.Verify(c => c.SetSessionStateBehavior(It.IsAny<SessionStateBehavior>()), Times.Never());
        }
        public void SetUpSessionStateDoesNotInvokeSessionStateBehaviorIfNoPageHasDirective()
        {
            // Arrange
            var page               = new Mock <WebPage>(MockBehavior.Strict);
            var startPage          = new Mock <StartPage>(MockBehavior.Strict);
            var webPageHttpHandler = new WebPageHttpHandler(page.Object, startPage: new Lazy <WebPageRenderingBase>(() => startPage.Object));
            var context            = new Mock <HttpContextBase>(MockBehavior.Strict);

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary <Type, SessionStateBehavior?>());

            // Assert
            context.Verify(c => c.SetSessionStateBehavior(It.IsAny <SessionStateBehavior>()), Times.Never());
        }
Esempio n. 7
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.cshtml
            //     ~/about       -> ~/views/about.cshtml or ~/views/about/index.cshtml
            //     ~/views/about -> ~/views/about.cshtml
            //     ~/xxx         -> ~/views/404.cshtml
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            var basePath = HttpContext.Current.Request.Url.AbsoluteUri;

            if (!basePath.EndsWith("/"))
            {
                basePath += '/';
            }

            requestContext.RouteData.DataTokens.Add("VIRTUAL_PATH", basePath);

            if (filePath == "~/")
            {
                filePath = "~/views/index.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
Esempio n. 8
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
            ViewEngines.Engines.Add(new CSSViewEngine());

            RazorCodeLanguage.Languages.Add("cscss", new CSharpRazorCodeLanguage());
            WebPageHttpHandler.RegisterExtension("cscss");

            MvcHandler.DisableMvcResponseHeader = true;
        }
Esempio n. 9
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RazorCodeLanguage.Languages.Add("cslog", new CSharpRazorCodeLanguage());
            RazorCodeLanguage.Languages.Add("cspdf", new CSharpRazorCodeLanguage());
            RazorCodeLanguage.Languages.Add("csmail", new CSharpRazorCodeLanguage());

            WebPageHttpHandler.RegisterExtension("cslog");
            WebPageHttpHandler.RegisterExtension("cspdf");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            RegisterViewEngines(ViewEngines.Engines);
        }
Esempio n. 10
0
        public void SetUpSessionStateUsesSessionStateValueFromRequestingPageIfAvailable()
        {
            // Arrange
            var page = new DisabledSessionWebPage();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context            = new Mock <HttpContextBase>(MockBehavior.Strict);

            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.Disabled)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary <Type, SessionStateBehavior?>());

            // Assert
            context.Verify();
        }
Esempio n. 11
0
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);


            //---------------------------------------------------------
            //for Ionic App
            //---------------------------------------------------------
            ViewEngines.Engines.Add(new IonicAppViewEngine());
            RazorCodeLanguage.Languages.Add("html", new CSharpRazorCodeLanguage());
            WebPageHttpHandler.RegisterExtension("html");
            //---------------------------------------------------------
        }
Esempio n. 12
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
            ViewEngines.Engines.Add(new CSSViewEngine());

            RazorCodeLanguage.Languages.Add("cscss", new CSharpRazorCodeLanguage());
            WebPageHttpHandler.RegisterExtension("cscss");

            MvcHandler.DisableMvcResponseHeader = true;

            new LogHandler().WriteLog(LogType.Information, "Website Started Up", string.Empty);
        }
Esempio n. 13
0
        public void CreateFromVirtualPathNonWebPageTest()
        {
            // Arrange
            var virtualPath      = "~/hello/test.cshtml";
            var handler          = new WebPageHttpHandler(new DummyPage());
            var mockBuildManager = new Mock <IVirtualPathFactory>();

            mockBuildManager.Setup(c => c.CreateInstance(It.IsAny <string>())).Returns(handler);
            mockBuildManager.Setup(c => c.Exists(It.Is <string>(p => p.Equals(virtualPath)))).Returns <string>(_ => true).Verifiable();

            // Act
            var result = WebPageHttpHandler.CreateFromVirtualPath(virtualPath, new VirtualPathFactoryManager(mockBuildManager.Object));

            // Assert
            Assert.Equal(handler, result);
            mockBuildManager.Verify();
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.cshtml
            //     ~/about       -> ~/views/about.cshtml or ~/views/about/index.cshtml
            //     ~/views/about -> ~/views/about.cshtml
            //     ~/xxx         -> ~/views/404.cshtml
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            //if (filePath.ToLower().Contains("api/"))
            //{
            //    HttpControllerRouteHandler routeHandler = ((System.Web.Routing.Route)(requestContext.RouteData.Route)).RouteHandler as HttpControllerRouteHandler;
            //}

            if (filePath == "~/")
            {
                filePath = "~/views/index.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
Esempio n. 15
0
        public void SetUpSessionStateUsesCache()
        {
            // Arrange
            var page = new PageWithBadAttribute();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context            = new Mock <HttpContextBase>(MockBehavior.Strict);
            var dictionary         = new ConcurrentDictionary <Type, SessionStateBehavior?>();

            dictionary.TryAdd(webPageHttpHandler.GetType(), SessionStateBehavior.Default);
            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.Default)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, dictionary);

            // Assert
            context.Verify();
            Assert.Throws <Exception>(() => page.GetType().GetCustomAttributes(inherit: false), "Can't call me!");
        }
Esempio n. 16
0
        public void SetUpSessionStateThrowsIfMultipleSessionStateValueIsInvalid()
        {
            // Arrange
            var page = new PageWithMultipleSesionStateAttributes();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context            = new Mock <HttpContextBase>(MockBehavior.Strict);

            // Act
            Assert.Throws <InvalidOperationException>(
                () =>
                SessionStateUtil.SetUpSessionState(
                    context.Object,
                    webPageHttpHandler,
                    new ConcurrentDictionary <Type, SessionStateBehavior?>()
                    ),
                "At most one SessionState value can be declared per page."
                );
        }
        public void SetUpSessionStateUsesSessionStateValueFromStartPageHierarchy()
        {
            // Arrange
            var page = new Mock<WebPage>(MockBehavior.Strict);
            var startPage = new DefaultSessionWebPage
            {
                ChildPage = new ReadOnlySessionWebPage()
            };
            var webPageHttpHandler = new WebPageHttpHandler(page.Object, startPage: new Lazy<WebPageRenderingBase>(() => startPage));
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);
            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.ReadOnly)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary<Type, SessionStateBehavior?>());

            // Assert
            context.Verify();
        }
Esempio n. 18
0
        public void SetUpSessionStateUsesSessionStateValueFromStartPageHierarchy()
        {
            // Arrange
            var page      = new Mock <WebPage>(MockBehavior.Strict);
            var startPage = new DefaultSessionWebPage
            {
                ChildPage = new ReadOnlySessionWebPage()
            };
            var webPageHttpHandler = new WebPageHttpHandler(page.Object, startPage: new Lazy <WebPageRenderingBase>(() => startPage));
            var context            = new Mock <HttpContextBase>(MockBehavior.Strict);

            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.ReadOnly)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, new ConcurrentDictionary <Type, SessionStateBehavior?>());

            // Assert
            context.Verify();
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.html
            //     ~/about       -> ~/views/about.html or ~/views/about/index.html
            //     ~/views/about -> ~/views/about.html
            //     ~/xxx         -> ~/views/404.html
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/views/index.html";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".html";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .html file wasn't found

            if (handler == null)
            {
                // case a página requisitada não exista (partial html ou 404), retornar o index e deixar a SPA tratar roteamento
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/index");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/index.html");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(2));
            }

            return(handler);
        }
        public void VersionHeaderTest()
        {
            Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>();

            mockResponse
            .Setup(
                response =>
                response.AppendHeader(
                    "X-AspNetWebPages-Version",
                    LatestRazorVersion.MajorMinor
                    )
                )
            .Verifiable();

            Mock <HttpContextBase> mockContext = new Mock <HttpContextBase>();

            mockContext.SetupGet(context => context.Response).Returns(mockResponse.Object);

            WebPageHttpHandler.AddVersionHeader(mockContext.Object);
            mockResponse.Verify();
        }
Esempio n. 21
0
        public void CreateFromVirtualPathReturnsIHttpHandlerIfItCannotCreateAWebPageType()
        {
            // Arrange
            var pageVirtualPath    = "~/hello/test.cshtml";
            var handlerVirtualPath = "~/handler.asmx";
            var page        = new DummyPage();
            var handler     = new Mock <IHttpHandler>().Object;
            var mockFactory = new Mock <IVirtualPathFactory>();

            mockFactory.Setup(c => c.Exists(It.IsAny <string>())).Returns(true);
            mockFactory.Setup(c => c.CreateInstance(pageVirtualPath)).Returns(page);
            mockFactory.Setup(c => c.CreateInstance(handlerVirtualPath)).Returns(handler);

            // Act
            var handlerResult = WebPageHttpHandler.CreateFromVirtualPath(handlerVirtualPath, mockFactory.Object);
            var pageResult    = WebPageHttpHandler.CreateFromVirtualPath(pageVirtualPath, mockFactory.Object);

            // Assert
            Assert.Equal(handler, handlerResult);
            Assert.NotNull(pageResult as WebPageHttpHandler);
        }
Esempio n. 22
0
        public void HttpHandlerGeneratesSourceFilesHeadersIfRequestIsLocal()
        {
            // Arrange
            string pagePath = "~/index.cshtml", layoutPath = "~/Layout.cshtml", layoutPageName = "Layout.cshtml";
            var    page       = Utils.CreatePage(p => { p.Layout = layoutPageName; }, pagePath);
            var    layoutPage = Utils.CreatePage(p => { p.RenderBody(); }, layoutPath);

            Utils.AssignObjectFactoriesAndDisplayModeProvider(layoutPage, page);


            var headers = new NameValueCollection();
            var request = Utils.CreateTestRequest(pagePath, pagePath);

            request.Setup(c => c.IsLocal).Returns(true);
            request.Setup(c => c.MapPath(It.IsAny <string>())).Returns <string>(path => path);
            request.Setup(c => c.Cookies).Returns(new HttpCookieCollection());

            var response = new Mock <HttpResponseBase>()
            {
                CallBase = true
            };

            response.SetupGet(r => r.Headers).Returns(headers);
            response.SetupGet(r => r.Output).Returns(TextWriter.Null);
            response.Setup(r => r.AppendHeader(It.IsAny <string>(), It.IsAny <string>())).Callback <string, string>((name, value) => headers.Add(name, value));
            response.Setup(r => r.AddHeader(It.IsAny <string>(), It.IsAny <string>())).Callback <string, string>((name, value) => headers.Add(name, value));
            response.Setup(r => r.Cookies).Returns(new HttpCookieCollection());

            var context = Utils.CreateTestContext(request.Object, response.Object);

            // Act
            var webPageHttpHandler = new WebPageHttpHandler(page);

            webPageHttpHandler.ProcessRequestInternal(context.Object);

            // Assert
            Assert.Equal("3.0", headers[WebPageHttpHandler.WebPagesVersionHeaderName]);
            Assert.Equal("=?UTF-8?B?fi9pbmRleC5jc2h0bWx8fi9MYXlvdXQuY3NodG1s?=", headers["X-SourceFiles"]);
        }
        /// <summary>
        /// Gets the handler for given request.
        /// </summary>
        /// <param name="requestContext">The request.</param>
        /// <returns>The handler for the input request.</returns>
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/index.cshtml";
            }
            else if (filePath == "~/preview/OnJobCreating")
            {
                filePath = "~/Views/NativeConnectorSetup.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "Views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            IHttpHandler handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/Shared/Error.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
Esempio n. 24
0
        public void SetUpSessionStateThrowsIfSessionStateValueIsInvalid()
        {
            // Arrange
            var page               = new Mock <WebPage>(MockBehavior.Strict);
            var startPage          = new InvalidSessionState();
            var webPageHttpHandler = new WebPageHttpHandler(
                page.Object,
                startPage: new Lazy <WebPageRenderingBase>(() => startPage)
                );
            var context = new Mock <HttpContextBase>(MockBehavior.Strict);

            // Act
            Assert.Throws <ArgumentException>(
                () =>
                SessionStateUtil.SetUpSessionState(
                    context.Object,
                    webPageHttpHandler,
                    new ConcurrentDictionary <Type, SessionStateBehavior?>()
                    ),
                "Value \"jabberwocky\" specified in \"~/_Invalid.cshtml\" is an invalid value for the SessionState directive. Possible values are: \"Default, Required, ReadOnly, Disabled\"."
                );
        }
        public void SourceFileHeaderTest()
        {
            // Arrange
            var contents = "test";
            var writer = new StringWriter();

            Mock<HttpResponseBase> httpResponse = new Mock<HttpResponseBase>();
            httpResponse.SetupGet(r => r.Output).Returns(writer);
            Mock<HttpRequestBase> httpRequest = Utils.CreateTestRequest("~/index.cshtml", "~/index.cshtml");
            httpRequest.SetupGet(r => r.IsLocal).Returns(true);
            httpRequest.Setup(r => r.MapPath(It.IsAny<string>())).Returns<string>(p => p);
            Mock<HttpContextBase> context = Utils.CreateTestContext(httpRequest.Object, httpResponse.Object);
            var page = Utils.CreatePage(p => p.Write(contents));

            // Act 
            var webPageHttpHandler = new WebPageHttpHandler(page);
            webPageHttpHandler.ProcessRequestInternal(context.Object);

            // Assert
            Assert.Equal(contents, writer.ToString());
            Assert.Equal(1, page.PageContext.SourceFiles.Count);
            Assert.True(page.PageContext.SourceFiles.Contains("~/index.cshtml"));
        }
Esempio n. 26
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var substitutedVirtualPath = GetSubstitutedVirtualPath(requestContext);
            int index = substitutedVirtualPath.IndexOf('?');

            if (index != -1)
            {
                substitutedVirtualPath = substitutedVirtualPath.Substring(0, index);
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(substitutedVirtualPath);

            Trace.TraceInformation("Routing {0} = {1}", this._virtualPath, handler);

            if (handler != null)
            {
                return(handler);
            }

            return(new MvcHandler(requestContext));

            //return WebPageHttpHandler.CreateFromVirtualPath("~/db/Error.cshtml");
        }
        public void SetUpSessionStateUsesCache()
        {
            // Arrange
            var page = new PageWithBadAttribute();
            var webPageHttpHandler = new WebPageHttpHandler(page, startPage: null);
            var context = new Mock<HttpContextBase>(MockBehavior.Strict);
            var dictionary = new ConcurrentDictionary<Type, SessionStateBehavior?>();
            dictionary.TryAdd(webPageHttpHandler.GetType(), SessionStateBehavior.Default);
            context.Setup(c => c.SetSessionStateBehavior(SessionStateBehavior.Default)).Verifiable();

            // Act
            SessionStateUtil.SetUpSessionState(context.Object, webPageHttpHandler, dictionary);

            // Assert
            context.Verify();
            Assert.Throws<Exception>(() => page.GetType().GetCustomAttributes(inherit: false), "Can't call me!");
        }
Esempio n. 28
0
 public IHttpHandler GetHttpHandler(RequestContext requestContext)
 {
     return(WebPageHttpHandler.CreateFromVirtualPath("~/views/inventory/inventory.cshtml"));
 }
        public void HttpHandlerGeneratesSourceFilesHeadersIfRequestIsLocal()
        {
            // Arrange
            string pagePath = "~/index.cshtml", layoutPath = "~/Layout.cshtml", layoutPageName = "Layout.cshtml";
            var page = Utils.CreatePage(p => { p.Layout = layoutPageName; }, pagePath);
            var layoutPage = Utils.CreatePage(p => { p.RenderBody(); }, layoutPath);
            Utils.AssignObjectFactoriesAndDisplayModeProvider(layoutPage, page);


            var headers = new NameValueCollection();
            var request = Utils.CreateTestRequest(pagePath, pagePath);
            request.Setup(c => c.IsLocal).Returns(true);
            request.Setup(c => c.MapPath(It.IsAny<string>())).Returns<string>(path => path);
            request.Setup(c => c.Cookies).Returns(new HttpCookieCollection());

            var response = new Mock<HttpResponseBase>() { CallBase = true };
            response.SetupGet(r => r.Headers).Returns(headers);
            response.SetupGet(r => r.Output).Returns(TextWriter.Null);
            response.Setup(r => r.AppendHeader(It.IsAny<string>(), It.IsAny<string>())).Callback<string, string>((name, value) => headers.Add(name, value));
            response.Setup(r => r.AddHeader(It.IsAny<string>(), It.IsAny<string>())).Callback<string, string>((name, value) => headers.Add(name, value));
            response.Setup(r => r.Cookies).Returns(new HttpCookieCollection());

            var context = Utils.CreateTestContext(request.Object, response.Object);

            // Act
            var webPageHttpHandler = new WebPageHttpHandler(page);
            webPageHttpHandler.ProcessRequestInternal(context.Object);

            // Assert
            Assert.Equal("3.0", headers[WebPageHttpHandler.WebPagesVersionHeaderName]);
            Assert.Equal("=?UTF-8?B?fi9pbmRleC5jc2h0bWx8fi9MYXlvdXQuY3NodG1s?=", headers["X-SourceFiles"]);
        }
        public void CreateFromVirtualPathNonWebPageTest()
        {
            // Arrange
            var virtualPath = "~/hello/test.cshtml";
            var handler = new WebPageHttpHandler(new DummyPage());
            var mockBuildManager = new Mock<IVirtualPathFactory>();
            mockBuildManager.Setup(c => c.CreateInstance(It.IsAny<string>())).Returns(handler);
            mockBuildManager.Setup(c => c.Exists(It.Is<string>(p => p.Equals(virtualPath)))).Returns<string>(_ => true).Verifiable();

            // Act
            var result = WebPageHttpHandler.CreateFromVirtualPath(virtualPath, new VirtualPathFactoryManager(mockBuildManager.Object));

            // Assert
            Assert.Equal(handler, result);
            mockBuildManager.Verify();
        }
Esempio n. 31
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/views/SCPages/SCIndex.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/SCPages/", StringComparison.OrdinalIgnoreCase))
                {
                    if (filePath.StartsWith("~/portfolio-detail/ShaligramInfotechAPI/api/PortfolioApi/", StringComparison.OrdinalIgnoreCase))
                    {
                        filePath = filePath.Replace("portfolio-detail/", "");
                    }
                    else
                    {
                        if (filePath.StartsWith("~/portfolio-detail", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/GetParticularPortfolioDetail";
                        }
                        else if (filePath.Equals("~/subscribe", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/Subscription";
                        }
                        else if (filePath.Equals("~/portfolio", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/portfolio-index";
                        }
                        else if (filePath.Equals("~/portfolio/true", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/portfolio-index";
                        }
                        else if (filePath.StartsWith("~/blog", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/BlogPost";
                        }
                        else if (filePath.Equals("~/pricing-comparison-uk", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonuk";
                        }
                        else if (filePath.Equals("~/pricing-comparison-aus", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonaus";
                        }
                        else if (filePath.Equals("~/pricing-comparison-us", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonus";
                        }
                        else
                        {
                            filePath = filePath.Insert(2, "views/SCPages/");
                        }
                    }
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
 public void CreateFromVirtualPathNonWebPageTest() {
     var handler = new WebPageHttpHandler(new DummyPage());
     var result = WebPageHttpHandler.CreateFromVirtualPath("~/hello/test.cshtml",
         (path, type) => handler);
     Assert.AreEqual(handler, result);
 }