Skip to content

Commit 86b9421

Browse files
Check all links for templates
1 parent 799bcff commit 86b9421

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

src/ProjectTemplates/test/EmptyWebTemplateTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using ProjectTemplates.Tests.Helpers;
56
using Xunit;
67
using Xunit.Abstractions;
@@ -19,15 +20,15 @@ public EmptyWebTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputHel
1920
[Theory]
2021
[InlineData(null)]
2122
[InlineData("F#")]
22-
public void EmptyWebTemplate(string languageOverride)
23+
public async Task EmptyWebTemplate(string languageOverride)
2324
{
2425
Project.RunDotNetNew("web", language: languageOverride);
2526

2627
foreach (var publish in new[] { false, true })
2728
{
2829
using (var aspNetProcess = Project.StartAspNetProcess(publish))
2930
{
30-
aspNetProcess.AssertOk("/");
31+
await aspNetProcess.AssertLinksWork("/");
3132
}
3233
}
3334
}

src/ProjectTemplates/test/Helpers/AspNetProcess.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System.Linq;
88
using System.Net;
99
using System.Net.Http;
10+
using System.Threading.Tasks;
11+
using AngleSharp.Dom.Html;
12+
using AngleSharp.Parser.Html;
1013
using Microsoft.AspNetCore.Certificates.Generation;
1114
using Microsoft.Extensions.CommandLineUtils;
1215
using OpenQA.Selenium;
@@ -134,7 +137,37 @@ private Uri GetListeningUri(ITestOutputHelper output)
134137
return new Uri(listeningUrlString, UriKind.Absolute);
135138
}
136139

137-
public void AssertOk(string requestUrl)
140+
public async Task AssertLinksWork(string requestUrl)
141+
{
142+
var request = new HttpRequestMessage(
143+
HttpMethod.Get,
144+
new Uri(_listeningUri, requestUrl));
145+
146+
var response = await _httpClient.SendAsync(request);
147+
148+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
149+
var parser = new HtmlParser();
150+
var html = await parser.ParseAsync(await response.Content.ReadAsStreamAsync());
151+
foreach (IHtmlAnchorElement link in html.Links)
152+
{
153+
if(link.Protocol == "about:")
154+
{
155+
AssertOk(link.PathName);
156+
}
157+
else
158+
{
159+
var result = await _httpClient.GetAsync(link.Href);
160+
Assert.True(IsSuccessStatusCode(result), $"{link.Href} is a broken link!");
161+
}
162+
}
163+
}
164+
165+
private bool IsSuccessStatusCode(HttpResponseMessage response)
166+
{
167+
return response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.Redirect;
168+
}
169+
170+
private void AssertOk(string requestUrl)
138171
=> AssertStatusCode(requestUrl, HttpStatusCode.OK);
139172

140173
public void AssertNotFound(string requestUrl)

src/ProjectTemplates/test/MvcTemplateTest.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using ProjectTemplates.Tests.Helpers;
56
using Xunit;
67
using Xunit.Abstractions;
@@ -19,7 +20,7 @@ public MvcTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputHelper o
1920
[Theory]
2021
[InlineData(null)]
2122
[InlineData("F#")]
22-
public void MvcTemplate_NoAuthImpl(string languageOverride)
23+
public async Task MvcTemplate_NoAuthImpl(string languageOverride)
2324
{
2425
Project.RunDotNetNew("mvc", language: languageOverride);
2526

@@ -40,16 +41,16 @@ public void MvcTemplate_NoAuthImpl(string languageOverride)
4041
{
4142
using (var aspNetProcess = Project.StartAspNetProcess(publish))
4243
{
43-
aspNetProcess.AssertOk("/");
44-
aspNetProcess.AssertOk("/Home/Privacy");
44+
await aspNetProcess.AssertLinksWork("/");
45+
await aspNetProcess.AssertLinksWork("/Home/Privacy");
4546
}
4647
}
4748
}
4849

