Skip to content

Commit 8744e8b

Browse files
committed
Draft of test.
1 parent f876e4d commit 8744e8b

File tree

5 files changed

+103
-46
lines changed

5 files changed

+103
-46
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
10+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
11+
using TestServer;
12+
using Components.TestServer.RazorComponents;
13+
using Microsoft.AspNetCore.E2ETesting;
14+
using Xunit.Abstractions;
15+
using OpenQA.Selenium;
16+
17+
namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
18+
19+
public class StatusCodePagesTest(BrowserFixture browserFixture, BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture, ITestOutputHelper output)
20+
: ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>>(browserFixture, serverFixture, output)
21+
{
22+
23+
[Fact]
24+
public async Task StatusCodePagesWithReexecution()
25+
{
26+
Navigate($"{ServerPathBase}/set-not-found");
27+
28+
Browser.Equal("Re-executed page", () => Browser.Title);
29+
var infoText = Browser.FindElement(By.Id("test-info")).Text;
30+
Assert.Contains("Welcome On Page Re-executed After Not Found Event", infoText);
31+
}
32+
}

src/Components/test/testassets/Components.TestServer/RazorComponentEndpointsStartup.cs

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public void ConfigureServices(IServiceCollection services)
4949
services.AddScoped<InteractiveServerService>();
5050
services.AddScoped<InteractiveAutoService>();
5151

52-
53-
54-
55-
5652
services.AddHttpContextAccessor();
5753
services.AddSingleton<AsyncOperationService>();
5854
services.AddCascadingAuthenticationState();
@@ -77,57 +73,67 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7773

7874
app.Map("/subdir", app =>
7975
{
80-
WebAssemblyTestHelper.ServeCoopHeadersIfWebAssemblyThreadingEnabled(app);
81-
82-
if (!env.IsDevelopment())
76+
app.Map("/reexecution", reexecutionApp =>
8377
{
84-
app.UseExceptionHandler("/Error", createScopeForErrors: true);
85-
}
78+
reexecutionApp.UseStatusCodePagesWithReExecute("/not-found-reexecute", true);
79+
});
8680

87-
app.UseRouting();
88-
UseFakeAuthState(app);
89-
app.UseAntiforgery();
81+
ConfigureSubdirPipeline(app, env);
82+
});
83+
}
84+
85+
protected virtual void ConfigureSubdirPipeline(IApplicationBuilder app, IWebHostEnvironment env)
86+
{
87+
WebAssemblyTestHelper.ServeCoopHeadersIfWebAssemblyThreadingEnabled(app);
9088

91-
app.Use((ctx, nxt) =>
89+
if (!env.IsDevelopment())
90+
{
91+
app.UseExceptionHandler("/Error", createScopeForErrors: true);
92+
}
93+
94+
app.UseRouting();
95+
UseFakeAuthState(app);
96+
app.UseAntiforgery();
97+
98+
app.Use((ctx, nxt) =>
99+
{
100+
if (ctx.Request.Query.ContainsKey("add-csp"))
92101
{
93-
if (ctx.Request.Query.ContainsKey("add-csp"))
94-
{
95-
ctx.Response.Headers.Add("Content-Security-Policy", "script-src 'self' 'unsafe-inline'");
96-
}
97-
return nxt();
98-
});
102+
ctx.Response.Headers.Add("Content-Security-Policy", "script-src 'self' 'unsafe-inline'");
103+
}
104+
return nxt();
105+
});
99106

