diff --git a/Rotativa.Demo/Controllers/HomeController.cs b/Rotativa.Demo/Controllers/HomeController.cs index 9de651f..853dcd3 100644 --- a/Rotativa.Demo/Controllers/HomeController.cs +++ b/Rotativa.Demo/Controllers/HomeController.cs @@ -91,13 +91,13 @@ public ActionResult TestSaveOnServer(string fileName) public ActionResult TestViewWithModel(string id) { - var model = new TestViewModel {DocTitle = id, DocContent = "This is a test"}; + var model = new TestViewModel { DocTitle = id, DocContent = "This is a test" }; return new ViewAsPdf(model); } public ActionResult TestPartialViewWithModel(string id) { - var model = new TestViewModel { DocTitle = id, DocContent = "This is a test" }; + var model = new TestViewModel { DocTitle = id, DocContent = "This is a test with a partial view" }; return new PartialViewAsPdf(model); } diff --git a/Rotativa.Tests/RotativaTests.cs b/Rotativa.Tests/RotativaTests.cs index 5f52806..4c18b48 100644 --- a/Rotativa.Tests/RotativaTests.cs +++ b/Rotativa.Tests/RotativaTests.cs @@ -1,145 +1,162 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Diagnostics; -using System.Linq; -using System.Net; -using System.Text; -using NUnit.Framework; -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using OpenQA.Selenium.Firefox; -using OpenQA.Selenium.IE; -using SharpTestsEx; - -namespace Rotativa.Tests -{ - [TestFixture] - public class RotativaTests - { - private IWebDriver selenium; - private StringBuilder verificationErrors; - - [TestFixtureSetUp] - public void SetupTest() - { - selenium = new FirefoxDriver(); - selenium.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10)); - verificationErrors = new StringBuilder(); - } - - [SetUp] - public void TestSetUp() - { - var rotativaDemoUrl = ConfigurationManager.AppSettings["RotativaDemoUrl"]; - selenium.Navigate().GoToUrl(rotativaDemoUrl); - } - - [TestFixtureTearDown] - public void FixtureTearDown() - { - if (selenium != null) selenium.Close(); - } - - [Test] - public void Is_the_site_reachable() - { - Assert.AreEqual("Home Page", selenium.Title); - } - - [Test] - public void Can_print_the_test_pdf() - { - - var testLink = selenium.FindElement(By.LinkText("Test")); - var pdfHref = testLink.GetAttribute("href"); - using (var wc = new WebClient()) - { - var pdfResult = wc.DownloadData(new Uri(pdfHref)); - var pdfTester = new PdfTester(); - pdfTester.LoadPdf(pdfResult); - pdfTester.PdfIsValid.Should().Be.True(); - pdfTester.PdfContains("My MVC Application").Should().Be.True(); - } - } - - [Test] - public void Can_print_the_authorized_pdf() - { - - var testLink = selenium.FindElement(By.LinkText("Logged In Test")); - var pdfHref = testLink.GetAttribute("href"); - testLink.Click(); - var username = selenium.FindElement(By.Id("UserName")); - username.SendKeys("admin"); - var password = selenium.FindElement(By.Id("Password")); - password.SendKeys("admin"); - password.Submit(); - var manage = selenium.Manage(); - var cookies = manage.Cookies.AllCookies; - using (var wc = new WebClient()) - { - foreach (var cookie in cookies) - { - var cookieText = cookie.Name + "=" + cookie.Value; - wc.Headers.Add(HttpRequestHeader.Cookie, cookieText); - } - var pdfResult = wc.DownloadData(new Uri(pdfHref)); - var pdfTester = new PdfTester(); - pdfTester.LoadPdf(pdfResult); - pdfTester.PdfIsValid.Should().Be.True(); - pdfTester.PdfContains("My MVC Application").Should().Be.True(); - pdfTester.PdfContains("admin").Should().Be.True(); - } - } - - [Test] - public void Can_print_the_pdf_from_a_view() - { - - var testLink = selenium.FindElement(By.LinkText("Test View")); - var pdfHref = testLink.GetAttribute("href"); - using (var wc = new WebClient()) - { - var pdfResult = wc.DownloadData(new Uri(pdfHref)); - var pdfTester = new PdfTester(); - pdfTester.LoadPdf(pdfResult); - pdfTester.PdfIsValid.Should().Be.True(); - pdfTester.PdfContains("My MVC Application").Should().Be.True(); - } - } - - [Test] - public void Can_print_the_pdf_from_a_vie_with_non_ascii_chars() - { - - var testLink = selenium.FindElement(By.LinkText("Test View")); - var pdfHref = testLink.GetAttribute("href"); - using (var wc = new WebClient()) - { - var pdfResult = wc.DownloadData(new Uri(pdfHref)); - var pdfTester = new PdfTester(); - pdfTester.LoadPdf(pdfResult); - pdfTester.PdfIsValid.Should().Be.True(); - pdfTester.PdfContains("àéù").Should().Be.True(); - } - } - - [Test] - public void Can_print_the_pdf_from_a_view_with_a_model() - { - - var testLink = selenium.FindElement(By.LinkText("Test ViewAsPdf with a model")); - var pdfHref = testLink.GetAttribute("href"); - var title = "This is my test!"; - using (var wc = new WebClient()) - { - var pdfResult = wc.DownloadData(new Uri(pdfHref + "/" + title)); - var pdfTester = new PdfTester(); - pdfTester.LoadPdf(pdfResult); - pdfTester.PdfIsValid.Should().Be.True(); - pdfTester.PdfContains(title).Should().Be.True(); - } - } - } -} +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Text; +using NUnit.Framework; +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Firefox; +using OpenQA.Selenium.IE; +using SharpTestsEx; + +namespace Rotativa.Tests +{ + [TestFixture] + public class RotativaTests + { + private IWebDriver selenium; + private StringBuilder verificationErrors; + + [TestFixtureSetUp] + public void SetupTest() + { + selenium = new FirefoxDriver(); + selenium.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10)); + verificationErrors = new StringBuilder(); + } + + [SetUp] + public void TestSetUp() + { + var rotativaDemoUrl = ConfigurationManager.AppSettings["RotativaDemoUrl"]; + selenium.Navigate().GoToUrl(rotativaDemoUrl); + } + + [TestFixtureTearDown] + public void FixtureTearDown() + { + if (selenium != null) selenium.Close(); + } + + [Test] + public void Is_the_site_reachable() + { + Assert.AreEqual("Home Page", selenium.Title); + } + + [Test] + public void Can_print_the_test_pdf() + { + + var testLink = selenium.FindElement(By.LinkText("Test")); + var pdfHref = testLink.GetAttribute("href"); + using (var wc = new WebClient()) + { + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains("My MVC Application").Should().Be.True(); + } + } + + [Test] + public void Can_print_the_authorized_pdf() + { + + var testLink = selenium.FindElement(By.LinkText("Logged In Test")); + var pdfHref = testLink.GetAttribute("href"); + testLink.Click(); + var username = selenium.FindElement(By.Id("UserName")); + username.SendKeys("admin"); + var password = selenium.FindElement(By.Id("Password")); + password.SendKeys("admin"); + password.Submit(); + var manage = selenium.Manage(); + var cookies = manage.Cookies.AllCookies; + using (var wc = new WebClient()) + { + foreach (var cookie in cookies) + { + var cookieText = cookie.Name + "=" + cookie.Value; + wc.Headers.Add(HttpRequestHeader.Cookie, cookieText); + } + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains("My MVC Application").Should().Be.True(); + pdfTester.PdfContains("admin").Should().Be.True(); + } + } + + [Test] + public void Can_print_the_pdf_from_a_view() + { + + var testLink = selenium.FindElement(By.LinkText("Test View")); + var pdfHref = testLink.GetAttribute("href"); + using (var wc = new WebClient()) + { + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains("My MVC Application").Should().Be.True(); + } + } + + [Test] + public void Can_print_the_pdf_from_a_vie_with_non_ascii_chars() + { + + var testLink = selenium.FindElement(By.LinkText("Test View")); + var pdfHref = testLink.GetAttribute("href"); + using (var wc = new WebClient()) + { + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains("àéù").Should().Be.True(); + } + } + + [Test] + public void Can_print_the_pdf_from_a_view_with_a_model() + { + + var testLink = selenium.FindElement(By.LinkText("Test ViewAsPdf with a model")); + var pdfHref = testLink.GetAttribute("href"); + var title = "This is a test"; + using (var wc = new WebClient()) + { + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains(title).Should().Be.True(); + } + } + + [Test] + public void Can_print_the_pdf_from_a_partial_view_with_a_model() + { + + var testLink = selenium.FindElement(By.LinkText("Test PartialViewAsPdf with a model")); + var pdfHref = testLink.GetAttribute("href"); + var content = "This is a test with a partial view"; + using (var wc = new WebClient()) + { + var pdfResult = wc.DownloadData(new Uri(pdfHref)); + var pdfTester = new PdfTester(); + pdfTester.LoadPdf(pdfResult); + pdfTester.PdfIsValid.Should().Be.True(); + pdfTester.PdfContains(content).Should().Be.True(); + } + } + } +} diff --git a/Rotativa/PartialViewAsPdf.cs b/Rotativa/PartialViewAsPdf.cs index 979f88f..e29b95f 100644 --- a/Rotativa/PartialViewAsPdf.cs +++ b/Rotativa/PartialViewAsPdf.cs @@ -26,50 +26,10 @@ public PartialViewAsPdf(string partialViewName, object model) : base(partialViewName, model) { } - - protected override byte[] CallTheDriver(ControllerContext context) + + protected override ViewEngineResult GetView(ControllerContext context, string viewName, string masterName) { - context.Controller.ViewData.Model = Model; - - // use action name if the view name was not provided - if (string.IsNullOrEmpty(ViewName)) - ViewName = context.RouteData.GetRequiredString("action"); - - using (var sw = new StringWriter()) - { - ViewEngineResult partialViewResult = ViewEngines.Engines.FindPartialView(context, ViewName); - - // view not found, throw an exception with searched locations - if (partialViewResult.View == null) - { - var locations = new StringBuilder(); - locations.AppendLine(); - - foreach (string location in partialViewResult.SearchedLocations) - { - locations.AppendLine(location); - } - - throw new InvalidOperationException( - string.Format("The partial view '{0}' was not found, searched locations: {1}", ViewName, - locations)); - } - - var viewContext = new ViewContext(context, partialViewResult.View, context.Controller.ViewData, - context.Controller.TempData, sw); - partialViewResult.View.Render(viewContext, sw); - - StringBuilder html = sw.GetStringBuilder(); - - // replace href and src attributes with full URLs - string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, - HttpContext.Current.Request.Url.Authority); - html.Replace(" href=\"/", string.Format(" href=\"{0}/", baseUrl)); - html.Replace(" src=\"/", string.Format(" src=\"{0}/", baseUrl)); - - var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString()); - return fileContent; - } + return ViewEngines.Engines.FindPartialView(context, ViewName); } } } \ No newline at end of file diff --git a/Rotativa/ViewAsPdf.cs b/Rotativa/ViewAsPdf.cs index ea613db..e191bcf 100644 --- a/Rotativa/ViewAsPdf.cs +++ b/Rotativa/ViewAsPdf.cs @@ -63,6 +63,11 @@ protected override string GetUrl(ControllerContext context) { return string.Empty; } + + protected virtual ViewEngineResult GetView(ControllerContext context, string viewName, string masterName) + { + return ViewEngines.Engines.FindView(context, ViewName, MasterName); + } protected override byte[] CallTheDriver(ControllerContext context) { @@ -74,7 +79,7 @@ protected override byte[] CallTheDriver(ControllerContext context) using (var sw = new StringWriter()) { - ViewEngineResult viewResult = ViewEngines.Engines.FindView(context, ViewName, MasterName); + ViewEngineResult viewResult = GetView(context, ViewName, MasterName); // view not found, throw an exception with searched locations if (viewResult.View == null)