public void CreateFromVirtualPathTest() { var contents = "test"; var textWriter = new StringWriter(); var httpResponse = new Mock <HttpResponseBase>(); httpResponse.SetupGet(r => r.Output).Returns(textWriter); var httpContext = Utils.CreateTestContext(response: httpResponse.Object); var mockBuildManager = new Mock <IVirtualPathFactory>(); var virtualPath = "~/hello/test.cshtml"; var page = Utils.CreatePage(p => p.Write(contents)); mockBuildManager.Setup(c => c.Exists(It.Is <string>(p => p.Equals(virtualPath)))).Returns <string>(_ => true).Verifiable(); mockBuildManager.Setup(c => c.CreateInstance(It.Is <string>(p => p.Equals(virtualPath)))).Returns(page).Verifiable(); // Act IHttpHandler handler = WebPageHttpHandler.CreateFromVirtualPath(virtualPath, new VirtualPathFactoryManager(mockBuildManager.Object)); Assert.IsType <WebPageHttpHandler>(handler); WebPageHttpHandler webPageHttpHandler = (WebPageHttpHandler)handler; webPageHttpHandler.ProcessRequestInternal(httpContext.Object); // Assert Assert.Equal(contents, textWriter.ToString()); mockBuildManager.Verify(); }
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")); }
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 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")); }
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"]); }