100-
_ = app.UseEndpoints(endpoints =>
107+
_ = app.UseEndpoints(endpoints =>
108+
{
109+
var contentRootStaticAssetsPath = Path.Combine(env.ContentRootPath, "Components.TestServer.staticwebassets.endpoints.json");
110+
if (File.Exists(contentRootStaticAssetsPath))
101111
{
102-
var contentRootStaticAssetsPath = Path.Combine(env.ContentRootPath, "Components.TestServer.staticwebassets.endpoints.json");
103-
if (File.Exists(contentRootStaticAssetsPath))
104-
{
105-
endpoints.MapStaticAssets(contentRootStaticAssetsPath);
106-
}
107-
else
108-
{
109-
endpoints.MapStaticAssets();
110-
}
112+
endpoints.MapStaticAssets(contentRootStaticAssetsPath);
113+
}
114+
else
115+
{
116+
endpoints.MapStaticAssets();
117+
}
111118

112-
_ = endpoints.MapRazorComponents<TRootComponent>()
113-
.AddAdditionalAssemblies(Assembly.Load("Components.WasmMinimal"))
114-
.AddInteractiveServerRenderMode(options =>
115-
{
116-
var config = app.ApplicationServices.GetRequiredService<WebSocketCompressionConfiguration>();
117-
options.DisableWebSocketCompression = config.IsCompressionDisabled;
119+
_ = endpoints.MapRazorComponents<TRootComponent>()
120+
.AddAdditionalAssemblies(Assembly.Load("Components.WasmMinimal"))
121+
.AddInteractiveServerRenderMode(options =>
122+
{
123+
var config = app.ApplicationServices.GetRequiredService<WebSocketCompressionConfiguration>();
124+
options.DisableWebSocketCompression = config.IsCompressionDisabled;
118125

119-
options.ContentSecurityFrameAncestorsPolicy = config.CspPolicy;
126+
options.ContentSecurityFrameAncestorsPolicy = config.CspPolicy;
120127

121-
options.ConfigureWebSocketAcceptContext = config.ConfigureWebSocketAcceptContext;
122-
})
123-
.AddInteractiveWebAssemblyRenderMode(options => options.PathPrefix = "/WasmMinimal");
128+
options.ConfigureWebSocketAcceptContext = config.ConfigureWebSocketAcceptContext;
129+
})
130+
.AddInteractiveWebAssemblyRenderMode(options => options.PathPrefix = "/WasmMinimal");
124131

125-
NotEnabledStreamingRenderingComponent.MapEndpoints(endpoints);
126-
StreamingRenderingForm.MapEndpoints(endpoints);
127-
InteractiveStreamingRenderingComponent.MapEndpoints(endpoints);
132+
NotEnabledStreamingRenderingComponent.MapEndpoints(endpoints);
133+
StreamingRenderingForm.MapEndpoints(endpoints);
134+
InteractiveStreamingRenderingComponent.MapEndpoints(endpoints);
128135

129-
MapEnhancedNavigationEndpoints(endpoints);
130-
});
136+
MapEnhancedNavigationEndpoints(endpoints);
131137
});
132138
}
133139

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@page "/reexecution/set-not-found"
2+
@attribute [StreamRendering(false)]
3+
@inject NavigationManager NavigationManager
4+
5+
<p id="test-info">Any content</p>
6+
7+
@code{
8+
protected override void OnInitialized()
9+
{
10+
NavigationManager.NotFound();
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@page "/not-found-reexecute"
2+
3+
<PageTitle>Re-executed page</PageTitle>
4+
5+
<h3 id="test-info">Welcome On Page Re-executed After Not Found Event</h3>
6+
<p>This page is shown when UseStatusCodePagesWithReExecute is set and another page sensitive 404</p>

src/Middleware/Diagnostics/src/StatusCodePage/StatusCodePagesExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ public static IApplicationBuilder UseStatusCodePagesWithReExecute(
194194
});
195195
}
196196

197-
return app.UseStatusCodePages(new StatusCodePagesOptions
197+
var options = new StatusCodePagesOptions
198198
{
199199
HandleAsync = CreateHandler(pathFormat, queryFormat),
200200
CreateScopeForErrors = createScopeForErrors
201-
});
201+
};
202+
return app.UseMiddleware<StatusCodePagesMiddleware>(options);
202203
}
203204

204205
private static Func<StatusCodeContext, Task> CreateHandler(string pathFormat, string? queryFormat, RequestDelegate? next = null)

0 commit comments

Comments
 (0)