From 0047a5388d781343709ec74433ad04ecaca88308 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 2 Sep 2019 16:32:38 +0900 Subject: [PATCH] Add test for dead links in Views --- BTCPayServer.Tests/UnitTest1.cs | 43 +++++++++++++++++++++++++++++ BTCPayServer/Views/Home/Home.cshtml | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 4f5a4899a..6b39c7299 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -73,6 +73,49 @@ namespace BTCPayServer.Tests Logs.LogProvider = new XUnitLogProvider(helper); } + [Fact] + [Trait("Fast", "Fast")] + public async Task CheckNoDeadLink() + { + var views = Path.Combine(LanguageService.TryGetSolutionDirectoryInfo().FullName, "BTCPayServer", "Views"); + var viewFiles = Directory.EnumerateFiles(views, "*.cshtml", SearchOption.AllDirectories).ToArray(); + Assert.NotEmpty(viewFiles); + Regex regex = new Regex("href=\"(http.*?)\""); + var httpClient = new HttpClient(); + List checkLinks = new List(); + foreach (var file in viewFiles) + { + checkLinks.Add(CheckLinks(regex, httpClient, file)); + } + await Task.WhenAll(checkLinks); + } + + private static async Task CheckLinks(Regex regex, HttpClient httpClient, string file) + { + List checkLinks = new List(); + var text = await File.ReadAllTextAsync(file); + foreach (var match in regex.Matches(text).OfType()) + { + checkLinks.Add(AssertLinkNotDead(httpClient, match, file)); + } + await Task.WhenAll(checkLinks); + } + + private static async Task AssertLinkNotDead(HttpClient httpClient, Match match, string file) + { + var url = match.Groups[1].Value; + try + { + Assert.Equal(HttpStatusCode.OK, (await httpClient.GetAsync(url)).StatusCode); + Logs.Tester.LogInformation($"OK: {url} ({file})"); + } + catch + { + Logs.Tester.LogInformation($"FAILED: {url} ({file})"); + throw; + } + } + [Fact] [Trait("Fast", "Fast")] public void CanHandleUriValidation() diff --git a/BTCPayServer/Views/Home/Home.cshtml b/BTCPayServer/Views/Home/Home.cshtml index a12a99ed3..4afdbe067 100644 --- a/BTCPayServer/Views/Home/Home.cshtml +++ b/BTCPayServer/Views/Home/Home.cshtml @@ -114,7 +114,7 @@ -

On Slack

+

On Slack