-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathRazorComponentResultTest.cs
523 lines (452 loc) · 22.7 KB
/
RazorComponentResultTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Components.Endpoints.Forms;
using Microsoft.AspNetCore.Components.Endpoints.Tests.TestComponents;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Infrastructure;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Internal;
using Moq;
namespace Microsoft.AspNetCore.Components.Endpoints;
public class RazorComponentResultTest
{
[Fact]
public void RejectsNullParameters()
{
Assert.Throws<ArgumentNullException>(() => new RazorComponentResult(typeof(SimpleComponent), (object)null));
Assert.Throws<ArgumentNullException>(() => new RazorComponentResult(typeof(SimpleComponent), null));
}
[Fact]
public void AcceptsDictionaryParameters()
{
var paramsDict = new Dictionary<string, object> { { "First", 123 } };
var result = new RazorComponentResult(typeof(SimpleComponent), paramsDict);
Assert.Single(result.Parameters);
Assert.Equal(123, result.Parameters["First"]);
Assert.Same(paramsDict, result.Parameters);
}
[Fact]
public void AcceptsObjectParameters()
{
var result = new RazorComponentResult(typeof(SimpleComponent), new { Param1 = 123, Param2 = "Another" });
Assert.Equal(2, result.Parameters.Count);
Assert.Equal(123, result.Parameters["Param1"]);
Assert.Equal("Another", result.Parameters["Param2"]);
}
[Fact]
public async Task CanRenderComponentStatically()
{
// Arrange
var result = new RazorComponentResult<SimpleComponent>();
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act
await result.ExecuteAsync(httpContext);
// Assert
Assert.Equal("<h1>Hello from SimpleComponent</h1>", GetStringContent(responseBody));
Assert.Equal("text/html; charset=utf-8", httpContext.Response.ContentType);
Assert.Equal(200, httpContext.Response.StatusCode);
}
[Fact]
public async Task ResponseIncludesStatusCodeAndContentTypeAndHtml()
{
// Arrange
var result = new RazorComponentResult<SimpleComponent>
{
StatusCode = 123,
ContentType = "application/test-content-type",
};
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act
await result.ExecuteAsync(httpContext);
// Assert
Assert.Equal("<h1>Hello from SimpleComponent</h1>", GetStringContent(responseBody));
Assert.Equal("application/test-content-type", httpContext.Response.ContentType);
Assert.Equal(123, httpContext.Response.StatusCode);
}
[Fact]
public async Task PerformsStreamingRendering()
{
// Arrange
var tcs = new TaskCompletionSource();
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act/Assert 1: Emits the initial pre-quiescent output to the response
var result = new RazorComponentResult(typeof(StreamingAsyncLoadingComponent),
PropertyHelper.ObjectToDictionary(new { LoadingTask = tcs.Task }).AsReadOnly());
var completionTask = result.ExecuteAsync(httpContext);
await WaitForContentWrittenAsync(responseBody);
Assert.Equal(
"<!--bl:X-->Loading task status: WaitingForActivation<!--/bl:X-->",
MaskComponentIds(GetStringContent(responseBody)));
// Assert 2: Result task remains incomplete for as long as the component's loading operation remains in flight
// This keeps the HTTP response open
await Task.Yield();
Assert.False(completionTask.IsCompleted);
// Act/Assert 3: When loading completes, it emits a streaming batch update and completes the response
tcs.SetResult();
await completionTask;
Assert.Equal(
"<!--bl:X-->Loading task status: WaitingForActivation<!--/bl:X--><blazor-ssr><template blazor-component-id=\"X\">Loading task status: RanToCompletion</template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
MaskComponentIds(GetStringContent(responseBody)));
}
[Fact]
public async Task EmitsEachComponentOnlyOncePerStreamingUpdate_WhenAComponentRendersTwice()
{
// Arrange
var tcs = new TaskCompletionSource();
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act/Assert 1: Emits the initial pre-quiescent output to the response
var result = new RazorComponentResult(typeof(DoubleRenderingStreamingAsyncComponent),
PropertyHelper.ObjectToDictionary(new { WaitFor = tcs.Task }).AsReadOnly());
var completionTask = result.ExecuteAsync(httpContext);
await WaitForContentWrittenAsync(responseBody);
Assert.Equal(
"<!--bl:X-->Loading...<!--/bl:X-->",
MaskComponentIds(GetStringContent(responseBody)));
// Act/Assert 2: When loading completes, it emits a streaming batch update with only one copy of the final output,
// despite the RenderBatch containing two diffs from the component
tcs.SetResult();
await completionTask;
Assert.Equal(
"<!--bl:X-->Loading...<!--/bl:X--><blazor-ssr><template blazor-component-id=\"X\">Loaded</template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
MaskComponentIds(GetStringContent(responseBody)));
}
[Fact]
public async Task EmitsEachComponentOnlyOncePerStreamingUpdate_WhenAnAncestorAlsoUpdated()
{
// Since the HTML rendered for each component also includes all its descendants, we don't
// want to render output for any component that also has an ancestor in the set of updates
// (as it would then be output twice)
// Arrange
var tcs = new TaskCompletionSource();
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act/Assert 1: Emits the initial pre-quiescent output to the response
var result = new RazorComponentResult(typeof(StreamingComponentWithChild),
PropertyHelper.ObjectToDictionary(new { LoadingTask = tcs.Task }).AsReadOnly());
var completionTask = result.ExecuteAsync(httpContext);
await WaitForContentWrittenAsync(responseBody);
var expectedInitialHtml = "<!--bl:X-->[LoadingTask: WaitingForActivation]\n<!--bl:X-->[Child render: 1]\n<!--/bl:X--><!--/bl:X-->";
Assert.Equal(
expectedInitialHtml,
MaskComponentIds(GetStringContent(responseBody)));
// Act/Assert 2: When loading completes, it emits a streaming batch update in which the
// child is present only within the parent markup, not as a separate entry
tcs.SetResult();
await completionTask;
Assert.Equal(
$"{expectedInitialHtml}<blazor-ssr><template blazor-component-id=\"X\">[LoadingTask: RanToCompletion]\n<!--bl:X-->[Child render: 2]\n<!--/bl:X--></template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
MaskComponentIds(GetStringContent(responseBody)));
}
[Fact]
public async Task WaitsForQuiescenceIfPreventStreamingRenderingIsTrue()
{
// Arrange
var tcs = new TaskCompletionSource();
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act/Assert: Doesn't complete until loading finishes
var result = new RazorComponentResult(typeof(StreamingAsyncLoadingComponent),
PropertyHelper.ObjectToDictionary(new { LoadingTask = tcs.Task }).AsReadOnly())
{
PreventStreamingRendering = true
};
var completionTask = result.ExecuteAsync(httpContext);
await Task.Yield();
Assert.False(completionTask.IsCompleted);
// Act/Assert: Does complete when loading finishes
tcs.SetResult();
await completionTask;
Assert.Equal(
"<!--bl:X-->Loading task status: RanToCompletion<!--/bl:X-->",
MaskComponentIds(GetStringContent(responseBody)));
}
[Fact]
public async Task SupportsLayouts()
{
// Arrange
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act
await new RazorComponentResult(typeof(ComponentWithLayout)).ExecuteAsync(httpContext);
// Assert
Assert.Equal($"[TestParentLayout with content: [TestLayout with content: Page\n]\n]\n", GetStringContent(responseBody));
}
[Fact]
public async Task OnNavigationBeforeResponseStarted_Redirects()
{
// Arrange
var httpContext = GetTestHttpContext();
// Act
await new RazorComponentResult(typeof(ComponentThatRedirectsSynchronously)).ExecuteAsync(httpContext);
// Assert
Assert.Equal("https://test/somewhere/else", httpContext.Response.Headers.Location);
}
[Fact]
public async Task OnNavigationAfterResponseStarted_WithStreamingOff_Throws()
{
// Arrange
var httpContext = GetTestHttpContext();
var responseMock = new Mock<IHttpResponseFeature>();
responseMock.Setup(r => r.HasStarted).Returns(true);
responseMock.Setup(r => r.Headers).Returns(new HeaderDictionary());
httpContext.Features.Set(responseMock.Object);
// Act
var result = new RazorComponentResult(typeof(StreamingComponentThatRedirectsAsynchronously))
{
PreventStreamingRendering = true
};
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
() => result.ExecuteAsync(httpContext));
// Assert
Assert.Contains("A navigation command was attempted during prerendering after the server already started sending the response", ex.Message);
}
[Fact]
public async Task OnNavigationAfterResponseStarted_WithStreamingOn_EmitsCommand()
{
// Arrange
var httpContext = GetTestHttpContext();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
// Act
await new RazorComponentResult(typeof(StreamingComponentThatRedirectsAsynchronously)).ExecuteAsync(httpContext);
// Assert
var markup = MaskComponentIds(GetStringContent(responseBody));
Assert.StartsWith(
"<!--bl:X-->Some output\n<!--/bl:X--><blazor-ssr><template type=\"redirection\">_framework/opaque-redirect?url=",
markup);
Assert.EndsWith(
"</template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
markup);
}
[Fact]
public async Task OnUnhandledExceptionBeforeResponseStarted_Throws()
{
// Arrange
var httpContext = GetTestHttpContext();
// Act
var ex = await Assert.ThrowsAsync<InvalidTimeZoneException>(() =>
new RazorComponentResult(typeof(ComponentThatThrowsSynchronously)).ExecuteAsync(httpContext));
// Assert
Assert.Contains("Test message", ex.Message);
}
[Fact]
public async Task OnUnhandledExceptionAfterResponseStarted_WithStreamingOff_Throws()
{
// Arrange
var httpContext = GetTestHttpContext();
// Act
var ex = await Assert.ThrowsAsync<InvalidTimeZoneException>(
() => new RazorComponentResult(typeof(StreamingComponentThatThrowsAsynchronously)) { PreventStreamingRendering = true }
.ExecuteAsync(httpContext));
// Assert
Assert.Contains("Test message", ex.Message);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task OnUnhandledExceptionAfterResponseStarted_WithStreamingOn_EmitsCommand(bool isDevelopmentEnvironment)
{
// Arrange
var httpContext = GetTestHttpContext(isDevelopmentEnvironment ? Environments.Development : Environments.Production);
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
var expectedResponseExceptionInfo = isDevelopmentEnvironment
? "System.InvalidTimeZoneException: Test message with <b>markup</b>"
: "There was an unhandled exception on the current request. For more details turn on detailed exceptions by setting 'DetailedErrors: true' in 'appSettings.Development.json'";
// Act
var ex = await Assert.ThrowsAsync<InvalidTimeZoneException>(
() => new RazorComponentResult(typeof(StreamingComponentThatThrowsAsynchronously))
.ExecuteAsync(httpContext));
// Assert
Assert.Contains("Test message with <b>markup</b>", ex.Message);
Assert.Contains(
$"<!--bl:X-->Some output\n<!--/bl:X--><blazor-ssr><template type=\"error\">{expectedResponseExceptionInfo}",
MaskComponentIds(GetStringContent(responseBody)));
}
[Fact]
public async Task StreamingRendering_IsOffByDefault_AndCanBeEnabledForSubtree()
{
// Arrange
var testContext = PrepareVaryStreamingScenariosTests();
var initialOutputTask = testContext.Renderer.NonStreamingPendingTasksCompletion;
// Act/Assert: Even if all other blocking tasks complete, we don't produce output until the top-level
// nonstreaming component completes
Assert.NotNull(initialOutputTask);
testContext.WithinNestedNonstreamingRegionTask.SetResult();
await Task.Yield(); // Just to show it's still not completed after
Assert.False(initialOutputTask.IsCompleted);
// Act/Assert: Produce initial output, noting absence of streaming markers at top level
testContext.TopLevelComponentTask.SetResult();
await initialOutputTask;
await WaitForContentWrittenAsync(testContext.ResponseBody);
var html = MaskComponentIds(GetStringContent(testContext.ResponseBody));
Assert.StartsWith("[Top level component: Loaded]", html);
Assert.Contains("[Within streaming region: <!--bl:X-->Loading...<!--/bl:X-->]", html);
Assert.Contains("[Within nested nonstreaming region: Loaded]", html);
Assert.DoesNotContain("blazor-ssr", html);
// Act/Assert: Complete the streaming
testContext.WithinStreamingRegionTask.SetResult();
await testContext.Quiescence;
html = GetStringContent(testContext.ResponseBody);
Assert.EndsWith(
"<blazor-ssr><template blazor-component-id=\"X\">Loaded</template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
MaskComponentIds(html));
}
[Fact]
public async Task StreamingRendering_CanBeDisabledForSubtree()
{
// Arrange
var testContext = PrepareVaryStreamingScenariosTests();
var initialOutputTask = testContext.Renderer.NonStreamingPendingTasksCompletion;
// Act/Assert: Even if all other nonblocking tasks complete, we don't produce output until
// the component in the nonstreaming subtree is quiescent
Assert.NotNull(initialOutputTask);
testContext.TopLevelComponentTask.SetResult();
await Task.Yield(); // Just to show it's still not completed after
Assert.False(initialOutputTask.IsCompleted);
// Act/Assert: Does produce output when nonstreaming subtree is quiescent
testContext.WithinNestedNonstreamingRegionTask.SetResult();
await initialOutputTask;
await WaitForContentWrittenAsync(testContext.ResponseBody);
var html = MaskComponentIds(GetStringContent(testContext.ResponseBody));
Assert.Contains("[Within streaming region: <!--bl:X-->Loading...<!--/bl:X-->]", html);
Assert.DoesNotContain("blazor-ssr", html);
// Assert: No boundary markers around nonstreaming components, even if they are nested in a streaming region
Assert.Contains("[Top level component: Loaded]", html);
Assert.Contains("[Within nested nonstreaming region: Loaded]", html);
// Act/Assert: Complete the streaming
testContext.WithinStreamingRegionTask.SetResult();
await testContext.Quiescence;
html = GetStringContent(testContext.ResponseBody);
Assert.EndsWith(
"<blazor-ssr><template blazor-component-id=\"X\">Loaded</template><blazor-ssr-end></blazor-ssr-end></blazor-ssr>",
MaskComponentIds(html));
}
// We don't want these tests to be hardcoded for specific component ID numbers, so replace them all with X for assertions
private static readonly Regex TemplateElementComponentIdRegex = new Regex("blazor-component-id=\"\\d+\"");
private static readonly Regex OpenBoundaryMarkerRegex = new Regex("<!--bl:\\d+-->");
private static readonly Regex CloseBoundaryMarkerRegex = new Regex("<!--/bl:\\d+-->");
private static string MaskComponentIds(string html)
{
html = TemplateElementComponentIdRegex.Replace(html, "blazor-component-id=\"X\"");
html = OpenBoundaryMarkerRegex.Replace(html, "<!--bl:X-->");
html = CloseBoundaryMarkerRegex.Replace(html, "<!--/bl:X-->");
return html;
}
private VaryStreamingScenariosContext PrepareVaryStreamingScenariosTests()
{
var httpContext = GetTestHttpContext();
var renderer = httpContext.RequestServices.GetRequiredService<EndpointHtmlRenderer>();
var responseBody = new MemoryStream();
httpContext.Response.Body = responseBody;
var topLevelComponentTask = new TaskCompletionSource();
var withinStreamingRegionTask = new TaskCompletionSource();
var withinNestedNonstreamingRegionTask = new TaskCompletionSource();
var parameters = new Dictionary<string, object>
{
{ nameof(VaryStreamingScenarios.TopLevelComponentTask), topLevelComponentTask.Task },
{ nameof(VaryStreamingScenarios.WithinStreamingRegionTask), withinStreamingRegionTask.Task },
{ nameof(VaryStreamingScenarios.WithinNestedNonstreamingRegionTask), withinNestedNonstreamingRegionTask.Task },
};
var result = new RazorComponentResult(typeof(VaryStreamingScenarios), parameters);
var quiescence = result.ExecuteAsync(httpContext);
return new(renderer, quiescence, responseBody, topLevelComponentTask, withinStreamingRegionTask, withinNestedNonstreamingRegionTask);
}
private record struct VaryStreamingScenariosContext(
EndpointHtmlRenderer Renderer,
Task Quiescence,
MemoryStream ResponseBody,
TaskCompletionSource TopLevelComponentTask,
TaskCompletionSource WithinStreamingRegionTask,
TaskCompletionSource WithinNestedNonstreamingRegionTask);
private static string GetStringContent(MemoryStream stream)
{
stream.Position = 0;
return new StreamReader(stream).ReadToEnd().Replace("\r\n", "\n");
}
public static DefaultHttpContext GetTestHttpContext(string environmentName = null)
{
var mockWebHostEnvironment = Mock.Of<IWebHostEnvironment>(
x => x.EnvironmentName == (environmentName ?? Environments.Production));
var serviceCollection = new ServiceCollection()
.AddAntiforgery()
.AddSingleton(new DiagnosticListener("test"))
.AddSingleton<IWebHostEnvironment>(mockWebHostEnvironment)
.AddSingleton<EndpointHtmlRenderer>()
.AddSingleton<IComponentPrerenderer>(services => services.GetRequiredService<EndpointHtmlRenderer>())
.AddSingleton<NavigationManager, FakeNavigationManager>()
.AddSingleton<ServerComponentSerializer>()
.AddSingleton<ComponentStatePersistenceManager>()
.AddSingleton<IDataProtectionProvider, FakeDataProtectionProvider>()
.AddSingleton<HttpContextFormDataProvider>()
.AddSingleton<ComponentStatePersistenceManager>()
.AddSingleton<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State)
.AddSingleton<AntiforgeryStateProvider, EndpointAntiforgeryStateProvider>()
.AddLogging();
var result = new DefaultHttpContext { RequestServices = serviceCollection.BuildServiceProvider() };
result.Request.Scheme = "https";
result.Request.Host = new HostString("test");
return result;
}
// Some tests want to observe the output state when some content has been written, but without waiting for
// the whole streaming task to complete. There isn't any public API that exposes this specific phase, so
// rather than making the renderer track additional tasks that would only be used in tests, this allows the
// tests to continue once the first batch of output was written.
private static async Task WaitForContentWrittenAsync(Stream stream, TimeSpan? timeout = default)
{
var timeoutRemaining = timeout.GetValueOrDefault(TimeSpan.FromSeconds(1));
var pollInterval = TimeSpan.FromMilliseconds(50);
do
{
if (stream.Position > 0)
{
return;
}
await Task.Delay(pollInterval);
timeoutRemaining = timeoutRemaining - pollInterval;
} while (timeoutRemaining.TotalMilliseconds > 0);
Assert.Fail("Timeout elapsed without content being written");
}
class FakeDataProtectionProvider : IDataProtectionProvider
{
public IDataProtector CreateProtector(string purpose)
=> new FakeDataProtector();
class FakeDataProtector : IDataProtector
{
public IDataProtector CreateProtector(string purpose) => this;
public byte[] Protect(byte[] plaintext) => new byte[] { 1, 2, 3 };
public byte[] Unprotect(byte[] protectedData) => throw new NotImplementedException();
}
}
class FakeNavigationManager : NavigationManager, IHostEnvironmentNavigationManager
{
public new void Initialize(string baseUri, string uri)
=> base.Initialize(baseUri, uri);
protected override void NavigateToCore(string uri, NavigationOptions options)
{
// Equivalent to what RemoteNavigationManager would do
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
}
}
}