Observable logs (#51)

* observable log entries

* event test
This commit is contained in:
deeptowncitizen
2017-10-07 09:05:02 -04:00
committed by Stef Heyenrath
parent 9c55ff5ea6
commit 2d39a18b70
4 changed files with 87 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -161,13 +162,20 @@ namespace WireMock.Owin
if (_options.MaxRequestLogCount != null) if (_options.MaxRequestLogCount != null)
{ {
_options.LogEntries = _options.LogEntries.Skip(_options.LogEntries.Count - _options.MaxRequestLogCount.Value).ToList(); var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
for (var i = 0; i < amount; i++, _options.LogEntries.RemoveAt(0)) ;
} }
if (_options.RequestLogExpirationDuration != null) if (_options.RequestLogExpirationDuration != null)
{ {
var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value); var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);
_options.LogEntries = _options.LogEntries.Where(le => le.RequestMessage.DateTime > checkTime).ToList();
for (var i = _options.LogEntries.Count - 1; i >= 0; i--)
{
var le = _options.LogEntries[i];
if (le.RequestMessage.DateTime <= checkTime)
_options.LogEntries.RemoveAt(i);
}
} }
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers; using WireMock.Matchers;
@@ -15,7 +16,7 @@ namespace WireMock.Owin
public IList<Mapping> Mappings { get; set; } public IList<Mapping> Mappings { get; set; }
public IList<LogEntry> LogEntries { get; set; } public ObservableCollection<LogEntry> LogEntries { get; set; }
public int? RequestLogExpirationDuration { get; set; } public int? RequestLogExpirationDuration { get; set; }
@@ -24,7 +25,7 @@ namespace WireMock.Owin
public WireMockMiddlewareOptions() public WireMockMiddlewareOptions()
{ {
Mappings = new List<Mapping>(); Mappings = new List<Mapping>();
LogEntries = new List<LogEntry>(); LogEntries = new ObservableCollection<LogEntry>();
} }
} }
} }

View File

@@ -2,6 +2,7 @@
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 JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
@@ -11,6 +12,28 @@ namespace WireMock.Server
{ {
public partial class FluentMockServer 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> /// <summary>
/// Gets the request logs. /// Gets the request logs.
/// </summary> /// </summary>

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using RestEase;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;
namespace WireMock.Net.Tests
{
public class ObservableLogEntriesTest: IDisposable
{
private FluentMockServer _server;
[Fact]
public async void Test()
{
// given
_server = FluentMockServer.Start();
_server
.Given(Request.Create()
.WithPath("/foo")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
var count = 0;
_server.LogEntriesChanged += (sender, args) => count++;
// when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
Check.That(count).Equals(1);
}
public void Dispose()
{
_server?.Dispose();
}
}
}