Skip to content

Commit

Permalink
Test download of CSS from link tags
Browse files Browse the repository at this point in the history
Wraps up #72
  • Loading branch information
martinnormark committed Dec 16, 2015
1 parent 69c7704 commit 4431590
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
7 changes: 6 additions & 1 deletion PreMailer.Net/PreMailer.Net.Tests/LinkTagCssSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class LinkTagCssSourceTests
{
private readonly Mock<IWebDownloader> _webDownloader = new Mock<IWebDownloader>();

public LinkTagCssSourceTests()
{
WebDownloader.SharedDownloader = _webDownloader.Object;
}

[TestMethod]
public void ImplementsInterface()
{
Expand Down Expand Up @@ -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("<link href=\"{0}\" />", path));
var sut = new LinkTagCssSource(node.FirstElement(), new Uri(baseUrl), webDownloader: _webDownloader.Object);
var sut = new LinkTagCssSource(node.FirstElement(), new Uri(baseUrl));

return sut;
}
Expand Down
51 changes: 51 additions & 0 deletions PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PreMailer.Net.Downloaders;
using Moq;

namespace PreMailer.Net.Tests
{
Expand Down Expand Up @@ -281,5 +283,54 @@ public void AddAnalyticsTags_AddsTagsAndExcludesDomain()
.MoveCssInline();
Assert.IsTrue(expected == premailedOutput.Html);
}

[TestMethod]
public void ContainsLinkCssElement_DownloadsCss()
{
var mockDownloader = new Mock<IWebDownloader>();
mockDownloader.Setup(d => d.DownloadString(It.IsAny<Uri>())).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("<html><head><link href=\"{0}\"></link></head><body><div id=\"high-imp\" class=\"test\">test</div></body></html>", 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<IWebDownloader>();
mockDownloader.Setup(d => d.DownloadString(It.IsAny<Uri>())).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("<html><head><link href=\"{0}\"></link></head><body><div id=\"high-imp\" class=\"test\">test</div></body></html>", fullUrl);

PreMailer sut = new PreMailer(input, baseUri);
sut.MoveCssInline();

mockDownloader.Verify(d => d.DownloadString(It.IsAny<Uri>()), Times.Never());
}

[TestMethod]
public void ContainsLinkCssElement_DownloadsCss_InlinesContent()
{
var mockDownloader = new Mock<IWebDownloader>();
mockDownloader.Setup(d => d.DownloadString(It.IsAny<Uri>())).Returns(".test { width: 150px; }");
WebDownloader.SharedDownloader = mockDownloader.Object;

string input = "<html><head><link href=\"http://a.com/b.css\"></link></head><body><div class=\"test\">test</div></body></html>";

PreMailer sut = new PreMailer(input, new Uri("http://a.com"));
var premailedOutput = sut.MoveCssInline();

Assert.IsTrue(premailedOutput.Html.Contains("<div class=\"test\" style=\"width: 150px\">"));
}
}
}
21 changes: 20 additions & 1 deletion PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions PreMailer.Net/PreMailer.Net/Sources/LinkTagCssSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,7 +25,7 @@ public string GetCss()
{
if (_cssContents == null)
{
_cssContents = _webDownloader.DownloadString(_downloadUri);
_cssContents = WebDownloader.SharedDownloader.DownloadString(_downloadUri);
}

return _cssContents;
Expand Down

0 comments on commit 4431590

Please sign in to comment.