scenario and state (added admin interface for GET and DELETE)

This commit is contained in:
Stef Heyenrath
2017-10-07 18:10:17 +02:00
parent 782418f107
commit 8199cdc382
6 changed files with 110 additions and 124 deletions

View File

@@ -31,6 +31,7 @@ namespace WireMock.Server
private const string AdminMappings = "/__admin/mappings";
private const string AdminRequests = "/__admin/requests";
private const string AdminSettings = "/__admin/settings";
private const string AdminScenarios = "/__admin/scenarios";
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})$");
@@ -117,6 +118,15 @@ namespace WireMock.Server
// __admin/requests/find
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).RespondWith(new DynamicResponseProvider(RequestsFind));
// __admin/scenarios
// __admin/scenarios
Given(Request.Create().WithPath(AdminScenarios).UsingGet()).RespondWith(new DynamicResponseProvider(ScenariosGet));
Given(Request.Create().WithPath(AdminScenarios).UsingDelete()).RespondWith(new DynamicResponseProvider(ScenariosReset));
// __admin/scenarios/reset
Given(Request.Create().WithPath(AdminScenarios + "/reset").UsingPost()).RespondWith(new DynamicResponseProvider(ScenariosReset));
}
#region Proxy and Record
@@ -459,6 +469,26 @@ namespace WireMock.Server
}
#endregion Requests/find
#region Scenarios
private ResponseMessage ScenariosGet(RequestMessage requestMessage)
{
var scenarios = Scenarios.Select(s => new
{
Name = s.Key,
Started = s.Value != null,
NextState = s.Value
});
return ToJson(scenarios);
}
private ResponseMessage ScenariosReset(RequestMessage requestMessage)
{
ResetScenarios();
return new ResponseMessage { Body = "Scenarios reset" };
}
#endregion
private IRequestBuilder InitRequestBuilder(RequestModel requestModel)
{
IRequestBuilder requestBuilder = Request.Create();

View File

@@ -19,36 +19,15 @@ namespace WireMock.Server
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged += value;
}
}
remove
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged -= value;
}
}
add => _options.LogEntries.CollectionChanged += value;
remove => _options.LogEntries.CollectionChanged -= value;
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
}
}
}
public IEnumerable<LogEntry> LogEntries => new ReadOnlyCollection<LogEntry>(_options.LogEntries);
/// <summary>
/// The search log-entries based on matchers.
@@ -58,26 +37,23 @@ namespace WireMock.Server
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries)
{
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries)
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult);
}
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult);
}
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
}
/// <summary>
@@ -86,10 +62,7 @@ namespace WireMock.Server
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.Clear();
}
_options.LogEntries.Clear();
}
/// <summary>
@@ -99,18 +72,15 @@ namespace WireMock.Server
[PublicAPI]
public bool DeleteLogEntry(Guid guid)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
@@ -44,16 +45,13 @@ namespace WireMock.Server
/// Gets the mappings.
/// </summary>
[PublicAPI]
public IEnumerable<Mapping> Mappings
{
get
{
lock (((ICollection)_options.Mappings).SyncRoot)
{
return new ReadOnlyCollection<Mapping>(_options.Mappings);
}
}
}
public IEnumerable<Mapping> Mappings => new ReadOnlyCollection<Mapping>(_options.Mappings);
/// <summary>
/// Gets the scenarios.
/// </summary>
[PublicAPI]
public IDictionary<string, object> Scenarios => new ConcurrentDictionary<string, object>(_options.Scenarios);
#region Start/Stop
/// <summary>
@@ -258,10 +256,7 @@ namespace WireMock.Server
[PublicAPI]
public void ResetMappings()
{
lock (((ICollection)_options.Mappings).SyncRoot)
{
_options.Mappings = _options.Mappings.Where(m => m.IsAdminInterface).ToList();
}
_options.Mappings = _options.Mappings.Where(m => m.IsAdminInterface).ToList();
}
/// <summary>
@@ -271,18 +266,15 @@ namespace WireMock.Server
[PublicAPI]
public bool DeleteMapping(Guid guid)
{
lock (((ICollection)_options.Mappings).SyncRoot)
// Check a mapping exists with the same GUID, if so, remove it.
var existingMapping = _options.Mappings.FirstOrDefault(m => m.Guid == guid);
if (existingMapping != null)
{
// Check a mapping exists with the same GUID, if so, remove it.
var existingMapping = _options.Mappings.FirstOrDefault(m => m.Guid == guid);
if (existingMapping != null)
{
_options.Mappings.Remove(existingMapping);
return true;
}
return false;
_options.Mappings.Remove(existingMapping);
return true;
}
return false;
}
/// <summary>
@@ -349,6 +341,15 @@ namespace WireMock.Server
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
/// <summary>
/// Resets the Scenarios.
/// </summary>
[PublicAPI]
public void ResetScenarios()
{
_options.Scenarios.Clear();
}
/// <summary>
/// The given.
/// </summary>
@@ -368,13 +369,10 @@ namespace WireMock.Server
/// </param>
private void RegisterMapping(Mapping mapping)
{
lock (((ICollection)_options.Mappings).SyncRoot)
{
// Check a mapping exists with the same GUID, if so, remove it first.
DeleteMapping(mapping.Guid);
// Check a mapping exists with the same GUID, if so, remove it first.
DeleteMapping(mapping.Guid);
_options.Mappings.Add(mapping);
}
_options.Mappings.Add(mapping);
}
}
}