mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-30 22:32:56 +02:00
scenario and state (added admin interface for GET and DELETE)
This commit is contained in:
@@ -1,8 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -28,8 +24,6 @@ namespace WireMock.Owin
|
|||||||
private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
|
private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
|
||||||
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
||||||
|
|
||||||
private readonly IDictionary<string, object> _states = new ConcurrentDictionary<string, object>();
|
|
||||||
|
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
|
public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
|
||||||
{
|
{
|
||||||
@@ -59,9 +53,9 @@ namespace WireMock.Owin
|
|||||||
foreach (var mapping in _options.Mappings.Where(m => m.Scenario != null))
|
foreach (var mapping in _options.Mappings.Where(m => m.Scenario != null))
|
||||||
{
|
{
|
||||||
// Set start
|
// Set start
|
||||||
if (!_states.ContainsKey(mapping.Scenario) && mapping.IsStartState)
|
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
|
||||||
{
|
{
|
||||||
_states.Add(mapping.Scenario, null);
|
_options.Scenarios.Add(mapping.Scenario, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +63,7 @@ namespace WireMock.Owin
|
|||||||
.Select(m => new
|
.Select(m => new
|
||||||
{
|
{
|
||||||
Mapping = m,
|
Mapping = m,
|
||||||
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _states.ContainsKey(m.Scenario) ? _states[m.Scenario] : null)
|
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey(m.Scenario) ? _options.Scenarios[m.Scenario] : null)
|
||||||
})
|
})
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
@@ -107,7 +101,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
|
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
|
||||||
{
|
{
|
||||||
bool present = request.Headers.TryGetValue("Authorization", out var authorization);
|
bool present = request.Headers.TryGetValue("Authorization", out string authorization);
|
||||||
if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < MatchScores.Perfect)
|
if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < MatchScores.Perfect)
|
||||||
{
|
{
|
||||||
response = new ResponseMessage { StatusCode = 401 };
|
response = new ResponseMessage { StatusCode = 401 };
|
||||||
@@ -124,7 +118,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
if (targetMapping.Scenario != null)
|
if (targetMapping.Scenario != null)
|
||||||
{
|
{
|
||||||
_states[targetMapping.Scenario] = targetMapping.NextState;
|
_options.Scenarios[targetMapping.Scenario] = targetMapping.NextState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -153,33 +147,30 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
private void LogRequest(LogEntry entry, bool addRequest)
|
private void LogRequest(LogEntry entry, bool addRequest)
|
||||||
{
|
{
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
if (addRequest)
|
||||||
{
|
{
|
||||||
if (addRequest)
|
_options.LogEntries.Add(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.MaxRequestLogCount != null)
|
||||||
|
{
|
||||||
|
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
|
||||||
|
for (int i = 0; i < amount; i++)
|
||||||
{
|
{
|
||||||
_options.LogEntries.Add(entry);
|
_options.LogEntries.RemoveAt(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (_options.MaxRequestLogCount != null)
|
if (_options.RequestLogExpirationDuration != null)
|
||||||
|
{
|
||||||
|
var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);
|
||||||
|
|
||||||
|
for (var i = _options.LogEntries.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
|
var le = _options.LogEntries[i];
|
||||||
for (int i = 0; i < amount; i++)
|
if (le.RequestMessage.DateTime <= checkTime)
|
||||||
{
|
{
|
||||||
_options.LogEntries.RemoveAt(0);
|
_options.LogEntries.RemoveAt(i);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_options.RequestLogExpirationDuration != null)
|
|
||||||
{
|
|
||||||
var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);
|
|
||||||
|
|
||||||
for (var i = _options.LogEntries.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var le = _options.LogEntries[i];
|
|
||||||
if (le.RequestMessage.DateTime <= checkTime)
|
|
||||||
{
|
|
||||||
_options.LogEntries.RemoveAt(i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
@@ -14,18 +15,14 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
public bool AllowPartialMapping { get; set; }
|
public bool AllowPartialMapping { get; set; }
|
||||||
|
|
||||||
public IList<Mapping> Mappings { get; set; }
|
public IList<Mapping> Mappings { get; set; } = new List<Mapping>();
|
||||||
|
|
||||||
|
public ObservableCollection<LogEntry> LogEntries { get; } = new ObservableCollection<LogEntry>();
|
||||||
|
|
||||||
public ObservableCollection<LogEntry> LogEntries { get; }
|
|
||||||
|
|
||||||
public int? RequestLogExpirationDuration { get; set; }
|
public int? RequestLogExpirationDuration { get; set; }
|
||||||
|
|
||||||
public int? MaxRequestLogCount { get; set; }
|
public int? MaxRequestLogCount { get; set; }
|
||||||
|
|
||||||
public WireMockMiddlewareOptions()
|
public IDictionary<string, object> Scenarios { get; } = new ConcurrentDictionary<string, object>();
|
||||||
{
|
|
||||||
Mappings = new List<Mapping>();
|
|
||||||
LogEntries = new ObservableCollection<LogEntry>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,7 @@ namespace WireMock.Server
|
|||||||
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 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 _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})$");
|
||||||
|
|
||||||
@@ -117,6 +118,15 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
// __admin/requests/find
|
// __admin/requests/find
|
||||||
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).RespondWith(new DynamicResponseProvider(RequestsFind));
|
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
|
#region Proxy and Record
|
||||||
@@ -459,6 +469,26 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
#endregion Requests/find
|
#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)
|
private IRequestBuilder InitRequestBuilder(RequestModel requestModel)
|
||||||
{
|
{
|
||||||
IRequestBuilder requestBuilder = Request.Create();
|
IRequestBuilder requestBuilder = Request.Create();
|
||||||
|
|||||||
@@ -19,36 +19,15 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public event NotifyCollectionChangedEventHandler LogEntriesChanged
|
public event NotifyCollectionChangedEventHandler LogEntriesChanged
|
||||||
{
|
{
|
||||||
add
|
add => _options.LogEntries.CollectionChanged += value;
|
||||||
{
|
remove => _options.LogEntries.CollectionChanged -= value;
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
|
||||||
{
|
|
||||||
_options.LogEntries.CollectionChanged += value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
remove
|
|
||||||
{
|
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
|
||||||
{
|
|
||||||
_options.LogEntries.CollectionChanged -= value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the request logs.
|
/// Gets the request logs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> LogEntries
|
public IEnumerable<LogEntry> LogEntries => new ReadOnlyCollection<LogEntry>(_options.LogEntries);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
|
||||||
{
|
|
||||||
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The search log-entries based on matchers.
|
/// The search log-entries based on matchers.
|
||||||
@@ -58,26 +37,23 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
|
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>();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
foreach (var matcher in matchers)
|
||||||
foreach (var log in _options.LogEntries)
|
|
||||||
{
|
{
|
||||||
var requestMatchResult = new RequestMatchResult();
|
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
||||||
foreach (var matcher in matchers)
|
|
||||||
{
|
|
||||||
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
|
|
||||||
{
|
|
||||||
results.Add(log, 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>
|
/// <summary>
|
||||||
@@ -86,10 +62,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void ResetLogEntries()
|
public void ResetLogEntries()
|
||||||
{
|
{
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
_options.LogEntries.Clear();
|
||||||
{
|
|
||||||
_options.LogEntries.Clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -99,18 +72,15 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public bool DeleteLogEntry(Guid guid)
|
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.
|
_options.LogEntries.Remove(existing);
|
||||||
var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid);
|
return true;
|
||||||
if (existing != null)
|
|
||||||
{
|
|
||||||
_options.LogEntries.Remove(existing);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -44,16 +45,13 @@ namespace WireMock.Server
|
|||||||
/// Gets the mappings.
|
/// Gets the mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<Mapping> Mappings
|
public IEnumerable<Mapping> Mappings => new ReadOnlyCollection<Mapping>(_options.Mappings);
|
||||||
{
|
|
||||||
get
|
/// <summary>
|
||||||
{
|
/// Gets the scenarios.
|
||||||
lock (((ICollection)_options.Mappings).SyncRoot)
|
/// </summary>
|
||||||
{
|
[PublicAPI]
|
||||||
return new ReadOnlyCollection<Mapping>(_options.Mappings);
|
public IDictionary<string, object> Scenarios => new ConcurrentDictionary<string, object>(_options.Scenarios);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Start/Stop
|
#region Start/Stop
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -258,10 +256,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void ResetMappings()
|
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>
|
/// <summary>
|
||||||
@@ -271,18 +266,15 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public bool DeleteMapping(Guid guid)
|
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.
|
_options.Mappings.Remove(existingMapping);
|
||||||
var existingMapping = _options.Mappings.FirstOrDefault(m => m.Guid == guid);
|
return true;
|
||||||
if (existingMapping != null)
|
|
||||||
{
|
|
||||||
_options.Mappings.Remove(existingMapping);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -349,6 +341,15 @@ namespace WireMock.Server
|
|||||||
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resets the Scenarios.
|
||||||
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
|
public void ResetScenarios()
|
||||||
|
{
|
||||||
|
_options.Scenarios.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The given.
|
/// The given.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -368,13 +369,10 @@ namespace WireMock.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
private void RegisterMapping(Mapping mapping)
|
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);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
// var sut = _objectMother.Create();
|
// var sut = _objectMother.Create();
|
||||||
|
|
||||||
// // then
|
// // then
|
||||||
// Check.That(sut.States).IsNull();
|
// Check.That(sut.Scenarios).IsNull();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// private class ObjectMother
|
// private class ObjectMother
|
||||||
|
|||||||
Reference in New Issue
Block a user