Skip to content

Commit

Permalink
Observable logs (#51)
Browse files Browse the repository at this point in the history
* observable log entries

* event test
  • Loading branch information
dmtrrk authored and StefH committed Oct 7, 2017
1 parent 9c55ff5 commit 2d39a18
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/WireMock.Net/Owin/WireMockMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -161,13 +162,20 @@ private void LogRequest(LogEntry entry, bool addRequest)

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)
{
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);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using WireMock.Logging;
using WireMock.Matchers;

Expand All @@ -15,7 +16,7 @@ internal class WireMockMiddlewareOptions

public IList<Mapping> Mappings { get; set; }

public IList<LogEntry> LogEntries { get; set; }
public ObservableCollection<LogEntry> LogEntries { get; set; }

public int? RequestLogExpirationDuration { get; set; }

Expand All @@ -24,7 +25,7 @@ internal class WireMockMiddlewareOptions
public WireMockMiddlewareOptions()
{
Mappings = new List<Mapping>();
LogEntries = new List<LogEntry>();
LogEntries = new ObservableCollection<LogEntry>();
}
}
}
23 changes: 23 additions & 0 deletions src/WireMock.Net/Server/FluentMockServer.LogEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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;
Expand All @@ -11,6 +12,28 @@ 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>
Expand Down
50 changes: 50 additions & 0 deletions test/WireMock.Net.Tests/ObservableLogEntriesTest.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 2d39a18

Please sign in to comment.