Skip to content

Commit dc45f3e

Browse files
committed
Adjust test for the new behavior.
1 parent e0b407c commit dc45f3e

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/Components/Endpoints/test/EndpointHtmlRendererTest.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,35 +828,62 @@ public async Task Rendering_ComponentWithJsInteropThrows()
828828
exception.Message);
829829
}
830830

831-
[Fact]
832-
public async Task UriHelperRedirect_ThrowsInvalidOperationException_WhenResponseHasAlreadyStarted()
831+
[Theory]
832+
[InlineData(true)]
833+
[InlineData(false)]
834+
public async Task UriHelperRedirect_ThrowsInvalidOperationException_WhenResponseHasAlreadyStarted(bool expectException)
833835
{
836+
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.NavigationManager.EnableThrowNavigationException", isEnabled: expectException);
834837
// Arrange
835838
var ctx = new DefaultHttpContext();
836839
ctx.Request.Scheme = "http";
837840
ctx.Request.Host = new HostString("localhost");
838841
ctx.Request.PathBase = "/base";
839842
ctx.Request.Path = "/path";
840843
ctx.Request.QueryString = new QueryString("?query=value");
844+
ctx.Response.Body = new MemoryStream();
841845
var responseMock = new Mock<IHttpResponseFeature>();
842846
responseMock.Setup(r => r.HasStarted).Returns(true);
843847
ctx.Features.Set(responseMock.Object);
844848
var httpContext = GetHttpContext(ctx);
849+
string redirectUri = "http://localhost/redirect";
845850

846851
// Act
847-
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await renderer.PrerenderComponentAsync(
848-
httpContext,
849-
typeof(RedirectComponent),
850-
null,
851-
ParameterView.FromDictionary(new Dictionary<string, object>
852-
{
853-
{ "RedirectUri", "http://localhost/redirect" }
854-
})));
852+
if (expectException)
853+
{
854+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await renderer.PrerenderComponentAsync(
855+
httpContext,
856+
typeof(RedirectComponent),
857+
null,
858+
ParameterView.FromDictionary(new Dictionary<string, object>
859+
{
860+
{ "RedirectUri", redirectUri }
861+
})));
855862

856-
Assert.Equal("A navigation command was attempted during prerendering after the server already started sending the response. " +
857-
"Navigation commands can not be issued during server-side prerendering after the response from the server has started. Applications must buffer the" +
858-
"response and avoid using features like FlushAsync() before all components on the page have been rendered to prevent failed navigation commands.",
859-
exception.Message);
863+
Assert.Equal("A navigation command was attempted during prerendering after the server already started sending the response. " +
864+
"Navigation commands can not be issued during server-side prerendering after the response from the server has started. Applications must buffer the" +
865+
"response and avoid using features like FlushAsync() before all components on the page have been rendered to prevent failed navigation commands.",
866+
exception.Message);
867+
}
868+
else
869+
{
870+
await renderer.PrerenderComponentAsync(
871+
httpContext,
872+
typeof(RedirectComponent),
873+
null,
874+
ParameterView.FromDictionary(new Dictionary<string, object>
875+
{
876+
{ "RedirectUri", redirectUri }
877+
}));
878+
// read the custom element from the response body
879+
httpContext.Response.Body.Position = 0;
880+
var reader = new StreamReader(httpContext.Response.Body);
881+
var output = await reader.ReadToEndAsync();
882+
883+
// Assert that the output contains expected navigation instructions.
884+
var pattern = "^<blazor-ssr><template type=\"redirection\".*>.*<\\/template><blazor-ssr-end><\\/blazor-ssr-end><\\/blazor-ssr>$";
885+
Assert.Matches(pattern, output);
886+
}
860887
}
861888

862889
[Fact]

0 commit comments

Comments
 (0)