mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-08 22:05:13 +02:00
When Response.Create() is called without a pre-built ResponseMessage, use the DateTime.UtcNow (#1487)
This commit is contained in:
@@ -9,16 +9,17 @@ using WireMock.Util;
|
|||||||
namespace WireMock.Owin;
|
namespace WireMock.Owin;
|
||||||
|
|
||||||
internal class WireMockMiddlewareLogger(
|
internal class WireMockMiddlewareLogger(
|
||||||
IWireMockMiddlewareOptions _options,
|
IWireMockMiddlewareOptions options,
|
||||||
LogEntryMapper _logEntryMapper,
|
LogEntryMapper logEntryMapper,
|
||||||
IGuidUtils _guidUtils
|
IGuidUtils guidUtils,
|
||||||
|
IDateTimeUtils dateTimeUtils
|
||||||
) : IWireMockMiddlewareLogger
|
) : IWireMockMiddlewareLogger
|
||||||
{
|
{
|
||||||
public void LogRequestAndResponse(bool logRequest, RequestMessage request, IResponseMessage? response, MappingMatcherResult? match, MappingMatcherResult? partialMatch, Activity? activity)
|
public void LogRequestAndResponse(bool logRequest, RequestMessage request, IResponseMessage? response, MappingMatcherResult? match, MappingMatcherResult? partialMatch, Activity? activity)
|
||||||
{
|
{
|
||||||
var logEntry = new LogEntry
|
var logEntry = new LogEntry
|
||||||
{
|
{
|
||||||
Guid = _guidUtils.NewGuid(),
|
Guid = guidUtils.NewGuid(),
|
||||||
RequestMessage = request,
|
RequestMessage = request,
|
||||||
ResponseMessage = response,
|
ResponseMessage = response,
|
||||||
|
|
||||||
@@ -31,17 +32,17 @@ internal class WireMockMiddlewareLogger(
|
|||||||
PartialMatchResult = partialMatch?.RequestMatchResult
|
PartialMatchResult = partialMatch?.RequestMatchResult
|
||||||
};
|
};
|
||||||
|
|
||||||
WireMockActivitySource.EnrichWithLogEntry(activity, logEntry, _options.ActivityTracingOptions);
|
WireMockActivitySource.EnrichWithLogEntry(activity, logEntry, options.ActivityTracingOptions);
|
||||||
activity?.Dispose();
|
activity?.Dispose();
|
||||||
|
|
||||||
LogLogEntry(logEntry, logRequest);
|
LogLogEntry(logEntry, logRequest);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_options.SaveUnmatchedRequests == true && match?.RequestMatchResult is not { IsPerfectMatch: true })
|
if (options.SaveUnmatchedRequests == true && match?.RequestMatchResult is not { IsPerfectMatch: true })
|
||||||
{
|
{
|
||||||
var filename = $"{logEntry.Guid}.LogEntry.json";
|
var filename = $"{logEntry.Guid}.LogEntry.json";
|
||||||
_options.FileSystemHandler?.WriteUnmatchedRequest(filename, _options.DefaultJsonSerializer.Serialize(logEntry));
|
options.FileSystemHandler?.WriteUnmatchedRequest(filename, options.DefaultJsonSerializer.Serialize(logEntry));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -52,34 +53,34 @@ internal class WireMockMiddlewareLogger(
|
|||||||
|
|
||||||
public void LogLogEntry(LogEntry entry, bool addRequest)
|
public void LogLogEntry(LogEntry entry, bool addRequest)
|
||||||
{
|
{
|
||||||
_options.Logger.DebugRequestResponse(_logEntryMapper.Map(entry), entry.RequestMessage?.Path.StartsWith("/__admin/") == true);
|
options.Logger.DebugRequestResponse(logEntryMapper.Map(entry), entry.RequestMessage?.Path.StartsWith("/__admin/") == true);
|
||||||
|
|
||||||
// If addRequest is set to true and MaxRequestLogCount is null or does have a value greater than 0, try to add a new request log.
|
// If addRequest is set to true and MaxRequestLogCount is null or does have a value greater than 0, try to add a new request log.
|
||||||
if (addRequest && _options.MaxRequestLogCount is null or > 0)
|
if (addRequest && options.MaxRequestLogCount is null or > 0)
|
||||||
{
|
{
|
||||||
TryAddLogEntry(entry);
|
TryAddLogEntry(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
|
// In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
|
||||||
if (_options.MaxRequestLogCount is > 0)
|
if (options.MaxRequestLogCount is > 0)
|
||||||
{
|
{
|
||||||
var logEntries = _options.LogEntries.ToList();
|
var logEntries = options.LogEntries.ToList();
|
||||||
|
|
||||||
foreach (var logEntry in logEntries
|
foreach (var logEntry in logEntries
|
||||||
.OrderBy(le => le.RequestMessage?.DateTime ?? le.ResponseMessage?.DateTime)
|
.OrderBy(le => le.RequestMessage?.DateTime ?? le.ResponseMessage?.DateTime)
|
||||||
.Take(logEntries.Count - _options.MaxRequestLogCount.Value))
|
.Take(logEntries.Count - options.MaxRequestLogCount.Value))
|
||||||
{
|
{
|
||||||
TryRemoveLogEntry(logEntry);
|
TryRemoveLogEntry(logEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
|
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
|
||||||
if (_options.RequestLogExpirationDuration is > 0)
|
if (options.RequestLogExpirationDuration is > 0)
|
||||||
{
|
{
|
||||||
var logEntries = _options.LogEntries.ToList();
|
var logEntries = options.LogEntries.ToList();
|
||||||
|
|
||||||
var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);
|
var checkTime = dateTimeUtils.UtcNow.AddHours(-options.RequestLogExpirationDuration.Value);
|
||||||
foreach (var logEntry in logEntries.Where(le => le.RequestMessage?.DateTime < checkTime || le.ResponseMessage?.DateTime < checkTime))
|
foreach (var logEntry in logEntries.Where(le => le.RequestMessage?.DateTime < checkTime || le.ResponseMessage?.DateTime < checkTime))
|
||||||
{
|
{
|
||||||
TryRemoveLogEntry(logEntry);
|
TryRemoveLogEntry(logEntry);
|
||||||
@@ -91,7 +92,7 @@ internal class WireMockMiddlewareLogger(
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_options.LogEntries.Add(logEntry);
|
options.LogEntries.Add(logEntry);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -103,7 +104,7 @@ internal class WireMockMiddlewareLogger(
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_options.LogEntries.Remove(logEntry);
|
options.LogEntries.Remove(logEntry);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ using WireMock.RequestBuilders;
|
|||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using WireMock.Transformers;
|
using WireMock.Transformers;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Util;
|
|
||||||
|
|
||||||
namespace WireMock.ResponseBuilders;
|
namespace WireMock.ResponseBuilders;
|
||||||
|
|
||||||
@@ -86,7 +85,11 @@ public partial class Response : IResponseBuilder
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static IResponseBuilder Create(ResponseMessage? responseMessage = null)
|
public static IResponseBuilder Create(ResponseMessage? responseMessage = null)
|
||||||
{
|
{
|
||||||
var message = responseMessage ?? new ResponseMessage();
|
var message = responseMessage ?? new ResponseMessage
|
||||||
|
{
|
||||||
|
DateTime = DateTime.UtcNow // We set it here to the current time.
|
||||||
|
};
|
||||||
|
|
||||||
return new Response(message);
|
return new Response(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user