Permalink
Browse files

Added PartialViewAsPdf

  • Loading branch information...
1 parent bb867bf commit 00db69bbbe05dea87de463f2f08ab4557a78aa55 @webgio committed Dec 7, 2012
Showing with 173 additions and 191 deletions.
  1. +2 −2 Rotativa.Demo/Controllers/HomeController.cs
  2. +162 −145 Rotativa.Tests/RotativaTests.cs
  3. +3 −43 Rotativa/PartialViewAsPdf.cs
  4. +6 −1 Rotativa/ViewAsPdf.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);
}
@@ -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();
+ }
+ }
+ }
+}
@@ -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);
}
}
}
@@ -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)

0 comments on commit 00db69b

Please sign in to comment.