-
Notifications
You must be signed in to change notification settings - Fork 643
/
MarkdownServiceFacts.cs
194 lines (172 loc) · 11.8 KB
/
MarkdownServiceFacts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Data.Entity.Core.Objects;
using Moq;
using Xunit;
namespace NuGetGallery
{
public class MarkdownServiceFacts
{
public class GetReadMeHtmlMethod
{
private readonly MarkdownService _markdownService;
private readonly Mock<IFeatureFlagService> _featureFlagService;
public GetReadMeHtmlMethod()
{
_featureFlagService = new Mock<IFeatureFlagService>();
_markdownService = new MarkdownService(_featureFlagService.Object);
}
[Theory]
[InlineData(-1)]
public void ThrowsArgumentOutOfRangeExceptionForNegativeIncrementHeadersBy(int incrementHeadersBy)
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => _markdownService.GetHtmlFromMarkdown("markdown file test", incrementHeadersBy));
Assert.Equal(nameof(incrementHeadersBy), exception.ParamName);
Assert.Contains("must be greater than or equal to 0", exception.Message);
}
[Fact]
public void ThrowsArgumentNullExceptionForNullMarkdownString()
{
Assert.Throws<ArgumentNullException>(() => _markdownService.GetHtmlFromMarkdown(null, 0));
Assert.Throws<ArgumentNullException>(() => _markdownService.GetHtmlFromMarkdown(null));
}
[Theory]
[InlineData("<script>alert('test')</script>", "<p><script>alert('test')</script></p>", true)]
[InlineData("<script>alert('test')</script>", "<p><script>alert('test')</script></p>", false)]
[InlineData("<img src=\"javascript:alert('test');\">", "<p><img src="javascript:alert('test');"></p>", true)]
[InlineData("<img src=\"javascript:alert('test');\">", "<p><img src="javascript:alert('test');"></p>", false)]
[InlineData("<a href=\"javascript:alert('test');\">", "<p><a href="javascript:alert('test');"></p>", true)]
[InlineData("<a href=\"javascript:alert('test');\">", "<p><a href="javascript:alert('test');"></p>", false)]
public void EncodesHtmlInMarkdown(string originalMd, string expectedHtml, bool isMarkdigMdRenderingEnabled)
{
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(isMarkdigMdRenderingEnabled);
Assert.Equal(expectedHtml, _markdownService.GetHtmlFromMarkdown(originalMd).Content);
}
[Theory]
[InlineData("# Heading", "<h1>Heading</h1>", true, 0)]
[InlineData("# Heading", "<h1>Heading</h1>", false, 0)]
[InlineData("# Heading", "<h2>Heading</h2>", true, 1)]
[InlineData("# Heading", "<h2>Heading</h2>", false, 1)]
[InlineData("# Heading", "<h6>Heading</h6>", true, 6)]
[InlineData("# Heading", "<h6>Heading</h6>", false, 6)]
[InlineData("# Heading", "<h6>Heading</h6>", true, 7)]
[InlineData("# Heading", "<h6>Heading</h6>", false, 7)]
[InlineData("# Heading", "<h6>Heading</h6>", true, 5)]
[InlineData("# Heading", "<h6>Heading</h6>", false, 5)]
public void EncodesHtmlInMarkdownWithAdaptiveHeader(string originalMd, string expectedHtml, bool isMarkdigMdRenderingEnabled, int incrementHeadersBy)
{
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(isMarkdigMdRenderingEnabled);
Assert.Equal(expectedHtml, _markdownService.GetHtmlFromMarkdown(originalMd, incrementHeadersBy).Content);
}
[Theory]
[InlineData("# Heading", "<h2>Heading</h2>", false, true)]
[InlineData("# Heading", "<h2>Heading</h2>", false, false)]
[InlineData("\ufeff# Heading with BOM", "<h2>Heading with BOM</h2>", false, true)]
[InlineData("\ufeff# Heading with BOM", "<h2>Heading with BOM</h2>", false, false)]
[InlineData("- List", "<ul>\n<li>List</li>\n</ul>", false, true)]
[InlineData("- List", "<ul>\r\n<li>List</li>\r\n</ul>", false, false)]
[InlineData("[text](http://www.test.com)", "<p><a href=\"http://www.test.com/\" rel=\"nofollow\">text</a></p>", false, true)]
[InlineData("[text](http://www.test.com)", "<p><a href=\"http://www.test.com/\" rel=\"nofollow\">text</a></p>", false, false)]
[InlineData("[text](javascript:alert('hi'))", "<p><a href=\"\" rel=\"nofollow\">text</a></p>", false, true)]
[InlineData("[text](javascript:alert('hi'))", "<p><a href=\"\" rel=\"nofollow\">text</a></p>", false, false)]
[InlineData("> <text>Blockquote</text>", "<blockquote>\n<p><text>Blockquote</text></p>\n</blockquote>", false, true)]
[InlineData("> <text>Blockquote</text>", "<blockquote>\r\n<p><text>Blockquote</text></p>\r\n</blockquote>", false, false)]
[InlineData("> > <text>Blockquote</text>", "<blockquote>\n<blockquote>\n<p><text>Blockquote</text></p>\n</blockquote>\n</blockquote>", false, true)]
[InlineData("> > <text>Blockquote</text>", "<blockquote>\r\n<p>> <text>Blockquote</text></p>\r\n</blockquote>", false, false)]
[InlineData("[text](http://www.asp.net)", "<p><a href=\"https://www.asp.net/\" rel=\"nofollow\">text</a></p>", false, true)]
[InlineData("[text](http://www.asp.net)", "<p><a href=\"https://www.asp.net/\" rel=\"nofollow\">text</a></p>", false, false)]
[InlineData("[text](badurl://www.asp.net)", "<p><a href=\"\" rel=\"nofollow\">text</a></p>", false, true)]
[InlineData("[text](badurl://www.asp.net)", "<p><a href=\"\" rel=\"nofollow\">text</a></p>", false, false)]
[InlineData("![image](http://www.asp.net/fake.jpg)", "<p><img src=\"https://www.asp.net/fake.jpg\" alt=\"image\" /></p>", true, true)]
[InlineData("![image](http://www.asp.net/fake.jpg)", "<p><img src=\"https://www.asp.net/fake.jpg\" alt=\"image\" /></p>", true, false)]
[InlineData("![image](https://www.asp.net/fake.jpg)", "<p><img src=\"https://www.asp.net/fake.jpg\" alt=\"image\" /></p>", false, true)]
[InlineData("![image](https://www.asp.net/fake.jpg)", "<p><img src=\"https://www.asp.net/fake.jpg\" alt=\"image\" /></p>", false, false)]
[InlineData("![image](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" alt=\"image\" /></p>", true, true)]
[InlineData("![image](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" alt=\"image\" /></p>", true, false)]
[InlineData("## License\n\tLicensed under the Apache License, Version 2.0 (the \"License\");", "<h3>License</h3>\n<pre><code>Licensed under the Apache License, Version 2.0 (the "License");\n</code></pre>", false, true)]
[InlineData("## License\n\tLicensed under the Apache License, Version 2.0 (the \"License\");", "<h3>License</h3>\n<pre><code>Licensed under the Apache License, Version 2.0 (the "License");\n</code></pre>", false, true)]
public void ConvertsMarkdownToHtml(string originalMd, string expectedHtml, bool imageRewriteExpected, bool isMarkdigMdRenderingEnabled)
{
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(isMarkdigMdRenderingEnabled);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(imageRewriteExpected, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithExtension()
{
var originalMd = "This is a paragraph\r\n with a break inside";
var expectedHtml = "<p>This is a paragraph<br />\nwith a break inside</p>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithPipeTable()
{
var originalMd = @"a | b
-- | -
0 | 1";
var expectedHtml = "<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>1</td>\n</tr>\n</tbody>\n</table>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithGridTable()
{
var originalMd = @"+---+---+
| a | b |
+===+===+
| 1 | 2 |
+---+---+
";
var expectedHtml = "<table>\n<col style=\"width:50%\" />\n<col style=\"width:50%\" />\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>1</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithEmojiAndSmiley()
{
var originalMd = "This is a test with a :) and a :angry: smiley";
var expectedHtml = "<p>This is a test with a 😃 and a 😠 smiley</p>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithTaskLists()
{
var originalMd = @"- [ ] Item1
- [x] Item2
- [ ] Item3
- Item4";
var expectedHtml = "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input disabled=\"disabled\" type=\"checkbox\" /> Item1</li>\n<li class=\"task-list-item\">" +
"<input disabled=\"disabled\" type=\"checkbox\" checked=\"checked\" /> Item2</li>\n<li class=\"task-list-item\"><input disabled=\"disabled\" type=\"checkbox\" /> " +
"Item3</li>\n<li>Item4</li>\n</ul>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
[Fact]
public void TestToHtmlWithAddtionalList()
{
var originalMd = @"1. First item
Some text
2. Second item";
var expectedHtml = "<ol>\n<li>First item</li>\n</ol>\n<p>Some text</p>\n<ol start=\"2\">\n<li>Second item</li>\n</ol>";
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(true);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(false, readMeResult.ImagesRewritten);
}
}
}
}