mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-10 03:13:53 +02:00
Observable logs (refactor some code)
This commit is contained in:
@@ -23,6 +23,11 @@ namespace WireMock.Matchers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const double Perfect = 1.0;
|
public const double Perfect = 1.0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The almost perfect match score
|
||||||
|
/// </summary>
|
||||||
|
public const double AlmostPerfect = 0.99;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert a bool to the score.
|
/// Convert a bool to the score.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -25,9 +25,7 @@ namespace WireMock.Owin
|
|||||||
{
|
{
|
||||||
Urls.Add(uriPrefix);
|
Urls.Add(uriPrefix);
|
||||||
|
|
||||||
int port;
|
PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
|
||||||
string host;
|
|
||||||
PortUtil.TryExtractProtocolAndPort(uriPrefix, out host, out port);
|
|
||||||
Ports.Add(port);
|
Ports.Add(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -163,7 +163,10 @@ namespace WireMock.Owin
|
|||||||
if (_options.MaxRequestLogCount != null)
|
if (_options.MaxRequestLogCount != null)
|
||||||
{
|
{
|
||||||
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
|
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
|
||||||
for (var i = 0; i < amount; i++, _options.LogEntries.RemoveAt(0)) ;
|
for (int i = 0; i < amount; i++)
|
||||||
|
{
|
||||||
|
_options.LogEntries.RemoveAt(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_options.RequestLogExpirationDuration != null)
|
if (_options.RequestLogExpirationDuration != null)
|
||||||
@@ -174,7 +177,9 @@ namespace WireMock.Owin
|
|||||||
{
|
{
|
||||||
var le = _options.LogEntries[i];
|
var le = _options.LogEntries[i];
|
||||||
if (le.RequestMessage.DateTime <= checkTime)
|
if (le.RequestMessage.DateTime <= checkTime)
|
||||||
|
{
|
||||||
_options.LogEntries.RemoveAt(i);
|
_options.LogEntries.RemoveAt(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
public IList<Mapping> Mappings { get; set; }
|
public IList<Mapping> Mappings { get; set; }
|
||||||
|
|
||||||
public ObservableCollection<LogEntry> LogEntries { get; set; }
|
public ObservableCollection<LogEntry> LogEntries { get; }
|
||||||
|
|
||||||
public int? RequestLogExpirationDuration { get; set; }
|
public int? RequestLogExpirationDuration { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -447,7 +447,7 @@ namespace WireMock.Server
|
|||||||
foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/")))
|
foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/")))
|
||||||
{
|
{
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > 0.99)
|
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > MatchScores.AlmostPerfect)
|
||||||
{
|
{
|
||||||
dict.Add(logEntry, requestMatchResult);
|
dict.Add(logEntry, requestMatchResult);
|
||||||
}
|
}
|
||||||
@@ -559,7 +559,6 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName);
|
return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseModel.StatusCode.HasValue)
|
if (responseModel.StatusCode.HasValue)
|
||||||
|
|||||||
@@ -1,113 +1,116 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using WireMock.Matchers;
|
||||||
namespace WireMock.Server
|
|
||||||
{
|
namespace WireMock.Server
|
||||||
public partial class FluentMockServer
|
{
|
||||||
{
|
public partial class FluentMockServer
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Log entries notification handler
|
/// Log entries notification handler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public event NotifyCollectionChangedEventHandler LogEntriesChanged
|
public event NotifyCollectionChangedEventHandler LogEntriesChanged
|
||||||
{
|
{
|
||||||
add
|
add
|
||||||
{
|
{
|
||||||
lock (((ICollection) _options.LogEntries).SyncRoot)
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
{
|
{
|
||||||
_options.LogEntries.CollectionChanged += value;
|
_options.LogEntries.CollectionChanged += value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
remove
|
remove
|
||||||
{
|
{
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
{
|
{
|
||||||
_options.LogEntries.CollectionChanged -= value;
|
_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
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
{
|
{
|
||||||
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
|
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The search log-entries based on matchers.
|
/// The search log-entries based on matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchers">The matchers.</param>
|
/// <param name="matchers">The matchers.</param>
|
||||||
/// <returns>The <see cref="IEnumerable"/>.</returns>
|
/// <returns>The <see cref="IEnumerable"/>.</returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
|
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
|
||||||
{
|
{
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
{
|
{
|
||||||
var results = new Dictionary<LogEntry, RequestMatchResult>();
|
var results = new Dictionary<LogEntry, RequestMatchResult>();
|
||||||
|
|
||||||
foreach (var log in _options.LogEntries)
|
foreach (var log in _options.LogEntries)
|
||||||
{
|
{
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
foreach (var matcher in matchers)
|
foreach (var matcher in matchers)
|
||||||
{
|
{
|
||||||
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestMatchResult.AverageTotalScore > 0.99)
|
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
|
||||||
results.Add(log, requestMatchResult);
|
{
|
||||||
}
|
results.Add(log, requestMatchResult);
|
||||||
|
}
|
||||||
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
|
}
|
||||||
}
|
|
||||||
}
|
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
|
||||||
|
}
|
||||||
/// <summary>
|
}
|
||||||
/// Resets the LogEntries.
|
|
||||||
/// </summary>
|
/// <summary>
|
||||||
[PublicAPI]
|
/// Resets the LogEntries.
|
||||||
public void ResetLogEntries()
|
/// </summary>
|
||||||
{
|
[PublicAPI]
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
public void ResetLogEntries()
|
||||||
{
|
{
|
||||||
_options.LogEntries.Clear();
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
}
|
{
|
||||||
}
|
_options.LogEntries.Clear();
|
||||||
|
}
|
||||||
/// <summary>
|
}
|
||||||
/// Deletes the mapping.
|
|
||||||
/// </summary>
|
/// <summary>
|
||||||
/// <param name="guid">The unique identifier.</param>
|
/// Deletes the mapping.
|
||||||
[PublicAPI]
|
/// </summary>
|
||||||
public bool DeleteLogEntry(Guid guid)
|
/// <param name="guid">The unique identifier.</param>
|
||||||
{
|
[PublicAPI]
|
||||||
lock (((ICollection)_options.LogEntries).SyncRoot)
|
public bool DeleteLogEntry(Guid guid)
|
||||||
{
|
{
|
||||||
// Check a logentry exists with the same GUID, if so, remove it.
|
lock (((ICollection)_options.LogEntries).SyncRoot)
|
||||||
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);
|
||||||
_options.LogEntries.Remove(existing);
|
if (existing != null)
|
||||||
return true;
|
{
|
||||||
}
|
_options.LogEntries.Remove(existing);
|
||||||
|
return true;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
private const int ServerStartDelay = 100;
|
private const int ServerStartDelay = 100;
|
||||||
private readonly IOwinSelfHost _httpServer;
|
private readonly IOwinSelfHost _httpServer;
|
||||||
private readonly object _syncRoot = new object();
|
|
||||||
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -293,10 +292,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddGlobalProcessingDelay(TimeSpan delay)
|
public void AddGlobalProcessingDelay(TimeSpan delay)
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
_options.RequestProcessingDelay = delay;
|
||||||
{
|
|
||||||
_options.RequestProcessingDelay = delay;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -305,10 +301,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AllowPartialMapping()
|
public void AllowPartialMapping()
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
_options.AllowPartialMapping = true;
|
||||||
{
|
|
||||||
_options.AllowPartialMapping = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -323,10 +316,7 @@ namespace WireMock.Server
|
|||||||
Check.NotNull(password, nameof(password));
|
Check.NotNull(password, nameof(password));
|
||||||
|
|
||||||
string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
|
string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
|
||||||
lock (_syncRoot)
|
_options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
|
||||||
{
|
|
||||||
_options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -335,10 +325,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void RemoveBasicAuthentication()
|
public void RemoveBasicAuthentication()
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
_options.AuthorizationMatcher = null;
|
||||||
{
|
|
||||||
_options.AuthorizationMatcher = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -348,10 +335,8 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
_options.MaxRequestLogCount = maxRequestLogCount;
|
||||||
{
|
|
||||||
_options.MaxRequestLogCount = maxRequestLogCount;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -361,10 +346,7 @@ namespace WireMock.Server
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
||||||
{
|
|
||||||
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
81
src/WireMock.Net/Util/NamedReaderWriterLocker.cs
Normal file
81
src/WireMock.Net/Util/NamedReaderWriterLocker.cs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace WireMock.Util
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// http://johnculviner.com/achieving-named-lock-locker-functionality-in-c-4-0/
|
||||||
|
/// </summary>
|
||||||
|
internal class NamedReaderWriterLocker
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<string, ReaderWriterLockSlim> _lockDict = new ConcurrentDictionary<string, ReaderWriterLockSlim>();
|
||||||
|
|
||||||
|
public ReaderWriterLockSlim GetLock(string name)
|
||||||
|
{
|
||||||
|
return _lockDict.GetOrAdd(name, s => new ReaderWriterLockSlim());
|
||||||
|
}
|
||||||
|
|
||||||
|
public TResult RunWithReadLock<TResult>(string name, Func<TResult> body)
|
||||||
|
{
|
||||||
|
var rwLock = GetLock(name);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rwLock.EnterReadLock();
|
||||||
|
return body();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
rwLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RunWithReadLock(string name, Action body)
|
||||||
|
{
|
||||||
|
var rwLock = GetLock(name);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rwLock.EnterReadLock();
|
||||||
|
body();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
rwLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TResult RunWithWriteLock<TResult>(string name, Func<TResult> body)
|
||||||
|
{
|
||||||
|
var rwLock = GetLock(name);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rwLock.EnterWriteLock();
|
||||||
|
return body();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
rwLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RunWithWriteLock(string name, Action body)
|
||||||
|
{
|
||||||
|
var rwLock = GetLock(name);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rwLock.EnterWriteLock();
|
||||||
|
body();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
rwLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveLock(string name)
|
||||||
|
{
|
||||||
|
ReaderWriterLockSlim o;
|
||||||
|
_lockDict.TryRemove(name, out o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using RestEase;
|
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
@@ -21,7 +15,7 @@ namespace WireMock.Net.Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async void Test()
|
public async void Test()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
_server
|
_server
|
||||||
@@ -29,16 +23,15 @@ namespace WireMock.Net.Tests
|
|||||||
.WithPath("/foo")
|
.WithPath("/foo")
|
||||||
.UsingGet())
|
.UsingGet())
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithStatusCode(200)
|
|
||||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||||
|
|
||||||
var count = 0;
|
int count = 0;
|
||||||
_server.LogEntriesChanged += (sender, args) => count++;
|
_server.LogEntriesChanged += (sender, args) => count++;
|
||||||
|
|
||||||
// when
|
// Act
|
||||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(count).Equals(1);
|
Check.That(count).Equals(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user