Fix router frontdoor readiness and route contracts
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StellaOps.Gateway.WebService.Configuration;
|
||||
using StellaOps.Gateway.WebService.Routing;
|
||||
using StellaOps.Router.Common.Abstractions;
|
||||
using StellaOps.Router.Common.Enums;
|
||||
@@ -51,6 +52,93 @@ public sealed class GatewayIntegrationTests : IClassFixture<GatewayWebApplicatio
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthReady_ReturnsServiceUnavailable_WhenRequiredMicroserviceIsMissing()
|
||||
{
|
||||
using var factory = CreateFactoryWithRequiredServices("policy");
|
||||
var client = factory.CreateClient();
|
||||
|
||||
var response = await client.GetAsync("/health/ready");
|
||||
|
||||
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
|
||||
|
||||
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
Assert.Equal("policy", payload.RootElement.GetProperty("missingMicroservices")[0].GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthReady_ReturnsOk_WhenRequiredMicroserviceIsRegistered()
|
||||
{
|
||||
using var factory = CreateFactoryWithRequiredServices("policy");
|
||||
using (var scope = factory.Services.CreateScope())
|
||||
{
|
||||
var routingState = scope.ServiceProvider.GetRequiredService<IGlobalRoutingState>();
|
||||
routingState.AddConnection(new ConnectionState
|
||||
{
|
||||
ConnectionId = "conn-policy",
|
||||
Instance = new InstanceDescriptor
|
||||
{
|
||||
InstanceId = "policy-01",
|
||||
ServiceName = "policy-gateway",
|
||||
Version = "1.0.0",
|
||||
Region = "test"
|
||||
},
|
||||
Status = InstanceHealthStatus.Healthy,
|
||||
TransportType = TransportType.Messaging
|
||||
});
|
||||
}
|
||||
|
||||
var client = factory.CreateClient();
|
||||
var response = await client.GetAsync("/health/ready");
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthReady_ReturnsServiceUnavailable_WhenOnlySiblingServiceIsRegistered()
|
||||
{
|
||||
using var factory = CreateFactoryWithRequiredServices("policy");
|
||||
using (var scope = factory.Services.CreateScope())
|
||||
{
|
||||
var routingState = scope.ServiceProvider.GetRequiredService<IGlobalRoutingState>();
|
||||
routingState.AddConnection(new ConnectionState
|
||||
{
|
||||
ConnectionId = "conn-policy-engine",
|
||||
Instance = new InstanceDescriptor
|
||||
{
|
||||
InstanceId = "policy-engine-01",
|
||||
ServiceName = "policy-engine",
|
||||
Version = "1.0.0",
|
||||
Region = "test"
|
||||
},
|
||||
Status = InstanceHealthStatus.Healthy,
|
||||
TransportType = TransportType.Messaging
|
||||
});
|
||||
}
|
||||
|
||||
var client = factory.CreateClient();
|
||||
var response = await client.GetAsync("/health/ready");
|
||||
|
||||
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
|
||||
|
||||
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
Assert.Equal("policy", payload.RootElement.GetProperty("missingMicroservices")[0].GetString());
|
||||
}
|
||||
|
||||
private WebApplicationFactory<Program> CreateFactoryWithRequiredServices(params string[] requiredServices)
|
||||
{
|
||||
return new GatewayWebApplicationFactory().WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.PostConfigure<GatewayOptions>(options =>
|
||||
{
|
||||
options.Health.RequiredMicroservices = requiredServices.ToList();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OpenApiJson_ReturnsValidOpenApiDocument()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user