2026-08-11 11:59:18 +02:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
2026-11-04 20:55:14 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
2026-08-11 11:59:18 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2026-11-04 20:55:14 +01:00
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
2026-08-11 11:59:18 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
|
|
|
|
|
|
public class SameOriginHttpMessageHandlerTests
|
|
|
|
|
{
|
|
|
|
|
private sealed class CapturingHandler : HttpMessageHandler
|
|
|
|
|
{
|
|
|
|
|
public HttpRequestMessage? LastRequest { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(
|
|
|
|
|
HttpRequestMessage request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
LastRequest = request;
|
|
|
|
|
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 20:55:14 +01:00
|
|
|
[Fact]
|
|
|
|
|
public void ResolveFromServer_WildcardKestrelHost_UsesLoopbackAddress()
|
|
|
|
|
{
|
|
|
|
|
var addresses = new ServerAddressesFeature();
|
|
|
|
|
addresses.Addresses.Add("http://+:8080");
|
|
|
|
|
var features = new FeatureCollection();
|
|
|
|
|
features.Set<IServerAddressesFeature>(addresses);
|
|
|
|
|
var server = Substitute.For<IServer>();
|
|
|
|
|
server.Features.Returns(features);
|
|
|
|
|
|
|
|
|
|
ApiBaseUrlResolver.ResolveFromServer(server).ShouldBe("http://localhost:8080/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SendAsync_UsesKestrelBindingInsteadOfBrowserHostForServerCall()
|
|
|
|
|
{
|
|
|
|
|
var addresses = new ServerAddressesFeature();
|
|
|
|
|
addresses.Addresses.Add("http://+:8080");
|
|
|
|
|
var features = new FeatureCollection();
|
|
|
|
|
features.Set<IServerAddressesFeature>(addresses);
|
|
|
|
|
var server = Substitute.For<IServer>();
|
|
|
|
|
server.Features.Returns(features);
|
|
|
|
|
var httpContext = new DefaultHttpContext();
|
|
|
|
|
httpContext.Request.Scheme = "https";
|
|
|
|
|
httpContext.Request.Host = new HostString("public.example.net");
|
|
|
|
|
var accessor = Substitute.For<IHttpContextAccessor>();
|
|
|
|
|
accessor.HttpContext.Returns(httpContext);
|
|
|
|
|
var capture = new CapturingHandler();
|
|
|
|
|
var handler = CreateHandler(new AuthState(), accessor, capture, server);
|
|
|
|
|
using var client = new HttpClient(handler) { BaseAddress = new(ApiBaseUrlResolver.PlaceholderBase) };
|
|
|
|
|
|
|
|
|
|
await client.GetAsync("api/auth/bootstrap-status");
|
|
|
|
|
|
|
|
|
|
capture.LastRequest!.RequestUri!.GetLeftPart(UriPartial.Authority)
|
|
|
|
|
.ShouldBe("http://localhost:8080");
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 11:59:18 +02:00
|
|
|
private static SameOriginHttpMessageHandler CreateHandler(
|
|
|
|
|
AuthState authState,
|
|
|
|
|
IHttpContextAccessor? accessor = null,
|
2026-11-04 20:55:14 +01:00
|
|
|
HttpMessageHandler? inner = null,
|
|
|
|
|
IServer? server = null)
|
2026-08-11 11:59:18 +02:00
|
|
|
{
|
|
|
|
|
var config = Substitute.For<IConfiguration>();
|
|
|
|
|
var handler = new SameOriginHttpMessageHandler(
|
|
|
|
|
accessor ?? Substitute.For<IHttpContextAccessor>(),
|
|
|
|
|
config,
|
2026-11-04 20:55:14 +01:00
|
|
|
server ?? Substitute.For<IServer>(),
|
2026-08-11 11:59:18 +02:00
|
|
|
authState)
|
|
|
|
|
{
|
|
|
|
|
InnerHandler = inner ?? new CapturingHandler()
|
|
|
|
|
};
|
|
|
|
|
return handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SendAsync_CircuitSansHttpContext_AvecAuthCookie_AjouteCookie()
|
|
|
|
|
{
|
|
|
|
|
var authState = new AuthState { AuthCookie = "CfDJ8-ticket" };
|
|
|
|
|
var accessor = Substitute.For<IHttpContextAccessor>();
|
|
|
|
|
accessor.HttpContext.Returns((HttpContext?)null);
|
|
|
|
|
var capture = new CapturingHandler();
|
|
|
|
|
var handler = CreateHandler(authState, accessor, capture);
|
|
|
|
|
using var client = new HttpClient(handler);
|
|
|
|
|
|
|
|
|
|
await client.GetAsync("http://heimdall.local/api/agents");
|
|
|
|
|
|
|
|
|
|
// Le cookie capturé au SSR est propagé pour authentifier l'appel du circuit
|
|
|
|
|
capture.LastRequest.ShouldNotBeNull();
|
|
|
|
|
var cookies = capture.LastRequest!.Headers.TryGetValues("Cookie", out var values)
|
|
|
|
|
? string.Join("; ", values)
|
|
|
|
|
: "";
|
|
|
|
|
cookies.ShouldContain($"{AuthState.AuthCookieName}=CfDJ8-ticket");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SendAsync_CircuitSansHttpContext_SansAuthCookie_PasDeCookie()
|
|
|
|
|
{
|
|
|
|
|
var authState = new AuthState(); // AuthCookie null
|
|
|
|
|
var accessor = Substitute.For<IHttpContextAccessor>();
|
|
|
|
|
accessor.HttpContext.Returns((HttpContext?)null);
|
|
|
|
|
var capture = new CapturingHandler();
|
|
|
|
|
var handler = CreateHandler(authState, accessor, capture);
|
|
|
|
|
using var client = new HttpClient(handler);
|
|
|
|
|
|
|
|
|
|
await client.GetAsync("http://heimdall.local/api/agents");
|
|
|
|
|
|
|
|
|
|
capture.LastRequest!.Headers.Contains("Cookie").ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SendAsync_AvecBearer_NePropagePasLeCookie()
|
|
|
|
|
{
|
|
|
|
|
var authState = new AuthState { AuthCookie = "CfDJ8-ticket" };
|
|
|
|
|
var accessor = Substitute.For<IHttpContextAccessor>();
|
|
|
|
|
accessor.HttpContext.Returns((HttpContext?)null);
|
|
|
|
|
var capture = new CapturingHandler();
|
|
|
|
|
var handler = CreateHandler(authState, accessor, capture);
|
|
|
|
|
using var client = new HttpClient(handler);
|
|
|
|
|
|
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "http://heimdall.local/api/agents");
|
|
|
|
|
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "jwt-token");
|
|
|
|
|
await client.SendAsync(request);
|
|
|
|
|
|
|
|
|
|
// Le Bearer JWT (sessionStorage) prime sur la propagation du cookie
|
|
|
|
|
capture.LastRequest!.Headers.Contains("Cookie").ShouldBeFalse();
|
|
|
|
|
capture.LastRequest!.Headers.Authorization!.Scheme.ShouldBe("Bearer");
|
|
|
|
|
}
|
|
|
|
|
}
|