__admin/settings

This commit is contained in:
Stef Heyenrath
2017-02-13 19:34:04 +01:00
parent 2944b5392a
commit b25444d083
4 changed files with 55 additions and 4 deletions

View File

@@ -0,0 +1,18 @@
namespace WireMock.Admin.Settings
{
/// <summary>
/// Settings
/// </summary>
public class SettingsModel
{
/// <summary>
/// Gets or sets the global delay in milliseconds.
/// </summary>
public int? GlobalProcessingDelay { get; set; }
/// <summary>
/// Gets or sets if partial mapping is allowed.
/// </summary>
public bool? AllowPartialMapping { get; set; }
}
}

View File

@@ -7,6 +7,7 @@ using Newtonsoft.Json;
using SimMetrics.Net; using SimMetrics.Net;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Admin.Requests; using WireMock.Admin.Requests;
using WireMock.Admin.Settings;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
@@ -25,6 +26,7 @@ namespace WireMock.Server
private const string AdminMappingsFolder = @"\__admin\mappings\"; private const string AdminMappingsFolder = @"\__admin\mappings\";
private const string AdminMappings = "/__admin/mappings"; private const string AdminMappings = "/__admin/mappings";
private const string AdminRequests = "/__admin/requests"; private const string AdminRequests = "/__admin/requests";
private const string AdminSettings = "/__admin/settings";
private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/requests\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/requests\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
@@ -48,6 +50,11 @@ namespace WireMock.Server
private void InitAdmin() private void InitAdmin()
{ {
// __admin/settings
Given(Request.Create().WithPath(AdminSettings).UsingGet()).RespondWith(new DynamicResponseProvider(SettingsGet));
Given(Request.Create().WithPath(AdminSettings).UsingVerb("PUT", "POST")).RespondWith(new DynamicResponseProvider(SettingsUpdate));
// __admin/mappings // __admin/mappings
Given(Request.Create().WithPath(AdminMappings).UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet)); Given(Request.Create().WithPath(AdminMappings).UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet));
Given(Request.Create().WithPath(AdminMappings).UsingPost()).RespondWith(new DynamicResponseProvider(MappingsPost)); Given(Request.Create().WithPath(AdminMappings).UsingPost()).RespondWith(new DynamicResponseProvider(MappingsPost));
@@ -77,6 +84,32 @@ namespace WireMock.Server
Given(Request.Create().WithPath(_adminRequestsGuidPathMatcher).UsingDelete()).RespondWith(new DynamicResponseProvider(RequestDelete)); Given(Request.Create().WithPath(_adminRequestsGuidPathMatcher).UsingDelete()).RespondWith(new DynamicResponseProvider(RequestDelete));
} }
#region Settings
private ResponseMessage SettingsGet(RequestMessage requestMessage)
{
var model = new SettingsModel
{
AllowPartialMapping = _allowPartialMapping,
GlobalProcessingDelay = _requestProcessingDelay?.Milliseconds
};
return ToJson(model);
}
private ResponseMessage SettingsUpdate(RequestMessage requestMessage)
{
var settings = JsonConvert.DeserializeObject<SettingsModel>(requestMessage.Body);
if (settings.AllowPartialMapping != null)
_allowPartialMapping = settings.AllowPartialMapping.Value;
if (settings.GlobalProcessingDelay != null)
_requestProcessingDelay = TimeSpan.FromMilliseconds(settings.GlobalProcessingDelay.Value);
return new ResponseMessage { Body = "Settings updated" };
}
#endregion Settings
#region Mapping/{guid} #region Mapping/{guid}
private ResponseMessage MappingGet(RequestMessage requestMessage) private ResponseMessage MappingGet(RequestMessage requestMessage)
{ {

View File

@@ -32,7 +32,7 @@ namespace WireMock.Server
private readonly object _syncRoot = new object(); private readonly object _syncRoot = new object();
private TimeSpan _requestProcessingDelay = TimeSpan.Zero; private TimeSpan? _requestProcessingDelay;
private bool _allowPartialMapping; private bool _allowPartialMapping;
@@ -272,7 +272,7 @@ namespace WireMock.Server
/// The delay. /// The delay.
/// </param> /// </param>
[PublicAPI] [PublicAPI]
public void AddRequestProcessingDelay(TimeSpan delay) public void AddGlobalProcessingDelay(TimeSpan delay)
{ {
lock (_syncRoot) lock (_syncRoot)
{ {
@@ -357,7 +357,7 @@ namespace WireMock.Server
{ {
lock (_syncRoot) lock (_syncRoot)
{ {
Task.Delay(_requestProcessingDelay).Wait(); Task.Delay(_requestProcessingDelay.Value).Wait();
} }
} }

View File

@@ -267,7 +267,7 @@ namespace WireMock.Net.Tests
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
_server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(200)); _server.AddGlobalProcessingDelay(TimeSpan.FromMilliseconds(200));
_server _server
.Given(Request.Create().WithPath("/*")) .Given(Request.Create().WithPath("/*"))
.RespondWith(Response.Create().WithBody(@"{ msg: ""Hello world!""}")); .RespondWith(Response.Create().WithBody(@"{ msg: ""Hello world!""}"));