|
7 | 7 | using FluentAssertions.Extensions; |
8 | 8 | using Microsoft.AspNetCore.Builder; |
9 | 9 | using Microsoft.AspNetCore.TestHost; |
| 10 | +using Microsoft.EntityFrameworkCore; |
10 | 11 | using Microsoft.Extensions.Configuration; |
11 | 12 | using Microsoft.Extensions.DependencyInjection; |
12 | 13 | using Microsoft.Extensions.Diagnostics.HealthChecks; |
@@ -516,6 +517,58 @@ public async Task Converts_AspNet_health_check_results() |
516 | 517 | """); |
517 | 518 | } |
518 | 519 |
|
| 520 | + [Fact] |
| 521 | + public async Task Can_use_scoped_AspNet_health_check() |
| 522 | + { |
| 523 | + WebApplicationBuilder builder = TestWebApplicationBuilderFactory.CreateDefault(false); |
| 524 | + builder.Configuration.AddInMemoryCollection(AppSettings); |
| 525 | + builder.Services.AddDbContext<TestDbContext>(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString())); |
| 526 | + builder.Services.AddHealthChecks().AddDbContextCheck<TestDbContext>(); |
| 527 | + builder.Services.AddHealthActuator(); |
| 528 | + await using WebApplication host = builder.Build(); |
| 529 | + |
| 530 | + // ReSharper disable once AccessToDisposedClosure |
| 531 | + Action action = () => host.Services.GetRequiredService<TestDbContext>(); |
| 532 | + action.Should().ThrowExactly<InvalidOperationException>(); |
| 533 | + |
| 534 | + await using (AsyncServiceScope scope = host.Services.CreateAsyncScope()) |
| 535 | + { |
| 536 | + await using var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>(); |
| 537 | + await dbContext.Database.EnsureDeletedAsync(TestContext.Current.CancellationToken); |
| 538 | + await dbContext.Database.EnsureCreatedAsync(TestContext.Current.CancellationToken); |
| 539 | + } |
| 540 | + |
| 541 | + host.MapHealthChecks("/health"); |
| 542 | + await host.StartAsync(TestContext.Current.CancellationToken); |
| 543 | + using var httpClient = new HttpClient(); |
| 544 | + |
| 545 | + HttpResponseMessage actuatorResponse = |
| 546 | + await httpClient.GetAsync(new Uri("http://localhost:5000/actuator/health"), TestContext.Current.CancellationToken); |
| 547 | + |
| 548 | + actuatorResponse.StatusCode.Should().Be(HttpStatusCode.OK); |
| 549 | + |
| 550 | + string actuatorResponseBody = await actuatorResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); |
| 551 | + |
| 552 | + actuatorResponseBody.Should().BeJson(""" |
| 553 | + { |
| 554 | + "status": "UP", |
| 555 | + "components": { |
| 556 | + "TestDbContext": { |
| 557 | + "status": "UP" |
| 558 | + } |
| 559 | + } |
| 560 | + } |
| 561 | + """); |
| 562 | + |
| 563 | + HttpResponseMessage aspNetResponse = await httpClient.GetAsync(new Uri("http://localhost:5000/health"), TestContext.Current.CancellationToken); |
| 564 | + |
| 565 | + aspNetResponse.StatusCode.Should().Be(HttpStatusCode.OK); |
| 566 | + |
| 567 | + string aspnetResponseBody = await aspNetResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); |
| 568 | + |
| 569 | + aspnetResponseBody.Should().Be("Healthy"); |
| 570 | + } |
| 571 | + |
519 | 572 | private sealed class AspNetHealthyCheck : IHealthCheck |
520 | 573 | { |
521 | 574 | public async Task<MicrosoftHealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) |
@@ -562,4 +615,7 @@ public Task<MicrosoftHealthCheckResult> CheckHealthAsync(HealthCheckContext cont |
562 | 615 | throw new InvalidOperationException("test-exception"); |
563 | 616 | } |
564 | 617 | } |
| 618 | + |
| 619 | + private sealed class TestDbContext(DbContextOptions options) |
| 620 | + : DbContext(options); |
565 | 621 | } |
0 commit comments