From 443159031235ea1540d5bbd78fdb79cb487f7dc0 Mon Sep 17 00:00:00 2001 From: "Martin H. Normark" Date: Thu, 17 Dec 2015 00:00:12 +0100 Subject: [PATCH] Test download of CSS from link tags Wraps up #72 --- .../LinkTagCssSourceTests.cs | 7 ++- .../PreMailer.Net.Tests/PreMailerTests.cs | 51 +++++++++++++++++++ .../Downloaders/WebDownloader.cs | 21 +++++++- .../PreMailer.Net/Sources/LinkTagCssSource.cs | 7 +-- 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/PreMailer.Net/PreMailer.Net.Tests/LinkTagCssSourceTests.cs b/PreMailer.Net/PreMailer.Net.Tests/LinkTagCssSourceTests.cs index 87fa289a..f504fd80 100644 --- a/PreMailer.Net/PreMailer.Net.Tests/LinkTagCssSourceTests.cs +++ b/PreMailer.Net/PreMailer.Net.Tests/LinkTagCssSourceTests.cs @@ -12,6 +12,11 @@ public class LinkTagCssSourceTests { private readonly Mock _webDownloader = new Mock(); + public LinkTagCssSourceTests() + { + WebDownloader.SharedDownloader = _webDownloader.Object; + } + [TestMethod] public void ImplementsInterface() { @@ -56,7 +61,7 @@ public void GetCSS_AbsoluteUrlInHref_CallsWebDownloader_WithSpecifiedPath() private LinkTagCssSource CreateSUT(string baseUrl = "http://a.com", string path = "a.css") { var node = CQ.CreateFragment(String.Format("", path)); - var sut = new LinkTagCssSource(node.FirstElement(), new Uri(baseUrl), webDownloader: _webDownloader.Object); + var sut = new LinkTagCssSource(node.FirstElement(), new Uri(baseUrl)); return sut; } diff --git a/PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs b/PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs index 3e7f3880..8a028a39 100644 --- a/PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs +++ b/PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs @@ -1,5 +1,7 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using PreMailer.Net.Downloaders; +using Moq; namespace PreMailer.Net.Tests { @@ -281,5 +283,54 @@ public void AddAnalyticsTags_AddsTagsAndExcludesDomain() .MoveCssInline(); Assert.IsTrue(expected == premailedOutput.Html); } + + [TestMethod] + public void ContainsLinkCssElement_DownloadsCss() + { + var mockDownloader = new Mock(); + mockDownloader.Setup(d => d.DownloadString(It.IsAny())).Returns(".a { display: block; }"); + WebDownloader.SharedDownloader = mockDownloader.Object; + + Uri baseUri = new Uri("http://a.com"); + Uri fullUrl = new Uri(baseUri, "b.css"); + string input = String.Format("
test
", fullUrl); + + PreMailer sut = new PreMailer(input, baseUri); + sut.MoveCssInline(); + + mockDownloader.Verify(d => d.DownloadString(fullUrl)); + } + + [TestMethod] + public void ContainsLinkCssElement_NotCssFile_DoNotDownload() + { + var mockDownloader = new Mock(); + mockDownloader.Setup(d => d.DownloadString(It.IsAny())).Returns(".a { display: block; }"); + WebDownloader.SharedDownloader = mockDownloader.Object; + + Uri baseUri = new Uri("http://a.com"); + Uri fullUrl = new Uri(baseUri, "b.bs"); + string input = String.Format("
test
", fullUrl); + + PreMailer sut = new PreMailer(input, baseUri); + sut.MoveCssInline(); + + mockDownloader.Verify(d => d.DownloadString(It.IsAny()), Times.Never()); + } + + [TestMethod] + public void ContainsLinkCssElement_DownloadsCss_InlinesContent() + { + var mockDownloader = new Mock(); + mockDownloader.Setup(d => d.DownloadString(It.IsAny())).Returns(".test { width: 150px; }"); + WebDownloader.SharedDownloader = mockDownloader.Object; + + string input = "
test
"; + + PreMailer sut = new PreMailer(input, new Uri("http://a.com")); + var premailedOutput = sut.MoveCssInline(); + + Assert.IsTrue(premailedOutput.Html.Contains("
")); + } } } diff --git a/PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs b/PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs index 03faa425..77f4824d 100644 --- a/PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs +++ b/PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs @@ -4,8 +4,27 @@ namespace PreMailer.Net.Downloaders { - internal class WebDownloader : IWebDownloader + public class WebDownloader : IWebDownloader { + private static IWebDownloader _sharedDownloader; + + public static IWebDownloader SharedDownloader + { + get + { + if (_sharedDownloader == null) + { + _sharedDownloader = new WebDownloader(); + } + + return _sharedDownloader; + } + set + { + _sharedDownloader = value; + } + } + public string DownloadString(Uri uri) { var request = WebRequest.Create(uri); diff --git a/PreMailer.Net/PreMailer.Net/Sources/LinkTagCssSource.cs b/PreMailer.Net/PreMailer.Net/Sources/LinkTagCssSource.cs index 44881b3d..a6e2213e 100644 --- a/PreMailer.Net/PreMailer.Net/Sources/LinkTagCssSource.cs +++ b/PreMailer.Net/PreMailer.Net/Sources/LinkTagCssSource.cs @@ -7,14 +7,11 @@ namespace PreMailer.Net.Sources { public class LinkTagCssSource : ICssSource { - private readonly IWebDownloader _webDownloader; private Uri _downloadUri; private string _cssContents; - public LinkTagCssSource(IDomObject node, Uri baseUri, IWebDownloader webDownloader = null) + public LinkTagCssSource(IDomObject node, Uri baseUri) { - _webDownloader = webDownloader ?? new WebDownloader(); - // There must be an href var href = node.Attributes.First(a => a.Key.Equals("href", StringComparison.OrdinalIgnoreCase)).Value; @@ -28,7 +25,7 @@ public string GetCss() { if (_cssContents == null) { - _cssContents = _webDownloader.DownloadString(_downloadUri); + _cssContents = WebDownloader.SharedDownloader.DownloadString(_downloadUri); } return _cssContents;