Files
VolcanicCore/tests/VolcanicCore.Tests/StaticFilesFallbackTests.cs

79 lines
2.7 KiB
C#

using System.Net;
using Microsoft.AspNetCore.Hosting;
namespace VolcanicCore.Tests;
public class StaticFilesFallbackTests
{
// Every test writes its own tiny index.html into a per-test wwwroot dir so
// we can assert the fallback served it verbatim.
private sealed class WwwFactory : WebFactory
{
public string ContentRoot { get; } = Path.Combine(Path.GetTempPath(), $"vc-www-{Guid.NewGuid():N}");
public WwwFactory()
{
Directory.CreateDirectory(Path.Combine(ContentRoot, "wwwroot"));
File.WriteAllText(Path.Combine(ContentRoot, "wwwroot", "index.html"),
"<!doctype html><title>SPA</title>");
File.WriteAllText(Path.Combine(ContentRoot, "wwwroot", "static-thing.txt"),
"hello static");
}
protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseContentRoot(ContentRoot);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && Directory.Exists(ContentRoot))
Directory.Delete(ContentRoot, recursive: true);
}
}
[Fact]
public async Task Api_route_still_wins()
{
using var f = new WwwFactory();
var c = f.CreateClient();
var res = await c.GetAsync("/api/health");
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
var body = await res.Content.ReadAsStringAsync();
Assert.Contains("\"ok\":true", body);
}
[Fact]
public async Task Unknown_route_falls_back_to_index_html()
{
using var f = new WwwFactory();
var c = f.CreateClient();
var res = await c.GetAsync("/workflows/abc123");
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
Assert.Contains("SPA", await res.Content.ReadAsStringAsync());
}
[Fact]
public async Task Static_asset_is_served_directly()
{
using var f = new WwwFactory();
var c = f.CreateClient();
var res = await c.GetAsync("/static-thing.txt");
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
Assert.Equal("hello static", await res.Content.ReadAsStringAsync());
}
[Fact]
public async Task Webhook_route_still_wins_over_fallback()
{
using var f = new WwwFactory();
var c = f.CreateClient();
var res = await c.PostAsync("/wh/nonexistent/foo", new StringContent(""));
// Router isn't registered → 404 from WebhookRouter, NOT the SPA index.
Assert.Equal(HttpStatusCode.NotFound, res.StatusCode);
Assert.DoesNotContain("SPA", await res.Content.ReadAsStringAsync());
}
}