/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Components/Endpoints/test/SSRRenderModeBoundaryTest.cs
142 строки
6 KB
Daria Tiurina
RenderFragment serialization (#66528)
19 май 2026, 22:37
Не верифицирован
19 май 2026, 22:37
654900e
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.Components.Endpoints; public class SSRRenderModeBoundaryTest { // While most aspects of SSRRenderModeBoundary are only interesting to test E2E, // the configuration validation aspect is better covered as unit tests because // otherwise we would need many different E2E test app configurations. [Fact] public void DoesNotAssertAboutConfiguredRenderModesOnUnknownEndpoints() { // Arrange: an endpoint with no ConfiguredRenderModesMetadata var httpContext = CreateHttpContext(); httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(), null)); // Act/Assert: no exception means we're OK new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveWebAssemblyRenderMode()); new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveAutoRenderMode()); } [Fact] public void ThrowsIfServerRenderModeUsedAndNotConfigured() { // Arrange var httpContext = CreateHttpContext(); PrepareEndpoint(httpContext, new WebAssemblyRenderModeSubclass()); // Act/Assert var ex = Assert.Throws<InvalidOperationException>(() => new SSRRenderModeBoundary( httpContext, typeof(TestComponent), new ServerRenderModeSubclass())); Assert.Contains($"A component of type '{typeof(TestComponent)}' has render mode '{nameof(ServerRenderModeSubclass)}'", ex.Message); Assert.Contains($"add a call to 'AddInteractiveServerRenderMode'", ex.Message); } [Fact] public void ThrowsIfWebAssemblyRenderModeUsedAndNotConfigured() { // Arrange var httpContext = CreateHttpContext(); PrepareEndpoint(httpContext, new ServerRenderModeSubclass()); // Act/Assert var ex = Assert.Throws<InvalidOperationException>(() => new SSRRenderModeBoundary( httpContext, typeof(TestComponent), new WebAssemblyRenderModeSubclass())); Assert.Contains($"A component of type '{typeof(TestComponent)}' has render mode '{nameof(WebAssemblyRenderModeSubclass)}'", ex.Message); Assert.Contains($"add a call to 'AddInteractiveWebAssemblyRenderMode'", ex.Message); } [Fact] public void ThrowsIfAutoRenderModeUsedAndServerNotConfigured() { // Arrange var httpContext = CreateHttpContext(); PrepareEndpoint(httpContext, new WebAssemblyRenderModeSubclass()); // Act/Assert var ex = Assert.Throws<InvalidOperationException>(() => new SSRRenderModeBoundary( httpContext, typeof(TestComponent), new AutoRenderModeSubclass())); Assert.Contains($"A component of type '{typeof(TestComponent)}' has render mode '{nameof(AutoRenderModeSubclass)}'", ex.Message); Assert.Contains($"add a call to 'AddInteractiveServerRenderMode'", ex.Message); } [Fact] public void ThrowsIfAutoRenderModeUsedAndWebAssemblyNotConfigured() { // Arrange var httpContext = CreateHttpContext(); PrepareEndpoint(httpContext, new ServerRenderModeSubclass()); // Act/Assert var ex = Assert.Throws<InvalidOperationException>(() => new SSRRenderModeBoundary( httpContext, typeof(TestComponent), new AutoRenderModeSubclass())); Assert.Contains($"A component of type '{typeof(TestComponent)}' has render mode '{nameof(AutoRenderModeSubclass)}'", ex.Message); Assert.Contains($"add a call to 'AddInteractiveWebAssemblyRenderMode'", ex.Message); } private static void PrepareEndpoint(HttpContext httpContext, params IComponentRenderMode[] configuredModes) { httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection( new ConfiguredRenderModesMetadata(configuredModes)), null)); } private static DefaultHttpContext CreateHttpContext() { return new DefaultHttpContext { RequestServices = new ServiceCollection() .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .BuildServiceProvider(), }; } class TestComponent : IComponent { public void Attach(RenderHandle renderHandle) => throw new NotImplementedException(); public Task SetParametersAsync(ParameterView parameters) => throw new NotImplementedException(); } public static IEnumerable<object[]> ComponentKeyTestData() { yield return new object[] { "test-string-key", "test-string-key" }; yield return new object[] { 42, "42" }; yield return new object[] { Guid.Parse("12345678-1234-1234-1234-123456789012"), "12345678-1234-1234-1234-123456789012" }; yield return new object[] { 123.45, "123.45" }; yield return new object[] { new DateTime(2023, 12, 25, 10, 30, 0, DateTimeKind.Utc), "12/25/2023 10:30:00" }; yield return new object[] { null, string.Empty }; yield return new object[] { new object(), string.Empty }; } [Theory] [MemberData(nameof(ComponentKeyTestData))] public void GetComponentMarkerKey_WorksWithVariousKeyTypes(object componentKey, string expectedFormattedKey) { // Arrange var httpContext = CreateHttpContext(); var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); // Act var markerKey = boundary.GetComponentMarkerKey(1, componentKey); // Assert Assert.Equal(expectedFormattedKey, markerKey.FormattedComponentKey); Assert.NotEmpty(markerKey.LocationHash); } class ServerRenderModeSubclass : InteractiveServerRenderMode { } class WebAssemblyRenderModeSubclass : InteractiveWebAssemblyRenderMode { } class AutoRenderModeSubclass : InteractiveAutoRenderMode { } }