68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using Omika.Core.Observability.Outbox;
|
|
|
|
namespace Omika.Tests.Observability.Outbox;
|
|
|
|
/// <summary>
|
|
/// Tests du DiagnosticUploadClient par fournisseur d'URL : l'URL est résolue à
|
|
/// chaque envoi depuis la même source de vérité que le client métier.
|
|
/// </summary>
|
|
public class DiagnosticUploadProviderTests
|
|
{
|
|
private sealed class UriCapturingHandler : HttpMessageHandler
|
|
{
|
|
public string? LastUri { get; private set; }
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
LastUri = request.RequestUri?.ToString();
|
|
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("""{"acceptedCount":1}""", Encoding.UTF8, "application/json")
|
|
});
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Upload_UtiliseUrlDuFournisseur()
|
|
{
|
|
var handler = new UriCapturingHandler();
|
|
string? currentBaseUrl = "https://api.example.com";
|
|
|
|
var client = new DiagnosticUploadClient(
|
|
new System.Net.Http.HttpClient(handler),
|
|
() => currentBaseUrl,
|
|
() => "token-1",
|
|
new DiagnosticUploadOptions());
|
|
|
|
DiagnosticUploadResult result = await client.UploadAsync(new DiagnosticBatchDto
|
|
{
|
|
Events = [new DiagnosticEventDto { EventId = Guid.NewGuid(), EventCode = "OMK-MOB-APP-001" }]
|
|
});
|
|
|
|
Assert.Equal(DiagnosticUploadOutcome.Success, result.Outcome);
|
|
Assert.Equal("https://api.example.com/api/diagnostics", handler.LastUri);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Upload_SansUrl_AucunAppelHttp()
|
|
{
|
|
var handler = new UriCapturingHandler();
|
|
|
|
var client = new DiagnosticUploadClient(
|
|
new System.Net.Http.HttpClient(handler),
|
|
() => null,
|
|
() => "token-1",
|
|
new DiagnosticUploadOptions());
|
|
|
|
DiagnosticUploadResult result = await client.UploadAsync(new DiagnosticBatchDto
|
|
{
|
|
Events = [new DiagnosticEventDto { EventId = Guid.NewGuid(), EventCode = "OMK-MOB-APP-001" }]
|
|
});
|
|
|
|
Assert.Equal(DiagnosticUploadOutcome.Failure, result.Outcome);
|
|
Assert.Null(handler.LastUri); // aucun fallback localhost, aucun envoi.
|
|
}
|
|
}
|