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