FindLogEntries (#1224)

This commit is contained in:
Stef Heyenrath
2024-12-23 20:00:03 +01:00
committed by GitHub
parent c4ae4eaf8e
commit 485f7ad952
7 changed files with 61 additions and 20 deletions

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using WireMock.Admin.Mappings;
using WireMock.Logging;
using WireMock.Matchers.Request;
using WireMock.Types;
namespace WireMock.Server;
@@ -27,12 +28,12 @@ public interface IWireMockServer : IDisposable
/// <summary>
/// Gets the request logs.
/// </summary>
IEnumerable<ILogEntry> LogEntries { get; }
IReadOnlyList<ILogEntry> LogEntries { get; }
/// <summary>
/// Gets the mappings as MappingModels.
/// </summary>
IEnumerable<MappingModel> MappingModels { get; }
IReadOnlyList<MappingModel> MappingModels { get; }
// <summary>
// Gets the mappings.
@@ -109,7 +110,12 @@ public interface IWireMockServer : IDisposable
/// <param name="guid">The unique identifier.</param>
bool DeleteMapping(Guid guid);
//IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers);
/// <summary>
/// Search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The request matchers to use.</param>
/// <returns>The <see cref="IReadOnlyList{ILogEntry}"/>.</returns>
IReadOnlyList<ILogEntry> FindLogEntries(params IRequestMatcher[] matchers);
// IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false);

View File

@@ -26,6 +26,14 @@ internal class RequestMessageMethodMatcher : IRequestMatcher
/// </summary>
public string[] Methods { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageMethodMatcher"/> class.
/// </summary>
/// <param name="methods">The methods.</param>
public RequestMessageMethodMatcher(params string[] methods) : this(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, methods)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageMethodMatcher"/> class.
/// </summary>

View File

@@ -11,7 +11,7 @@ namespace WireMock.Server;
public partial class WireMockServer
{
private static readonly Encoding[] FileBodyIsString = { Encoding.UTF8, Encoding.ASCII };
private static readonly Encoding[] FileBodyIsString = [Encoding.UTF8, Encoding.ASCII];
#region Files/{filename}
private IResponseMessage FilePost(IRequestMessage requestMessage)

View File

@@ -308,7 +308,7 @@ public partial class WireMockServer
}
else if (responseModel.HeadersRaw != null)
{
foreach (string headerLine in responseModel.HeadersRaw.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
foreach (string headerLine in responseModel.HeadersRaw.Split(["\n", "\r\n"], StringSplitOptions.RemoveEmptyEntries))
{
int indexColon = headerLine.IndexOf(":", StringComparison.Ordinal);
string key = headerLine.Substring(0, indexColon).TrimStart(' ', '\t');

View File

@@ -1,9 +1,7 @@
// Copyright © WireMock.Net
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
@@ -24,23 +22,20 @@ public partial class WireMockServer
remove => _logEntriesChanged -= value;
}
/// <inheritdoc cref="IWireMockServer.LogEntries" />
/// <inheritdoc />
[PublicAPI]
public IEnumerable<ILogEntry> LogEntries => new ReadOnlyCollection<LogEntry>(_options.LogEntries.ToList());
public IReadOnlyList<ILogEntry> LogEntries => _options.LogEntries.ToArray();
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
/// <inheritdoc />
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries(params IRequestMatcher[] matchers)
public IReadOnlyList<ILogEntry> FindLogEntries(params IRequestMatcher[] matchers)
{
Guard.NotNull(matchers);
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries.ToList())
foreach (var log in _options.LogEntries.ToArray())
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
@@ -54,7 +49,10 @@ public partial class WireMockServer
}
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
return results
.OrderBy(x => x.Value)
.Select(x => x.Key)
.ToArray();
}
/// <inheritdoc cref="IWireMockServer.ResetLogEntries" />

View File

@@ -84,11 +84,11 @@ public partial class WireMockServer : IWireMockServer
/// Gets the mappings.
/// </summary>
[PublicAPI]
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
public IReadOnlyList<IMapping> Mappings => _options.Mappings.Values.ToArray();
/// <inheritdoc cref="IWireMockServer.MappingModels" />
[PublicAPI]
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
public IReadOnlyList<MappingModel> MappingModels => ToMappingModels();
/// <summary>
/// Gets the scenarios.

View File

@@ -15,6 +15,7 @@ using RestEase;
using WireMock.Client;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
@@ -258,7 +259,7 @@ public class WireMockServerAdminTests
// Assert
var mapping = server.Mappings.First(m => !m.IsAdminInterface);
var request = (Request) mapping.RequestMatcher;
var request = (Request)mapping.RequestMatcher;
var pathMatcher = request.GetRequestMessageMatcher<RequestMessagePathMatcher>();
pathMatcher.Should().BeNull();
@@ -428,6 +429,34 @@ public class WireMockServerAdminTests
server.Stop();
}
[Fact]
public async Task WireMockServer_Admin_Logging_FindLogEntries()
{
// Assign
using var client = new HttpClient();
// Act
using var server = WireMockServer.Start();
var tasks = new[]
{
client.GetAsync($"{server.Url}/foo1"),
client.PostAsync($"{server.Url}/foo2", new StringContent("test")),
client.GetAsync($"{server.Url}/foo3")
};
await Task.WhenAll(tasks).ConfigureAwait(false);
// Act
var logEntries = server.FindLogEntries(new RequestMessageMethodMatcher("GET"));
// Assert
logEntries.Should().HaveCount(2);
logEntries.Single(le => le.RequestMessage.Path.EndsWith("foo1")).Should().NotBeNull();
logEntries.Single(le => le.RequestMessage.Path.EndsWith("foo3")).Should().NotBeNull();
}
[Fact]
public void WireMockServer_Admin_WatchStaticMappings()
{