Add Aspire Extension (#1109)

* 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>
This commit is contained in:
Stef Heyenrath
2024-07-27 18:53:59 +02:00
committed by GitHub
parent 69c829fae0
commit 4b12f3419f
70 changed files with 2849 additions and 31 deletions

View File

@@ -0,0 +1,112 @@
// Copyright © WireMock.Net
using System.Diagnostics.CodeAnalysis;
using WireMock.Client.Builders;
// ReSharper disable once CheckNamespace
namespace Aspire.Hosting;
/// <summary>
/// Represents the arguments required to configure and start a WireMock.Net Server.
/// </summary>
public class WireMockServerArguments
{
internal const int HttpContainerPort = 80;
/// <summary>
/// The default HTTP port where WireMock.Net is listening.
/// </summary>
public const int DefaultPort = 9091;
private const string DefaultLogger = "WireMockConsoleLogger";
/// <summary>
/// The HTTP port where WireMock.Net is listening.
/// If not defined, .NET Aspire automatically assigns a random port.
/// </summary>
public int? HttpPort { get; set; }
/// <summary>
/// The admin username.
/// </summary>
[MemberNotNullWhen(true, nameof(HasBasicAuthentication))]
public string? AdminUsername { get; set; }
/// <summary>
/// The admin password.
/// </summary>
[MemberNotNullWhen(true, nameof(HasBasicAuthentication))]
public string? AdminPassword { get; set; }
/// <summary>
/// Defines if the static mappings should be read at startup.
///
/// Default value is <c>false</c>.
/// </summary>
public bool ReadStaticMappings { get; set; }
/// <summary>
/// Watch the static mapping files + folder for changes when running.
///
/// Default value is <c>false</c>.
/// </summary>
public bool WithWatchStaticMappings { get; set; }
/// <summary>
/// Specifies the path for the (static) mapping json files.
/// </summary>
public string? MappingsPath { get; set; }
/// <summary>
/// Indicates whether the admin interface has Basic Authentication.
/// </summary>
public bool HasBasicAuthentication => !string.IsNullOrEmpty(AdminUsername) && !string.IsNullOrEmpty(AdminPassword);
/// <summary>
/// Optional delegate that will be invoked to configure the WireMock.Net resource using the <see cref="AdminApiMappingBuilder"/>.
/// </summary>
public Func<AdminApiMappingBuilder, Task>? ApiMappingBuilder { get; set; }
/// <summary>
/// Converts the current instance's properties to an array of command-line arguments for starting the WireMock.Net server.
/// </summary>
/// <returns>An array of strings representing the command-line arguments.</returns>
public string[] GetArgs()
{
var args = new Dictionary<string, string>();
Add(args, "--WireMockLogger", DefaultLogger);
if (HasBasicAuthentication)
{
Add(args, "--AdminUserName", AdminUsername!);
Add(args, "--AdminPassword", AdminPassword!);
}
if (ReadStaticMappings)
{
Add(args, "--ReadStaticMappings", "true");
}
if (WithWatchStaticMappings)
{
Add(args, "--ReadStaticMappings", "true");
Add(args, "--WatchStaticMappings", "true");
Add(args, "--WatchStaticMappingsInSubdirectories", "true");
}
return args
.SelectMany(k => new[] { k.Key, k.Value })
.ToArray();
}
private static void Add(IDictionary<string, string> args, string argument, string value)
{
args[argument] = value;
}
private static void Add(IDictionary<string, string> args, string argument, Func<string> action)
{
args[argument] = action();
}
}