Skip to content

Commit

Permalink
feat(templates): add missing tests for download size of BlazorWebAsse…
Browse files Browse the repository at this point in the history
…mbly in Boilerplate #9263 (#9264)
  • Loading branch information
mjebrahimi authored Nov 17, 2024
1 parent 4b70064 commit 853b58f
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@
"src/Tests/Services/UserService.cs",
"src/Tests/Services/FakePhoneService.cs",
"src/Tests/Services/FakeGoogleRecaptchaHttpClient.cs",
"src/Tests/Extensions/PlaywrightAssetCachingExtensions.cs",
"src/Tests/Extensions/PlaywrightCacheStorageExtensions.cs",
"src/Tests/Extensions/PlaywrightNetworkExtensions.cs",
"src/Tests/Extensions/PlaywrightCacheExtensions.cs",
"src/Tests/Extensions/PlaywrightHydrationExtensions.cs",
"src/Tests/Extensions/BrowserContextExtensions.cs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Boilerplate.Tests.Extensions;

public static partial class PlaywrightCacheExtensions
public static partial class PlaywrightAssetCachingExtensions
{
private static readonly ConcurrentDictionary<string, (byte[] Body, Dictionary<string, string> Headers)> cachedResponses = [];
public static ConcurrentDictionary<string, (byte[] Body, Dictionary<string, string> Headers)> CachedResponses { get; } = [];

public static Task EnableBlazorWasmCaching(this IPage page) => page.EnableAssetCaching(BlazorWasmRegex());

Expand All @@ -27,12 +27,12 @@ private static async Task CacheHandler(IRoute route)
{
var url = new Uri(route.Request.Url).PathAndQuery;

if (cachedResponses.TryGetValue(url, out var cachedResponse) is false)
if (CachedResponses.TryGetValue(url, out var cachedResponse) is false)
{
var response = await route.FetchAsync();
var body = await response.BodyAsync();
cachedResponse = (body, response.Headers);
cachedResponses[url] = cachedResponse;
CachedResponses[url] = cachedResponse;
}

await route.FulfillAsync(new RouteFulfillOptions
Expand All @@ -43,10 +43,19 @@ await route.FulfillAsync(new RouteFulfillOptions
});
}

public static void ClearCache() => cachedResponses.Clear();
public static bool ContainsAsset(Regex regex) => CachedResponses.Keys.Any(regex.IsMatch);

public static bool ContainsAsset(Regex regex) => cachedResponses.Keys.Any(regex.IsMatch);
public static bool ContainsAsset(string url) => CachedResponses.Keys.Any(url.Contains);

public static void ClearBlazorWasmCache() => ClearCache(BlazorWasmRegex());

public static void ClearCache() => CachedResponses.Clear();

public static void ClearCache(Regex regex) => CachedResponses.Where(x => regex.IsMatch(x.Key)).ToList().ForEach(key => CachedResponses.TryRemove(key));

public static void ClearCache(string url) => CachedResponses.Where(x => url.Contains(x.Key)).ToList().ForEach(key => CachedResponses.TryRemove(key));

//Glob pattern: /_framework/*.{wasm|pdb|dat}?v=sha256-*
[GeneratedRegex(@"\/_framework\/[\w\.]+\.((wasm)|(pdb)|(dat))\?v=sha256-.+")]
private static partial Regex BlazorWasmRegex();
public static partial Regex BlazorWasmRegex();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Text.RegularExpressions;

namespace Boilerplate.Tests.Extensions;

public static class PlaywrightCacheStorageExtensions
{
public static async Task DeleteCacheStorage(this IPage page, CacheId? cacheId = null)
{
// Chrome DevTools Protocol
// https://chromedevtools.github.io/devtools-protocol/tot/CacheStorage/

cacheId ??= new();
await using var client = await page.Context.NewCDPSessionAsync(page);
await client.SendAsync("CacheStorage.deleteCache", new() { ["cacheId"] = cacheId.Value });
}

public static async Task<CacheEntries> GetCacheStorageEntries(this IPage page, CacheId? cacheId = null)
{
cacheId ??= new();
await using var client = await page.Context.NewCDPSessionAsync(page);
var json = await client.SendAsync("CacheStorage.requestEntries", new() { ["cacheId"] = cacheId.Value });
return json.Value.Deserialize<CacheEntries>()!;
}
}

public record CacheId(string Origin = "http://localhost:5000/", string CacheName = "dotnet-resources-/")
{
public string Value { get; init; } = $"{Origin}|{CacheName}";

public override string ToString() => Value;
}

public class CacheEntries
{
[JsonPropertyName("cacheDataEntries")]
public List<CacheEntry> CacheDataEntries { get; set; } = [];

public List<CacheEntry> GetCacheEntries(Regex regex) => CacheDataEntries.Where(x => regex.IsMatch(x.RequestURL)).ToList();

public List<CacheEntry> GetCacheEntries(string url) => CacheDataEntries.Where(x => url.Contains(x.RequestURL)).ToList();

public bool ContainsCacheEntry(Regex regex) => CacheDataEntries.Exists(x => regex.IsMatch(x.RequestURL));

public bool ContainsCacheEntry(string url) => CacheDataEntries.Exists(x => url.Contains(x.RequestURL));
}

public class CacheEntry
{
[JsonPropertyName("requestURL")]
public string RequestURL { get; set; }

[JsonPropertyName("requestMethod")]
public string RequestMethod { get; set; }

[JsonPropertyName("responseTime")]
public double ResponseTime { get; set; }

[JsonPropertyName("responseStatus")]
public int ResponseStatus { get; set; }

[JsonPropertyName("responseStatusText")]
public string ResponseStatusText { get; set; }

[JsonPropertyName("responseType")]
public string ResponseType { get; set; }

[JsonPropertyName("requestHeaders")]
public List<RequestResponseHeader> RequestHeaders { get; set; }

[JsonPropertyName("responseHeaders")]
public List<RequestResponseHeader> ResponseHeaders { get; set; }
}

public class RequestResponseHeader
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("value")]
public string Value { get; set; }
}
Loading

0 comments on commit 853b58f

Please sign in to comment.