Skip to content

Commit 7be708b

Browse files
committed
Add PreRender test
1 parent 83c38a4 commit 7be708b

File tree

6 files changed

+171
-3
lines changed

6 files changed

+171
-3
lines changed

BlazorPreRender/BlazorApp1/BlazorApp1.Server/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
88
},
9-
"AllowedHosts": "*"
9+
"AllowedHosts": "*",
10+
"RenderOutputDirectory": ".RenderOutput"
1011
}

BlazorPreRender/BlazorApp1/BlazorApp1.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30717.126
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1", "BlazorApp1\BlazorApp1.csproj", "{8636DB7E-21C3-4231-90E2-011FD55DF5A9}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1", "BlazorApp1\BlazorApp1.csproj", "{8636DB7E-21C3-4231-90E2-011FD55DF5A9}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1.Server", "BlazorApp1.Server\BlazorApp1.Server.csproj", "{91D463CA-3617-47D5-9564-96B65B5B74DB}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Server", "BlazorApp1.Server\BlazorApp1.Server.csproj", "{91D463CA-3617-47D5-9564-96B65B5B74DB}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PreRenderer", "PreRenderer\PreRenderer.csproj", "{49B31D6A-5EC3-4813-89EC-E4C005488A1B}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{91D463CA-3617-47D5-9564-96B65B5B74DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{91D463CA-3617-47D5-9564-96B65B5B74DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{91D463CA-3617-47D5-9564-96B65B5B74DB}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{49B31D6A-5EC3-4813-89EC-E4C005488A1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{49B31D6A-5EC3-4813-89EC-E4C005488A1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{49B31D6A-5EC3-4813-89EC-E4C005488A1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{49B31D6A-5EC3-4813-89EC-E4C005488A1B}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.Mvc.Testing;
2+
using Microsoft.AspNetCore.TestHost;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
using System.Collections.Generic;
8+
using System.Net.Http;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Xunit.Abstractions;
11+
12+
namespace PreRenderer
13+
{
14+
public class AppTestFixture : WebApplicationFactory<BlazorApp1.Server.Program>
15+
{
16+
public ITestOutputHelper Output { get; set; }
17+
18+
protected override IHostBuilder CreateHostBuilder()
19+
{
20+
var builder = base.CreateHostBuilder();
21+
builder.UseEnvironment(Environments.Production);
22+
builder.ConfigureLogging(logging =>
23+
{
24+
logging.ClearProviders();
25+
logging.AddXUnit(Output);
26+
});
27+
28+
builder.ConfigureWebHost(
29+
webHostBuilder =>
30+
{
31+
webHostBuilder.UseStaticWebAssets();
32+
webHostBuilder.ConfigureTestServices(services =>
33+
{
34+
services.AddSingleton(_ => CreateDefaultClient());
35+
});
36+
});
37+
return builder;
38+
}
39+
}
40+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace PreRenderer
13+
{
14+
public class GenerateOutput : IClassFixture<AppTestFixture>, IDisposable
15+
{
16+
private readonly AppTestFixture _fixture;
17+
private readonly HttpClient _client;
18+
private readonly string _outputPath;
19+
private readonly ITestOutputHelper _output;
20+
21+
public GenerateOutput(AppTestFixture fixture, ITestOutputHelper output)
22+
{
23+
_fixture = fixture;
24+
_output = output;
25+
_fixture.Output = output;
26+
_client = fixture.CreateDefaultClient();
27+
28+
var config = _fixture.Services.GetRequiredService<IConfiguration>();
29+
_outputPath = config["RenderOutputDirectory"];
30+
31+
if (string.IsNullOrEmpty(_outputPath))
32+
{
33+
throw new ArgumentException("RenderOutputDirectory config value was null or empty", nameof(_outputPath));
34+
}
35+
}
36+
37+
public void Dispose()
38+
{
39+
_client?.Dispose();
40+
_fixture.Output = null;
41+
}
42+
43+
/// <summary>
44+
/// Massage the values into something that works for xunit theory
45+
/// </summary>
46+
public static IEnumerable<object[]> GetPagesToPreRender()
47+
=> PrerenderRoutes
48+
.Values
49+
.Select(config => new object[] { config });
50+
51+
[Theory, Trait("Category", "PreRender")]
52+
[MemberData(nameof(GetPagesToPreRender))]
53+
public async Task Render(string route)
54+
{
55+
// strip the initial / off
56+
var renderPath = route.Substring(1);
57+
58+
var relativePath = Path.Combine(_outputPath, renderPath);
59+
var outputDirectory = Path.GetFullPath(relativePath);
60+
61+
_output.WriteLine($"Creating directory '{outputDirectory}'");
62+
Directory.CreateDirectory(outputDirectory);
63+
64+
var fileName = Path.Combine(outputDirectory, "index.html");
65+
66+
_output.WriteLine($"Fetching prerendered content for '{route}'");
67+
var result = await _client.GetStreamAsync(route);
68+
69+
_output.WriteLine($"Writing content to '{fileName}'");
70+
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
71+
{
72+
await result.CopyToAsync(file);
73+
}
74+
75+
_output.WriteLine($"Pre rendering complete");
76+
77+
}
78+
}
79+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.1.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.1" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="1.3.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\BlazorApp1\BlazorApp1.csproj" />
25+
<ProjectReference Include="..\BlazorApp1.Server\BlazorApp1.Server.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
3+
namespace PreRenderer
4+
{
5+
public static class PrerenderRoutes
6+
{
7+
public static List<string> Values { get; } = new()
8+
{
9+
"/",
10+
"/counter",
11+
"/fetchdata",
12+
};
13+
}
14+
}

0 commit comments

Comments
 (0)