FluentMockServerSettings (#21)

This commit is contained in:
Stef Heyenrath
2017-03-20 17:22:09 +01:00
parent 7793330d1d
commit bd8e18b2c4
8 changed files with 305 additions and 44 deletions

View File

@@ -16,6 +16,12 @@ namespace WireMock.Net.StandAlone
[SwitchArgument('p', "AllowPartialMapping", true, Description = "Allow Partial Mapping (default set to true).", Optional = true)]
public bool AllowPartialMapping { get; set; }
[SwitchArgument('s', "StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)]
public bool StartAdminInterface { get; set; }
[SwitchArgument('r', "ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)]
public bool ReadStaticMappings { get; set; }
}
static void Main(params string[] args)
@@ -31,7 +37,12 @@ namespace WireMock.Net.StandAlone
if (!options.Urls.Any())
options.Urls.Add("http://localhost:9090/");
var server = FluentMockServer.StartWithAdminInterface(options.Urls.ToArray());
var server = FluentMockServer.Start(new FluentMockServerSettings
{
Urls = options.Urls.ToArray(),
StartAdminInterface = options.StartAdminInterface,
ReadStaticMappings = options.ReadStaticMappings
});
if (options.AllowPartialMapping)
server.AllowPartialMapping();

View File

@@ -66,11 +66,16 @@ namespace WireMock.Server
Check.NotNull(filename, nameof(filename));
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
Guid guid;
if (!Guid.TryParse(filenameWithoutExtension, out guid))
guid = Guid.NewGuid();
Guid guidFromFilename;
DeserializeAndAddMapping(File.ReadAllText(filename), guid);
if (Guid.TryParse(filenameWithoutExtension, out guidFromFilename))
{
DeserializeAndAddMapping(File.ReadAllText(filename), guidFromFilename);
}
else
{
DeserializeAndAddMapping(File.ReadAllText(filename));
}
}
private void InitAdmin()

View File

@@ -19,7 +19,7 @@ namespace WireMock.Server
/// <summary>
/// The fluent mock server.
/// </summary>
public partial class FluentMockServer
public partial class FluentMockServer : IDisposable
{
private readonly TinyHttpServer _httpServer;
@@ -112,6 +112,19 @@ namespace WireMock.Server
}
}
/// <summary>
/// Starts the specified settings.
/// </summary>
/// <param name="settings">The FluentMockServerSettings.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer Start(FluentMockServerSettings settings)
{
Check.NotNull(settings, nameof(settings));
return new FluentMockServer(settings);
}
/// <summary>
/// Start this FluentMockServer.
/// </summary>
@@ -119,14 +132,13 @@ namespace WireMock.Server
/// <param name="ssl">The SSL support.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer Start(int port = 0, bool ssl = false)
public static FluentMockServer Start([CanBeNull] int? port = 0, bool ssl = false)
{
Check.Condition(port, p => p >= 0, nameof(port));
if (port == 0)
port = PortUtil.FindFreeTcpPort();
return new FluentMockServer(false, port, ssl);
return new FluentMockServer(new FluentMockServerSettings
{
Port = port,
UseSSL = ssl
});
}
/// <summary>
@@ -139,7 +151,10 @@ namespace WireMock.Server
{
Check.NotEmpty(urls, nameof(urls));
return new FluentMockServer(false, urls);
return new FluentMockServer(new FluentMockServerSettings
{
Urls = urls
});
}
/// <summary>
@@ -149,14 +164,14 @@ namespace WireMock.Server
/// <param name="ssl">The SSL support.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer StartWithAdminInterface(int port = 0, bool ssl = false)
public static FluentMockServer StartWithAdminInterface(int? port = 0, bool ssl = false)
{
Check.Condition(port, p => p >= 0, nameof(port));
if (port == 0)
port = PortUtil.FindFreeTcpPort();
return new FluentMockServer(true, port, ssl);
return new FluentMockServer(new FluentMockServerSettings
{
Port = port,
UseSSL = ssl,
StartAdminInterface = true
});
}
/// <summary>
@@ -169,7 +184,57 @@ namespace WireMock.Server
{
Check.NotEmpty(urls, nameof(urls));
return new FluentMockServer(true, urls);
return new FluentMockServer(new FluentMockServerSettings
{
Urls = urls,
StartAdminInterface = true
});
}
/// <summary>
/// Start this FluentMockServer with the admin interface and read static mappings.
/// </summary>
/// <param name="urls">The urls.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
{
Check.NotEmpty(urls, nameof(urls));
return new FluentMockServer(new FluentMockServerSettings
{
Urls = urls,
StartAdminInterface = true,
ReadStaticMappings = true
});
}
private FluentMockServer(FluentMockServerSettings settings)
{
if (settings.Urls != null)
{
Urls = settings.Urls;
}
else
{
int port = settings.Port > 0 ? settings.Port.Value : PortUtil.FindFreeTcpPort();
Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
}
_httpServer = new TinyHttpServer(HandleRequestAsync, Urls);
Ports = _httpServer.Ports;
_httpServer.Start();
if (settings.StartAdminInterface == true)
{
InitAdmin();
}
if (settings.ReadStaticMappings == true)
{
ReadStaticMappings();
}
}
/// <summary>
@@ -184,27 +249,6 @@ namespace WireMock.Server
.RespondWith(new DynamicResponseProvider(request => new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }));
}
private FluentMockServer(bool startAdminInterface, int port, bool ssl) : this(startAdminInterface, (ssl ? "https" : "http") + "://localhost:" + port + "/")
{
}
private FluentMockServer(bool startAdminInterface, params string[] urls)
{
Urls = urls;
_httpServer = new TinyHttpServer(HandleRequestAsync, urls);
Ports = _httpServer.Ports;
_httpServer.Start();
if (startAdminInterface)
{
InitAdmin();
}
ReadStaticMappings();
}
/// <summary>
/// Stop this server.
/// </summary>
@@ -214,6 +258,17 @@ namespace WireMock.Server
_httpServer.Stop();
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (_httpServer != null && _httpServer.IsStarted)
{
_httpServer.Stop();
}
}
/// <summary>
/// Resets LogEntries and Mappings.
/// </summary>

View File

@@ -0,0 +1,48 @@
namespace WireMock.Server
{
/// <summary>
/// FluentMockServerSettings
/// </summary>
public class FluentMockServerSettings
{
/// <summary>
/// Gets or sets the port.
/// </summary>
/// <value>
/// The port.
/// </value>
public int? Port { get; set; }
/// <summary>
/// Gets or sets the use SSL.
/// </summary>
/// <value>
/// The use SSL.
/// </value>
public bool? UseSSL { get; set; }
/// <summary>
/// Gets or sets the start admin interface.
/// </summary>
/// <value>
/// The start admin interface.
/// </value>
public bool? StartAdminInterface { get; set; }
/// <summary>
/// Gets or sets the read static mappings.
/// </summary>
/// <value>
/// The read static mappings.
/// </value>
public bool? ReadStaticMappings { get; set; }
/// <summary>
/// Gets or sets the urls.
/// </summary>
/// <value>
/// The urls.
/// </value>
public string[] Urls { get; set; }
}
}