Skip to content

Commit 08cadfe

Browse files
authored
Improve SSR navigation tests (#62536)
* Make sure switch is in the correct position + add test with async operation before navigation. * Test SSR redirection with both values of switch.
1 parent 3f11ab0 commit 08cadfe

File tree

5 files changed

+116
-39
lines changed

5 files changed

+116
-39
lines changed

src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,28 @@ public void NavigatesWithoutInteractivityByRequestRedirection(bool controlFlowBy
8383
[Theory]
8484
[InlineData(true)]
8585
[InlineData(false)]
86-
public void ProgrammaticNavigationToNotExistingPathReExecutesTo404(bool streaming)
86+
public void ProgrammaticNavigationToNotExistingPath_ReExecutesTo404(bool streaming)
8787
{
88+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", isEnabled: true);
8889
string streamingPath = streaming ? "-streaming" : "";
8990
Navigate($"{ServerPathBase}/reexecution/redirection-not-found-ssr{streamingPath}?navigate-programmatically=true");
9091
AssertReExecutionPageRendered();
9192
}
9293

94+
[Fact]
95+
public void ProgrammaticNavigationToNotExistingPath_AfterAsyncOperation_ReExecutesTo404()
96+
{
97+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", isEnabled: true);
98+
Navigate($"{ServerPathBase}/reexecution/redirection-not-found-ssr?doAsync=true&navigate-programmatically=true");
99+
AssertReExecutionPageRendered();
100+
}
101+
93102
[Theory]
94103
[InlineData(true)]
95104
[InlineData(false)]
96-
public void LinkNavigationToNotExistingPathReExecutesTo404(bool streaming)
105+
public void LinkNavigationToNotExistingPath_ReExecutesTo404(bool streaming)
97106
{
107+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", isEnabled: true);
98108
string streamingPath = streaming ? "-streaming" : "";
99109
Navigate($"{ServerPathBase}/reexecution/redirection-not-found-ssr{streamingPath}");
100110
Browser.Click(By.Id("link-to-not-existing-page"));
@@ -104,8 +114,9 @@ public void LinkNavigationToNotExistingPathReExecutesTo404(bool streaming)
104114
[Theory]
105115
[InlineData(true)]
106116
[InlineData(false)]
107-
public void BrowserNavigationToNotExistingPathReExecutesTo404(bool streaming)
117+
public void BrowserNavigationToNotExistingPath_ReExecutesTo404(bool streaming)
108118
{
119+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", isEnabled: true);
109120
// non-existing path has to have re-execution middleware set up
110121
// so it has to have "reexecution" prefix. Otherwise middleware mapping
111122
// will not be activated, see configuration in Startup

src/Components/test/E2ETest/ServerRenderingTests/RedirectionTest.cs

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ public override async Task InitializeAsync()
3232
Browser.Equal("Redirections", () => _originalH1Element.Text);
3333
}
3434

35-
[Fact]
36-
public void RedirectStreamingGetToInternal()
35+
[Theory]
36+
[InlineData(true)]
37+
[InlineData(false)]
38+
public void RedirectStreamingGetToInternal(bool disableThrowNavigationException)
3739
{
40+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
41+
3842
Browser.Exists(By.LinkText("Streaming GET with internal redirection")).Click();
3943
AssertElementRemoved(_originalH1Element);
4044
Browser.Equal("Scroll to hash", () => Browser.Exists(By.TagName("h1")).Text);
@@ -47,16 +51,23 @@ public void RedirectStreamingGetToInternal()
4751
Assert.EndsWith("/subdir/redirect", Browser.Url);
4852
}
4953

50-
[Fact]
51-
public void RedirectStreamingGetToExternal()
54+
[Theory]
55+
[InlineData(true)]
56+
[InlineData(false)]
57+
public void RedirectStreamingGetToExternal(bool disableThrowNavigationException)
5258
{
59+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
5360
Browser.Exists(By.LinkText("Streaming GET with external redirection")).Click();
5461
Browser.Contains("microsoft.com", () => Browser.Url);
5562
}
5663

57-
[Fact]
58-
public void RedirectStreamingPostToInternal()
64+
[Theory]
65+
[InlineData(true)]
66+
[InlineData(false)]
67+
public void RedirectStreamingPostToInternal(bool disableThrowNavigationException)
5968
{
69+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
70+
6071
Browser.Exists(By.CssSelector("#form-streaming-internal button")).Click();
6172
AssertElementRemoved(_originalH1Element);
6273
Browser.Equal("Scroll to hash", () => Browser.Exists(By.TagName("h1")).Text);
@@ -69,16 +80,23 @@ public void RedirectStreamingPostToInternal()
6980
Assert.EndsWith("/subdir/redirect", Browser.Url);
7081
}
7182

72-
[Fact]
73-
public void RedirectStreamingPostToExternal()
83+
[Theory]
84+
[InlineData(true)]
85+
[InlineData(false)]
86+
public void RedirectStreamingPostToExternal(bool disableThrowNavigationException)
7487
{
88+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
7589
Browser.Exists(By.CssSelector("#form-streaming-external button")).Click();
7690
Browser.Contains("microsoft.com", () => Browser.Url);
7791
}
7892

79-
[Fact]
80-
public void RedirectEnhancedGetToInternal()
93+
[Theory]
94+
[InlineData(true)]
95+
[InlineData(false)]
96+
public void RedirectEnhancedGetToInternal(bool disableThrowNavigationException)
8197
{
98+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
99+
82100
// Note that for enhanced nav we can't preserve the hash part of the URL, as it
83101
// gets discarded when the browser follows a 'fetch' redirection. This is not solvable
84102
// unless we are willing to make the server return extra information so that the
@@ -95,16 +113,23 @@ public void RedirectEnhancedGetToInternal()
95113
Assert.EndsWith("/subdir/redirect", Browser.Url);
96114
}
97115

98-
[Fact]
99-
public void RedirectEnhancedGetToExternal()
116+
[Theory]
117+
[InlineData(true)]
118+
[InlineData(false)]
119+
public void RedirectEnhancedGetToExternal(bool disableThrowNavigationException)
100120
{
121+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
101122
Browser.Exists(By.LinkText("Enhanced GET with external redirection")).Click();
102123
Browser.Contains("microsoft.com", () => Browser.Url);
103124
}
104125

105-
[Fact]
106-
public void RedirectEnhancedPostToInternal()
126+
[Theory]
127+
[InlineData(true)]
128+
[InlineData(false)]
129+
public void RedirectEnhancedPostToInternal(bool disableThrowNavigationException)
107130
{
131+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
132+
108133
// See above for why enhanced nav doesn't support preserving the hash
109134
Browser.Exists(By.CssSelector("#form-enhanced-internal button")).Click();
110135
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
@@ -116,16 +141,23 @@ public void RedirectEnhancedPostToInternal()
116141
Assert.EndsWith("/subdir/redirect", Browser.Url);
117142
}
118143

119-
[Fact]
120-
public void RedirectEnhancedPostToExternal()
144+
[Theory]
145+
[InlineData(true)]
146+
[InlineData(false)]
147+
public void RedirectEnhancedPostToExternal(bool disableThrowNavigationException)
121148
{
149+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
122150
Browser.Exists(By.CssSelector("#form-enhanced-external button")).Click();
123151
Browser.Contains("microsoft.com", () => Browser.Url);
124152
}
125153

126-
[Fact]
127-
public void RedirectStreamingEnhancedGetToInternal()
154+
[Theory]
155+
[InlineData(true)]
156+
[InlineData(false)]
157+
public void RedirectStreamingEnhancedGetToInternal(bool disableThrowNavigationException)
128158
{
159+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
160+
129161
// See above for why enhanced nav doesn't support preserving the hash
130162
Browser.Exists(By.LinkText("Streaming enhanced GET with internal redirection")).Click();
131163
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
@@ -137,16 +169,25 @@ public void RedirectStreamingEnhancedGetToInternal()
137169
Assert.EndsWith("/subdir/redirect", Browser.Url);
138170
}
139171

140-
[Fact]
141-
public void RedirectStreamingEnhancedGetToExternal()
172+
[Theory]
173+
[InlineData(true)]
174+
[InlineData(false)]
175+
public void RedirectStreamingEnhancedGetToExternal(bool disableThrowNavigationException)
142176
{
177+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
178+
143179
Browser.Exists(By.LinkText("Streaming enhanced GET with external redirection")).Click();
144180
Browser.Contains("microsoft.com", () => Browser.Url);
145181
}
182+
146183

147-
[Fact]
148-
public void RedirectStreamingEnhancedPostToInternal()
184+
[Theory]
185+
[InlineData(true)]
186+
[InlineData(false)]
187+
public void RedirectStreamingEnhancedPostToInternal(bool disableThrowNavigationException)
149188
{
189+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
190+
150191
// See above for why enhanced nav doesn't support preserving the hash
151192
Browser.Exists(By.CssSelector("#form-streaming-enhanced-internal button")).Click();
152193
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
@@ -158,16 +199,24 @@ public void RedirectStreamingEnhancedPostToInternal()
158199
Assert.EndsWith("/subdir/redirect", Browser.Url);
159200
}
160201

161-
[Fact]
162-
public void RedirectStreamingEnhancedPostToExternal()
202+
[Theory]
203+
[InlineData(true)]
204+
[InlineData(false)]
205+
public void RedirectStreamingEnhancedPostToExternal(bool disableThrowNavigationException)
163206
{
207+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
208+
164209
Browser.Exists(By.CssSelector("#form-streaming-enhanced-external button")).Click();
165210
Browser.Contains("microsoft.com", () => Browser.Url);
166211
}
167212

168-
[Fact]
169-
public void RedirectEnhancedNonBlazorGetToInternal()
213+
[Theory]
214+
[InlineData(true)]
215+
[InlineData(false)]
216+
public void RedirectEnhancedNonBlazorGetToInternal(bool disableThrowNavigationException)
170217
{
218+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
219+
171220
// See above for why enhanced nav doesn't support preserving the hash
172221
Browser.Exists(By.LinkText("Enhanced GET to non-Blazor endpoint with internal redirection")).Click();
173222
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
@@ -179,16 +228,24 @@ public void RedirectEnhancedNonBlazorGetToInternal()
179228
Assert.EndsWith("/subdir/redirect", Browser.Url);
180229
}
181230

182-
[Fact]
183-
public void RedirectEnhancedNonBlazorGetToExternal()
231+
[Theory]
232+
[InlineData(true)]
233+
[InlineData(false)]
234+
public void RedirectEnhancedNonBlazorGetToExternal(bool disableThrowNavigationException)
184235
{
236+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
237+
185238
Browser.Exists(By.LinkText("Enhanced GET to non-Blazor endpoint with external redirection")).Click();
186239
Browser.Contains("microsoft.com", () => Browser.Url);
187240
}
188241

189-
[Fact]
190-
public void RedirectEnhancedNonBlazorPostToInternal()
242+
[Theory]
243+
[InlineData(true)]
244+
[InlineData(false)]
245+
public void RedirectEnhancedNonBlazorPostToInternal(bool disableThrowNavigationException)
191246
{
247+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
248+
192249
// See above for why enhanced nav doesn't support preserving the hash
193250
Browser.Exists(By.CssSelector("#form-nonblazor-enhanced-internal button")).Click();
194251
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
@@ -205,9 +262,13 @@ public void RedirectEnhancedNonBlazorPostToInternal()
205262
// response to something like a 200 that the 'fetch' is allowed to read (embedding the
206263
// destination URL).
207264

208-
[Fact]
209-
public void RedirectEnhancedGetToInternalWithErrorBoundary()
265+
[Theory]
266+
[InlineData(true)]
267+
[InlineData(false)]
268+
public void RedirectEnhancedGetToInternalWithErrorBoundary(bool disableThrowNavigationException)
210269
{
270+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.DisableThrowNavigationException", disableThrowNavigationException);
271+
211272
// This test verifies that redirection works even if an ErrorBoundary wraps
212273
// a component throwing a NavigationException.
213274

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/NotFound/RedirectionNotFound-SSR-streaming.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@page "/reexecution/redirection-not-found-ssr-streaming"
33
@attribute [StreamRendering(true)]
44

5-
<Components.WasmMinimal.Pages.NotFound.RedirectionNotFoundComponent StartStreaming="true" />
5+
<Components.WasmMinimal.Pages.NotFound.RedirectionNotFoundComponent DoAsyncOperationBeforeRedirection="true" />

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/NotFound/RedirectionNotFound-SSR.razor

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
@page "/reexecution/redirection-not-found-ssr"
33
@attribute [StreamRendering(false)]
44

5-
<Components.WasmMinimal.Pages.NotFound.RedirectionNotFoundComponent />
5+
<Components.WasmMinimal.Pages.NotFound.RedirectionNotFoundComponent DoAsyncOperationBeforeRedirection="@DoAsync" />
6+
7+
@code{
8+
[SupplyParameterFromQuery(Name = "doAsync")]
9+
public bool DoAsync { get; set; } = false;
10+
}

src/Components/test/testassets/Components.WasmMinimal/Pages/NotFound/RedirectionNotFoundComponent.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public bool? NavigateProgrammatically { get; set; }
1717

1818
[Parameter]
19-
public bool StartStreaming { get; set; } = false;
19+
public bool DoAsyncOperationBeforeRedirection { get; set; } = false;
2020

2121
[Parameter]
2222
public bool WaitForInteractivity { get; set; } = false;
@@ -25,7 +25,7 @@
2525

2626
protected override async Task OnInitializedAsync()
2727
{
28-
if (StartStreaming)
28+
if (DoAsyncOperationBeforeRedirection)
2929
{
3030
await Task.Yield();
3131
}

0 commit comments

Comments
 (0)