mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-25 10:19:04 +02:00
FindLogEntries (#1224)
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
|
|
||||||
namespace WireMock.Server;
|
namespace WireMock.Server;
|
||||||
@@ -27,12 +28,12 @@ public interface IWireMockServer : IDisposable
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the request logs.
|
/// Gets the request logs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IEnumerable<ILogEntry> LogEntries { get; }
|
IReadOnlyList<ILogEntry> LogEntries { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the mappings as MappingModels.
|
/// Gets the mappings as MappingModels.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IEnumerable<MappingModel> MappingModels { get; }
|
IReadOnlyList<MappingModel> MappingModels { get; }
|
||||||
|
|
||||||
// <summary>
|
// <summary>
|
||||||
// Gets the mappings.
|
// Gets the mappings.
|
||||||
@@ -109,7 +110,12 @@ public interface IWireMockServer : IDisposable
|
|||||||
/// <param name="guid">The unique identifier.</param>
|
/// <param name="guid">The unique identifier.</param>
|
||||||
bool DeleteMapping(Guid guid);
|
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);
|
// IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false);
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,14 @@ internal class RequestMessageMethodMatcher : IRequestMatcher
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string[] Methods { get; }
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageMethodMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageMethodMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace WireMock.Server;
|
|||||||
|
|
||||||
public partial class WireMockServer
|
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}
|
#region Files/{filename}
|
||||||
private IResponseMessage FilePost(IRequestMessage requestMessage)
|
private IResponseMessage FilePost(IRequestMessage requestMessage)
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ public partial class WireMockServer
|
|||||||
}
|
}
|
||||||
else if (responseModel.HeadersRaw != null)
|
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);
|
int indexColon = headerLine.IndexOf(":", StringComparison.Ordinal);
|
||||||
string key = headerLine.Substring(0, indexColon).TrimStart(' ', '\t');
|
string key = headerLine.Substring(0, indexColon).TrimStart(' ', '\t');
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -24,23 +22,20 @@ public partial class WireMockServer
|
|||||||
remove => _logEntriesChanged -= value;
|
remove => _logEntriesChanged -= value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.LogEntries" />
|
/// <inheritdoc />
|
||||||
[PublicAPI]
|
[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.
|
/// <inheritdoc />
|
||||||
/// </summary>
|
|
||||||
/// <param name="matchers">The matchers.</param>
|
|
||||||
/// <returns>The <see cref="IEnumerable"/>.</returns>
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> FindLogEntries(params IRequestMatcher[] matchers)
|
public IReadOnlyList<ILogEntry> FindLogEntries(params IRequestMatcher[] matchers)
|
||||||
{
|
{
|
||||||
Guard.NotNull(matchers);
|
Guard.NotNull(matchers);
|
||||||
|
|
||||||
var results = new Dictionary<LogEntry, RequestMatchResult>();
|
var results = new Dictionary<LogEntry, RequestMatchResult>();
|
||||||
|
|
||||||
foreach (var log in _options.LogEntries.ToList())
|
foreach (var log in _options.LogEntries.ToArray())
|
||||||
{
|
{
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
foreach (var matcher in matchers)
|
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" />
|
/// <inheritdoc cref="IWireMockServer.ResetLogEntries" />
|
||||||
|
|||||||
@@ -84,11 +84,11 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
/// Gets the mappings.
|
/// Gets the mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
public IReadOnlyList<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
|
public IReadOnlyList<MappingModel> MappingModels => ToMappingModels();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the scenarios.
|
/// Gets the scenarios.
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ using RestEase;
|
|||||||
using WireMock.Client;
|
using WireMock.Client;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -258,7 +259,7 @@ public class WireMockServerAdminTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var mapping = server.Mappings.First(m => !m.IsAdminInterface);
|
var mapping = server.Mappings.First(m => !m.IsAdminInterface);
|
||||||
var request = (Request) mapping.RequestMatcher;
|
var request = (Request)mapping.RequestMatcher;
|
||||||
var pathMatcher = request.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
var pathMatcher = request.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
||||||
pathMatcher.Should().BeNull();
|
pathMatcher.Should().BeNull();
|
||||||
|
|
||||||
@@ -428,6 +429,34 @@ public class WireMockServerAdminTests
|
|||||||
server.Stop();
|
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]
|
[Fact]
|
||||||
public void WireMockServer_Admin_WatchStaticMappings()
|
public void WireMockServer_Admin_WatchStaticMappings()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user