From 0cf2ac7ee2d43abda3ff22e31c77a7562592c71e Mon Sep 17 00:00:00 2001 From: David Reinhold Date: Wed, 9 May 2018 16:50:17 +0200 Subject: [PATCH] Updated driver to also throw exception if it receives an error message or code from the wkhtmltopdf-process --- Rotativa.Demo/Controllers/HomeController.cs | 15 ++++++ Rotativa.Demo/Views/Home/Index.cshtml | 51 +++++++++++---------- Rotativa.Tests/RotativaTests.cs | 35 ++++++++++++++ Rotativa/WkhtmlDriver.cs | 2 +- 4 files changed, 78 insertions(+), 25 deletions(-) diff --git a/Rotativa.Demo/Controllers/HomeController.cs b/Rotativa.Demo/Controllers/HomeController.cs index 39f121f..a6fd892 100644 --- a/Rotativa.Demo/Controllers/HomeController.cs +++ b/Rotativa.Demo/Controllers/HomeController.cs @@ -160,6 +160,21 @@ public ActionResult ErrorTest() return new ActionAsPdf("SomethingBad") { FileName = "Test.pdf" }; } + public ActionResult HttpStatus500Test() + { + return new UrlAsPdf("https://httpstat.us/500") { FileName = "Test.pdf" }; + + } + public ActionResult HttpStatus404Test() + { + return new UrlAsPdf("https://httpstat.us/404") { FileName = "Test.pdf" }; + } + public ActionResult HttpStatus200Test() + { + return new UrlAsPdf("https://httpstat.us/200") { FileName = "Test.pdf" }; + + } + public ActionResult SomethingBad() { return Redirect("http://thisdoesntexists"); diff --git a/Rotativa.Demo/Views/Home/Index.cshtml b/Rotativa.Demo/Views/Home/Index.cshtml index 8000b1f..ecd4d6d 100644 --- a/Rotativa.Demo/Views/Home/Index.cshtml +++ b/Rotativa.Demo/Views/Home/Index.cshtml @@ -4,30 +4,33 @@

@ViewBag.Message

-

+

diff --git a/Rotativa.Tests/RotativaTests.cs b/Rotativa.Tests/RotativaTests.cs index 3442a36..d54194b 100644 --- a/Rotativa.Tests/RotativaTests.cs +++ b/Rotativa.Tests/RotativaTests.cs @@ -360,5 +360,40 @@ public void Can_print_image_from_page_with_external_css_file() image.RawFormat.Should().Be.EqualTo(ImageFormat.Jpeg); } } + + [Test] + public void HttpError_statuses_does_not_generate_pdf_file() + { + var testLink = selenium.FindElement(By.LinkText("HttpStatus 200 Test")); + var pdfHref = testLink.GetAttribute("href"); + + var content = "200 OK"; + 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(); + } + + testLink = selenium.FindElement(By.LinkText("HttpStatus 404 Test")); + pdfHref = testLink.GetAttribute("href"); + + + using (var wc = new WebClient()) + { + Assert.Throws(typeof(WebException), () => wc.DownloadData(new Uri(pdfHref))); + } + + testLink = selenium.FindElement(By.LinkText("HttpStatus 500 Test")); + pdfHref = testLink.GetAttribute("href"); + + + using (var wc = new WebClient()) + { + Assert.Throws(typeof(WebException), () => wc.DownloadData(new Uri(pdfHref))); + } + } } } diff --git a/Rotativa/WkhtmlDriver.cs b/Rotativa/WkhtmlDriver.cs index a733e91..799a454 100644 --- a/Rotativa/WkhtmlDriver.cs +++ b/Rotativa/WkhtmlDriver.cs @@ -70,7 +70,7 @@ protected static byte[] Convert(string wkhtmlPath, string switches, string html, string error = proc.StandardError.ReadToEnd(); - if (ms.Length == 0) + if (ms.Length == 0 || !string.IsNullOrEmpty(error) || proc.ExitCode > 0) { throw new Exception(error); }