4950
[Theory]
5051
[InlineData(true)]
5152
[InlineData(false)]
52-
public void MvcTemplate_IndividualAuthImpl(bool useLocalDB)
53+
public async Task MvcTemplate_IndividualAuthImpl(bool useLocalDB)
5354
{
5455
Project.RunDotNetNew("mvc", auth: "Individual", useLocalDB: useLocalDB);
5556

@@ -71,9 +72,9 @@ public void MvcTemplate_IndividualAuthImpl(bool useLocalDB)
7172
{
7273
using (var aspNetProcess = Project.StartAspNetProcess(publish))
7374
{
74-
aspNetProcess.AssertOk("/");
75-
aspNetProcess.AssertOk("/Identity/Account/Login");
76-
aspNetProcess.AssertOk("/Home/Privacy");
75+
await aspNetProcess.AssertLinksWork("/");
76+
await aspNetProcess.AssertLinksWork("/Identity/Account/Login");
77+
await aspNetProcess.AssertLinksWork("/Home/Privacy");
7778
}
7879
}
7980
}

src/ProjectTemplates/test/RazorPagesTemplateTest.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Testing.xunit;
56
using ProjectTemplates.Tests.Helpers;
67
using Xunit;
@@ -18,7 +19,7 @@ public RazorPagesTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputH
1819
public Project Project { get; }
1920

2021
[Fact]
21-
private void RazorPagesTemplate_NoAuthImpl()
22+
private async Task RazorPagesTemplate_NoAuthImpl()
2223
{
2324
Project.RunDotNetNew("razor");
2425

@@ -35,16 +36,16 @@ private void RazorPagesTemplate_NoAuthImpl()
3536
{
3637
using (var aspNetProcess = Project.StartAspNetProcess(publish))
3738
{
38-
aspNetProcess.AssertOk("/");
39-
aspNetProcess.AssertOk("/Privacy");
39+
await aspNetProcess.AssertLinksWork("/");
40+
await aspNetProcess.AssertLinksWork("/Privacy");
4041
}
4142
}
4243
}
4344

4445
[Theory]
4546
[InlineData(false)]
4647
[InlineData(true)]
47-
public void RazorPagesTemplate_IndividualAuthImpl( bool useLocalDB)
48+
public async Task RazorPagesTemplate_IndividualAuthImpl( bool useLocalDB)
4849
{
4950
Project.RunDotNetNew("razor", auth: "Individual", useLocalDB: useLocalDB);
5051

@@ -64,9 +65,9 @@ public void RazorPagesTemplate_IndividualAuthImpl( bool useLocalDB)
6465
{
6566
using (var aspNetProcess = Project.StartAspNetProcess(publish))
6667
{
67-
aspNetProcess.AssertOk("/");
68-
aspNetProcess.AssertOk("/Identity/Account/Login");
69-
aspNetProcess.AssertOk("/Privacy");
68+
await aspNetProcess.AssertLinksWork("/");
69+
await aspNetProcess.AssertLinksWork("/Identity/Account/Login");
70+
await aspNetProcess.AssertLinksWork("/Privacy");
7071
}
7172
}
7273
}

src/ProjectTemplates/test/WebApiTemplateTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using ProjectTemplates.Tests.Helpers;
56
using Xunit;
67
using Xunit.Abstractions;
@@ -19,15 +20,15 @@ public WebApiTemplateTest(ProjectFactoryFixture factoryFixture, ITestOutputHelpe
1920
[Theory]
2021
[InlineData(null)]
2122
[InlineData("F#")]
22-
public void WebApiTemplate(string languageOverride)
23+
public async Task WebApiTemplate(string languageOverride)
2324
{
2425
Project.RunDotNetNew("webapi", language: languageOverride);
2526

2627
foreach (var publish in new[] { false, true })
2728
{
2829
using (var aspNetProcess = Project.StartAspNetProcess(publish))
2930
{
30-
aspNetProcess.AssertOk("/api/values");
31+
await aspNetProcess.AssertLinksWork("/api/values");
3132
aspNetProcess.AssertNotFound("/");
3233
}
3334
}

0 commit comments

Comments
 (0)