Skip to content

Commit 3a71770

Browse files
author
W Hörchner
committed
Add an ImportRuleCssSourceTest class
1 parent 7242f0a commit 3a71770

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using AngleSharp.Html.Parser;
2+
using Xunit;
3+
using Moq;
4+
using PreMailer.Net.Downloaders;
5+
using PreMailer.Net.Sources;
6+
using System;
7+
using System.Text;
8+
using System.IO;
9+
using System.Collections.Generic;
10+
11+
12+
namespace PreMailer.Net.Tests
13+
{
14+
public class ImportRuleCssSourceTests
15+
{
16+
private readonly Mock<IWebDownloader> _webDownloader = new Mock<IWebDownloader>();
17+
18+
19+
[Fact]
20+
public void FetchImportRules()
21+
{
22+
var baseUri = new Uri("https://a.com");
23+
var urls = new List<string>() { "variables.css", "/fonts.css", "https://fonts.google.com/css/test-font" };
24+
25+
var css = CreateCss(urls);
26+
var contents = ImportRuleCssSource.FetchImportRules(baseUri, css);
27+
28+
foreach (var url in urls)
29+
{
30+
_webDownloader.Verify(w => w.DownloadString(It.Is<Uri>(u => u.Equals(new Uri(baseUri, url)))));
31+
}
32+
33+
}
34+
35+
private string CreateCss(IEnumerable<string> imports)
36+
{
37+
WebDownloader.SharedDownloader = _webDownloader.Object;
38+
39+
var builder = new StringBuilder();
40+
foreach (var import in imports)
41+
{
42+
builder.AppendLine($"@import \"{import}\";");
43+
}
44+
45+
return builder.ToString();
46+
}
47+
}
48+
}

PreMailer.Net/PreMailer.Net/Sources/ImportRuleCssSource.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void DownloadContents()
7171
throw new WebException($"PreMailer.Net is unable to download the requested URL: {_downloadUri}", ex);
7272
}
7373

74-
if (_level < 2) // Stop processing imports at level 2
74+
if (_level < 2 && cssContents != null) // Stop processing imports at level 2
7575
{
7676
FetchImportRules(_downloadUri, cssContents, _level + 1, _contentBuilder, _importList);
7777
}
@@ -94,6 +94,11 @@ private static bool IsSupported(string scheme)
9494
/// <returns></returns>
9595
public static string FetchImportRules(Uri downloadUri, string contents)
9696
{
97+
if (contents == null)
98+
{
99+
return string.Empty;
100+
}
101+
97102
var contentBuilder = new StringBuilder();
98103
var importList = new List<Uri>();
99104

@@ -123,6 +128,7 @@ public static string FetchImportRules(Uri downloadUri, string contents)
123128
/// <param name="importList"></param>
124129
private static void FetchImportRules(Uri downloadUri, string contents, int level, StringBuilder contentBuilder, List<Uri> importList)
125130
{
131+
126132
string indent = level > 0 ? new string('\t', level) : string.Empty;
127133
int cnt = 0;
128134

0 commit comments

Comments
 (0)