|
3 | 3 |
|
4 | 4 | using Aspire.Hosting.ApplicationModel;
|
5 | 5 | using Aspire.Hosting.Utils;
|
| 6 | +using Microsoft.Extensions.DependencyInjection; |
6 | 7 | using Xunit;
|
7 | 8 |
|
8 | 9 | namespace Aspire.Hosting.GitHub.Models.Tests;
|
@@ -288,4 +289,41 @@ public void WithHealthCheckAddsHealthCheckAnnotation()
|
288 | 289 | Assert.Single(healthCheckAnnotations);
|
289 | 290 | Assert.Equal("github_check", healthCheckAnnotations[0].Key);
|
290 | 291 | }
|
| 292 | + |
| 293 | + [Fact] |
| 294 | + public void WithHealthCheckEnsuresIHttpClientFactoryIsRegistered() |
| 295 | + { |
| 296 | + using var builder = TestDistributedApplicationBuilder.Create(); |
| 297 | + builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key"; |
| 298 | + |
| 299 | + // Add the health check without explicitly calling AddHttpClient |
| 300 | + var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini").WithHealthCheck(); |
| 301 | + |
| 302 | + // Build the service provider to test dependency resolution |
| 303 | + var services = builder.Services.BuildServiceProvider(); |
| 304 | + |
| 305 | + // This should not throw because WithHealthCheck should ensure IHttpClientFactory is registered |
| 306 | + var httpClientFactory = services.GetService<IHttpClientFactory>(); |
| 307 | + Assert.NotNull(httpClientFactory); |
| 308 | + } |
| 309 | + |
| 310 | + [Fact] |
| 311 | + public void WithHealthCheckWorksWhenAddHttpClientIsCalledManually() |
| 312 | + { |
| 313 | + using var builder = TestDistributedApplicationBuilder.Create(); |
| 314 | + builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key"; |
| 315 | + |
| 316 | + // Manually call AddHttpClient (should not conflict with automatic registration in WithHealthCheck) |
| 317 | + builder.Services.AddHttpClient(); |
| 318 | + |
| 319 | + // Add the health check |
| 320 | + var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini").WithHealthCheck(); |
| 321 | + |
| 322 | + // Build the service provider to test dependency resolution |
| 323 | + var services = builder.Services.BuildServiceProvider(); |
| 324 | + |
| 325 | + // This should work fine since AddHttpClient can be called multiple times safely |
| 326 | + var httpClientFactory = services.GetService<IHttpClientFactory>(); |
| 327 | + Assert.NotNull(httpClientFactory); |
| 328 | + } |
291 | 329 | }
|
0 commit comments