mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 15:23:47 +01:00
* WireMock.Net.Aspire * . * xxx * nuget * [CodeFactor] Apply fixes * ut * t * **WireMock.Net.Aspire** * . * t * . * . * . * TESTS * docker utils * Install .NET Aspire workload * 4 * 4! * projects: '**/test/**/*.csproj' * script: 'dotnet workload install aspire' * projects: '**/test/**/*.csproj' * coverage * WithWatchStaticMappings * Admin * typo * port * fix * . * x * ... * wait * readme * x * 2 * async * <Version>0.0.1-preview-03</Version> * ... * fix aspire * admin/pwd * Install .NET Aspire workload * 0.0.1-preview-04 * WaitForHealthAsync * ... * IsHealthyAsync * . * add eps * name: 'Execute Aspire Tests' * name: Install .NET Aspire workload * . * dotnet test * remove duplicate * . * cc * dotnet tool install --global coverlet.console * -* * merge * /d:sonar.pullrequest.provider=github * <Version>0.0.1-preview-05</Version> * // Copyright © WireMock.Net * . --------- Co-authored-by: codefactor-io <support@codefactor.io>
52 lines
2.0 KiB
C#
52 lines
2.0 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using Aspire.Hosting.ApplicationModel;
|
|
using Aspire.Hosting.Lifecycle;
|
|
using Microsoft.Extensions.Logging;
|
|
using RestEase;
|
|
using WireMock.Client;
|
|
using WireMock.Client.Extensions;
|
|
|
|
namespace WireMock.Net.Aspire;
|
|
|
|
internal class WireMockServerLifecycleHook(ResourceLoggerService loggerService) : IDistributedApplicationLifecycleHook
|
|
{
|
|
public async Task AfterResourcesCreatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
|
|
{
|
|
var wireMockServerResources = appModel.Resources
|
|
.OfType<WireMockServerResource>()
|
|
.Where(resource => resource.Arguments.ApiMappingBuilder is not null)
|
|
.ToArray();
|
|
|
|
if (wireMockServerResources.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var wireMockServerResource in wireMockServerResources)
|
|
{
|
|
var endpoint = wireMockServerResource.GetEndpoint();
|
|
if (endpoint.IsAllocated)
|
|
{
|
|
var adminApi = CreateWireMockAdminApi(wireMockServerResource);
|
|
|
|
var logger = loggerService.GetLogger(wireMockServerResource);
|
|
logger.LogInformation("Checking Health status from WireMock.Net");
|
|
|
|
await adminApi.WaitForHealthAsync(cancellationToken: cancellationToken);
|
|
|
|
logger.LogInformation("Calling ApiMappingBuilder to add mappings to WireMock.Net");
|
|
var mappingBuilder = adminApi.GetMappingBuilder();
|
|
await wireMockServerResource.Arguments.ApiMappingBuilder!.Invoke(mappingBuilder);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static IWireMockAdminApi CreateWireMockAdminApi(WireMockServerResource resource)
|
|
{
|
|
var adminApi = RestClient.For<IWireMockAdminApi>(resource.GetEndpoint().Url);
|
|
return resource.Arguments.HasBasicAuthentication ?
|
|
adminApi.WithAuthorization(resource.Arguments.AdminUsername!, resource.Arguments.AdminPassword!) :
|
|
adminApi;
|
|
}
|
|
} |