-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathBootResourceCachingTest.cs
More file actions
150 lines (134 loc) · 6.98 KB
/
Copy pathBootResourceCachingTest.cs
File metadata and controls
150 lines (134 loc) · 6.98 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using HostedInAspNet.Server;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Components.E2ETest.Tests;
// Disabling parallelism for these tests because of flakiness
[CollectionDefinition(nameof(BootResourceCachingTest), DisableParallelization = true)]
[Collection(nameof(BootResourceCachingTest))]
public class BootResourceCachingTest
: ServerTestBase<AspNetSiteServerFixture>
{
// The cache name is derived from the application's base href value (in this case, '/')
private const string CacheName = "dotnet-resources-/";
public BootResourceCachingTest(
BrowserFixture browserFixture,
AspNetSiteServerFixture serverFixture,
ITestOutputHelper output)
: base(browserFixture, serverFixture, output)
{
serverFixture.BuildWebHostMethod = Program.BuildWebHost;
}
public override Task InitializeAsync()
{
return base.InitializeAsync(Guid.NewGuid().ToString());
}
[Fact]
public void CachesResourcesAfterFirstLoad()
{
// On the first load, we have to fetch everything
Navigate("/");
WaitUntilLoaded();
var initialResourcesRequested = GetAndClearRequestedPaths();
Assert.NotEmpty(initialResourcesRequested.Where(path => path.EndsWith("/blazor.boot.json", StringComparison.Ordinal)));
Assert.NotEmpty(initialResourcesRequested.Where(path => path.EndsWith("/dotnet.native.wasm", StringComparison.Ordinal)));
Assert.NotEmpty(initialResourcesRequested.Where(path => path.EndsWith(".js", StringComparison.Ordinal)));
Assert.NotEmpty(initialResourcesRequested.Where(path => path.EndsWith(".wasm", StringComparison.Ordinal)));
// On subsequent loads, we skip the items referenced from blazor.boot.json
// which includes .wasm (original .dll) files and dotnet.native.wasm
Navigate("about:blank");
Browser.Equal(string.Empty, () => Browser.Title);
Navigate("/");
WaitUntilLoaded();
var subsequentResourcesRequested = GetAndClearRequestedPaths();
Assert.NotEmpty(initialResourcesRequested.Where(path => path.EndsWith("/blazor.boot.json", StringComparison.Ordinal)));
Assert.Empty(subsequentResourcesRequested.Where(path => path.EndsWith("/dotnet.native.wasm", StringComparison.Ordinal)));
Assert.NotEmpty(subsequentResourcesRequested.Where(path => path.EndsWith(".js", StringComparison.Ordinal)));
Assert.Empty(subsequentResourcesRequested.Where(path => path.EndsWith(".wasm", StringComparison.Ordinal) && !path.EndsWith("/dotnet.native.wasm", StringComparison.Ordinal)));
}
[Fact]
public void IncrementallyUpdatesCache()
{
// Perform a first load to populate the cache
Navigate("/");
WaitUntilLoaded();
var cacheEntryUrls1 = GetCacheEntryUrls();
var cacheEntryForComponentsDll = cacheEntryUrls1.Single(url => url.Contains("/Microsoft.AspNetCore.Components.wasm"));
var cacheEntryForDotNetWasm = cacheEntryUrls1.Single(url => url.Contains("/dotnet.native.wasm"));
var cacheEntryForDotNetWasmWithChangedHash = cacheEntryForDotNetWasm.Replace(".sha256-", ".sha256-different");
// Remove some items we do need, and add an item we don't need
RemoveCacheEntry(cacheEntryForComponentsDll);
RemoveCacheEntry(cacheEntryForDotNetWasm);
AddCacheEntry(cacheEntryForDotNetWasmWithChangedHash, "ignored content");
var cacheEntryUrls2 = GetCacheEntryUrls();
Assert.DoesNotContain(cacheEntryForComponentsDll, cacheEntryUrls2);
Assert.DoesNotContain(cacheEntryForDotNetWasm, cacheEntryUrls2);
Assert.Contains(cacheEntryForDotNetWasmWithChangedHash, cacheEntryUrls2);
// On the next load, we'll fetch only the items we need (not things already cached)
GetAndClearRequestedPaths();
Navigate("about:blank");
Browser.Equal(string.Empty, () => Browser.Title);
Navigate("/");
WaitUntilLoaded();
var subsequentResourcesRequested = GetAndClearRequestedPaths();
Assert.Collection(subsequentResourcesRequested.Where(url => url.Contains(".wasm")),
requestedDll => Assert.Contains("/Microsoft.AspNetCore.Components.wasm", requestedDll),
requestedDll => Assert.Contains("/dotnet.native.wasm", requestedDll));
// We also update the cache (add new items, remove unnecessary items)
var cacheEntryUrls3 = GetCacheEntryUrls();
Assert.Contains(cacheEntryForComponentsDll, cacheEntryUrls3);
Assert.Contains(cacheEntryForDotNetWasm, cacheEntryUrls3);
Assert.DoesNotContain(cacheEntryForDotNetWasmWithChangedHash, cacheEntryUrls3);
}
private IReadOnlyCollection<string> GetCacheEntryUrls()
{
var js = @"
(async function(cacheName, completedCallback) {
const cache = await caches.open(cacheName);
const keys = await cache.keys();
const urls = keys.map(r => r.url);
completedCallback(urls);
}).apply(null, arguments)";
var jsExecutor = (IJavaScriptExecutor)Browser;
var result = (IEnumerable<object>)jsExecutor.ExecuteAsyncScript(js, CacheName);
return result.Cast<string>().ToList();
}
private void RemoveCacheEntry(string url)
{
var js = @"
(async function(cacheName, urlToRemove, completedCallback) {
const cache = await caches.open(cacheName);
await cache.delete(urlToRemove);
completedCallback();
}).apply(null, arguments)";
((IJavaScriptExecutor)Browser).ExecuteAsyncScript(js, CacheName, url);
}
private void AddCacheEntry(string url, string content)
{
var js = @"
(async function(cacheName, urlToAdd, contentToAdd, completedCallback) {
const cache = await caches.open(cacheName);
await cache.put(urlToAdd, new Response(contentToAdd));
completedCallback();
}).apply(null, arguments)";
((IJavaScriptExecutor)Browser).ExecuteAsyncScript(js, CacheName, url, content);
}
private IReadOnlyCollection<string> GetAndClearRequestedPaths()
{
var requestLog = _serverFixture.Host.Services.GetRequiredService<BootResourceRequestLog>();
var result = requestLog.RequestPaths.ToList();
requestLog.Clear();
return result;
}
private void WaitUntilLoaded()
{
var element = Browser.Exists(By.TagName("h1"), TimeSpan.FromSeconds(30));
Browser.Equal("Hello, world!", () => element.Text);
}
}