mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 00:59:02 +01:00
goed
This commit is contained in:
@@ -18,7 +18,7 @@ public class LogRequestModel
|
||||
/// <summary>
|
||||
/// The DateTime.
|
||||
/// </summary>
|
||||
public DateTime DateTime { get; set; }
|
||||
public required DateTime DateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Path.
|
||||
@@ -58,7 +58,7 @@ public class LogRequestModel
|
||||
/// <summary>
|
||||
/// The HTTP Version.
|
||||
/// </summary>
|
||||
public string HttpVersion { get; set; } = null!;
|
||||
public string? HttpVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Headers.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Collections.Generic;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Types;
|
||||
|
||||
@@ -80,4 +79,14 @@ public class LogResponseModel
|
||||
/// Gets or sets the Fault percentage.
|
||||
/// </summary>
|
||||
public double? FaultPercentage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The DateTime.
|
||||
/// </summary>
|
||||
public required DateTime DateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The method.
|
||||
/// </summary>
|
||||
public string? Method { get; set; }
|
||||
}
|
||||
@@ -56,6 +56,11 @@ public interface IResponseMessage
|
||||
/// </summary>
|
||||
DateTime DateTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the method.
|
||||
/// </summary>
|
||||
string? Method { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds the header.
|
||||
/// </summary>
|
||||
|
||||
@@ -52,23 +52,23 @@ internal class WireMockMiddlewareLogger(
|
||||
|
||||
public void LogLogEntry(LogEntry entry, bool addRequest)
|
||||
{
|
||||
if (entry.RequestMessage != null)
|
||||
{
|
||||
_options.Logger.DebugRequestResponse(_logEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));
|
||||
_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 && _options.MaxRequestLogCount is null or > 0)
|
||||
{
|
||||
TryAddLogEntry(entry);
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
TryAddLogEntry(entry);
|
||||
}
|
||||
|
||||
|
||||
// In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
|
||||
if (_options.MaxRequestLogCount is > 0)
|
||||
{
|
||||
var logEntries = _options.LogEntries.Where(le => le.RequestMessage != null).ToList();
|
||||
var logEntries = _options.LogEntries.ToList();
|
||||
|
||||
foreach (var logEntry in logEntries.OrderBy(le => le.RequestMessage!.DateTime).Take(logEntries.Count - _options.MaxRequestLogCount.Value))
|
||||
foreach (var logEntry in logEntries
|
||||
.OrderBy(le => le.RequestMessage?.DateTime ?? le.ResponseMessage?.DateTime)
|
||||
.Take(logEntries.Count - _options.MaxRequestLogCount.Value))
|
||||
{
|
||||
TryRemoveLogEntry(logEntry);
|
||||
}
|
||||
@@ -77,10 +77,10 @@ internal class WireMockMiddlewareLogger(
|
||||
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
|
||||
if (_options.RequestLogExpirationDuration is > 0)
|
||||
{
|
||||
var logEntries = _options.LogEntries.Where(le => le.RequestMessage != null).ToList();
|
||||
var logEntries = _options.LogEntries.ToList();
|
||||
|
||||
var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);
|
||||
foreach (var logEntry in logEntries.Where(le => le.RequestMessage!.DateTime < checkTime))
|
||||
foreach (var logEntry in logEntries.Where(le => le.RequestMessage?.DateTime < checkTime || le.ResponseMessage?.DateTime < checkTime))
|
||||
{
|
||||
TryRemoveLogEntry(logEntry);
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ public class ResponseMessage : IResponseMessage
|
||||
/// <inheritdoc />
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? Method { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public void AddHeader(string name, string value)
|
||||
{
|
||||
|
||||
@@ -565,7 +565,7 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
||||
bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = messageType.ToString(),
|
||||
DetectedBodyType = BodyType.Bytes
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
}
|
||||
|
||||
@@ -595,16 +595,13 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
||||
// Sent message - log as response
|
||||
responseMessage = new ResponseMessage
|
||||
{
|
||||
Method = method,
|
||||
StatusCode = HttpStatusCode.SwitchingProtocols, // WebSocket status
|
||||
BodyData = bodyData,
|
||||
DateTime = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
// Create a perfect match result
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
requestMatchResult.AddScore(typeof(WebSocketMessageDirection), MatchScores.Perfect, null);
|
||||
|
||||
// Create log entry
|
||||
var logEntry = new LogEntry
|
||||
{
|
||||
@@ -612,8 +609,7 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
||||
RequestMessage = requestMessage,
|
||||
ResponseMessage = responseMessage,
|
||||
MappingGuid = context.Mapping.Guid,
|
||||
MappingTitle = context.Mapping.Title,
|
||||
RequestMatchResult = requestMatchResult
|
||||
MappingTitle = context.Mapping.Title
|
||||
};
|
||||
|
||||
// Enrich activity if present
|
||||
|
||||
@@ -71,6 +71,8 @@ internal class LogEntryMapper(IWireMockMiddlewareOptions options)
|
||||
{
|
||||
logResponseModel = new LogResponseModel
|
||||
{
|
||||
DateTime = logEntry.ResponseMessage.DateTime,
|
||||
Method = logEntry.ResponseMessage.Method,
|
||||
StatusCode = logEntry.ResponseMessage.StatusCode,
|
||||
Headers = logEntry.ResponseMessage.Headers
|
||||
};
|
||||
|
||||
@@ -6,7 +6,6 @@ using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Stef.Validation;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -167,7 +166,7 @@ public class WireMockWebSocketContext : IWebSocketContext
|
||||
bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = messageType.ToString(),
|
||||
DetectedBodyType = BodyType.Bytes
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
}
|
||||
|
||||
@@ -197,16 +196,13 @@ public class WireMockWebSocketContext : IWebSocketContext
|
||||
// Sent message - log as response
|
||||
responseMessage = new ResponseMessage
|
||||
{
|
||||
Method = method,
|
||||
StatusCode = HttpStatusCode.SwitchingProtocols, // WebSocket status
|
||||
BodyData = bodyData,
|
||||
DateTime = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
// Create a perfect match result
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
requestMatchResult.AddScore(typeof(WebSocketMessageDirection), MatchScores.Perfect, null);
|
||||
|
||||
// Create log entry
|
||||
var logEntry = new LogEntry
|
||||
{
|
||||
@@ -214,8 +210,7 @@ public class WireMockWebSocketContext : IWebSocketContext
|
||||
RequestMessage = requestMessage,
|
||||
ResponseMessage = responseMessage,
|
||||
MappingGuid = Mapping.Guid,
|
||||
MappingTitle = Mapping.Title,
|
||||
RequestMatchResult = requestMatchResult
|
||||
MappingTitle = Mapping.Title
|
||||
};
|
||||
|
||||
// Enrich activity if present
|
||||
|
||||
Reference in New Issue
Block a user