Observable logs (refactor some code)

This commit is contained in:
Stef Heyenrath
2017-10-07 15:28:36 +02:00
parent 2d39a18b70
commit 207688e42b
9 changed files with 221 additions and 155 deletions

View File

@@ -447,7 +447,7 @@ namespace WireMock.Server
foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/")))
{
var requestMatchResult = new RequestMatchResult();
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > 0.99)
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > MatchScores.AlmostPerfect)
{
dict.Add(logEntry, requestMatchResult);
}
@@ -559,7 +559,6 @@ namespace WireMock.Server
}
return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName);
}
if (responseModel.StatusCode.HasValue)

View File

@@ -1,113 +1,116 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers.Request;
using System.Linq;
namespace WireMock.Server
{
public partial class FluentMockServer
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers.Request;
using System.Linq;
using WireMock.Matchers;
namespace WireMock.Server
{
public partial class FluentMockServer
{
/// <summary>
/// Log entries notification handler
/// </summary>
[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;
}
}
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
}
}
}
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[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 requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > 0.99)
results.Add(log, requestMatchResult);
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
}
}
/// <summary>
/// Resets the LogEntries.
/// </summary>
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.Clear();
}
}
/// <summary>
/// Deletes the mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
[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)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}
}
/// </summary>
[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;
}
}
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
}
}
}
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[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 requestMatchResult = new 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());
}
}
/// <summary>
/// Resets the LogEntries.
/// </summary>
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.Clear();
}
}
/// <summary>
/// Deletes the mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
[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)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}
}

View File

@@ -23,7 +23,6 @@ namespace WireMock.Server
{
private const int ServerStartDelay = 100;
private readonly IOwinSelfHost _httpServer;
private readonly object _syncRoot = new object();
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
/// <summary>
@@ -293,10 +292,7 @@ namespace WireMock.Server
[PublicAPI]
public void AddGlobalProcessingDelay(TimeSpan delay)
{
lock (_syncRoot)
{
_options.RequestProcessingDelay = delay;
}
_options.RequestProcessingDelay = delay;
}
/// <summary>
@@ -305,10 +301,7 @@ namespace WireMock.Server
[PublicAPI]
public void AllowPartialMapping()
{
lock (_syncRoot)
{
_options.AllowPartialMapping = true;
}
_options.AllowPartialMapping = true;
}
/// <summary>
@@ -323,10 +316,7 @@ namespace WireMock.Server
Check.NotNull(password, nameof(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>
@@ -335,10 +325,7 @@ namespace WireMock.Server
[PublicAPI]
public void RemoveBasicAuthentication()
{
lock (_syncRoot)
{
_options.AuthorizationMatcher = null;
}
_options.AuthorizationMatcher = null;
}
/// <summary>
@@ -348,10 +335,8 @@ namespace WireMock.Server
[PublicAPI]
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
{
lock (_syncRoot)
{
_options.MaxRequestLogCount = maxRequestLogCount;
}
_options.MaxRequestLogCount = maxRequestLogCount;
}
/// <summary>
@@ -361,10 +346,7 @@ namespace WireMock.Server
[PublicAPI]
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
{
lock (_syncRoot)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
/// <summary>