mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 23:44:43 +01:00
Fixed logic for SaveUnmatchedRequests (#1002)
* Fixed logic for SaveUnmatchedRequests * fix
This commit is contained in:
@@ -70,6 +70,7 @@ namespace WireMock.Owin
|
||||
services.AddSingleton<IRandomizerDoubleBetween0And1, RandomizerDoubleBetween0And1>();
|
||||
services.AddSingleton<IOwinRequestMapper, OwinRequestMapper>();
|
||||
services.AddSingleton<IOwinResponseMapper, OwinResponseMapper>();
|
||||
services.AddSingleton<IGuidUtils, GuidUtils>();
|
||||
|
||||
#if NETCOREAPP3_1 || NET5_0 || NET6_0 || NET7_0
|
||||
AddCors(services);
|
||||
|
||||
@@ -9,9 +9,8 @@ using JetBrains.Annotations;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Owin.Mappers;
|
||||
using Stef.Validation;
|
||||
using RandomDataGenerator.FieldOptions;
|
||||
using RandomDataGenerator.Randomizers;
|
||||
using WireMock.Services;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Owin;
|
||||
|
||||
@@ -63,7 +62,7 @@ internal class OwinSelfHost : IOwinSelfHost
|
||||
private void StartServers()
|
||||
{
|
||||
#if NET46
|
||||
_logger.Info("Server using .net 4.6.1 or higher");
|
||||
_logger.Info("Server using .net 4.6");
|
||||
#else
|
||||
_logger.Info("Server using .net 4.5.x");
|
||||
#endif
|
||||
@@ -74,12 +73,13 @@ internal class OwinSelfHost : IOwinSelfHost
|
||||
var requestMapper = new OwinRequestMapper();
|
||||
var responseMapper = new OwinResponseMapper(_options);
|
||||
var matcher = new MappingMatcher(_options, new RandomizerDoubleBetween0And1());
|
||||
var guidUtils = new GuidUtils();
|
||||
|
||||
Action<IAppBuilder> startup = app =>
|
||||
{
|
||||
app.Use<GlobalExceptionMiddleware>(_options, responseMapper);
|
||||
_options.PreWireMockMiddlewareInit?.Invoke(app);
|
||||
app.Use<WireMockMiddleware>(_options, requestMapper, responseMapper, matcher);
|
||||
app.Use<WireMockMiddleware>(_options, requestMapper, responseMapper, matcher, guidUtils);
|
||||
_options.PostWireMockMiddlewareInit?.Invoke(app);
|
||||
};
|
||||
|
||||
|
||||
@@ -30,29 +30,47 @@ namespace WireMock.Owin
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
private static readonly Task CompletedTask = Task.FromResult(false);
|
||||
|
||||
private readonly IWireMockMiddlewareOptions _options;
|
||||
private readonly IOwinRequestMapper _requestMapper;
|
||||
private readonly IOwinResponseMapper _responseMapper;
|
||||
private readonly IMappingMatcher _mappingMatcher;
|
||||
private readonly LogEntryMapper _logEntryMapper;
|
||||
private readonly IGuidUtils _guidUtils;
|
||||
|
||||
#if !USE_ASPNETCORE
|
||||
public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwinResponseMapper responseMapper, IMappingMatcher mappingMatcher) : base(next)
|
||||
public WireMockMiddleware(
|
||||
Next next,
|
||||
IWireMockMiddlewareOptions options,
|
||||
IOwinRequestMapper requestMapper,
|
||||
IOwinResponseMapper responseMapper,
|
||||
IMappingMatcher mappingMatcher,
|
||||
IGuidUtils guidUtils
|
||||
) : base(next)
|
||||
{
|
||||
_options = Guard.NotNull(options);
|
||||
_requestMapper = Guard.NotNull(requestMapper);
|
||||
_responseMapper = Guard.NotNull(responseMapper);
|
||||
_mappingMatcher = Guard.NotNull(mappingMatcher);
|
||||
_logEntryMapper = new LogEntryMapper(options);
|
||||
_guidUtils = Guard.NotNull(guidUtils);
|
||||
}
|
||||
#else
|
||||
public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwinResponseMapper responseMapper, IMappingMatcher mappingMatcher)
|
||||
public WireMockMiddleware(
|
||||
Next next,
|
||||
IWireMockMiddlewareOptions options,
|
||||
IOwinRequestMapper requestMapper,
|
||||
IOwinResponseMapper responseMapper,
|
||||
IMappingMatcher mappingMatcher,
|
||||
IGuidUtils guidUtils
|
||||
)
|
||||
{
|
||||
_options = Guard.NotNull(options);
|
||||
_requestMapper = Guard.NotNull(requestMapper);
|
||||
_responseMapper = Guard.NotNull(responseMapper);
|
||||
_mappingMatcher = Guard.NotNull(mappingMatcher);
|
||||
_logEntryMapper = new LogEntryMapper(options);
|
||||
_guidUtils = Guard.NotNull(guidUtils);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -170,7 +188,7 @@ namespace WireMock.Owin
|
||||
{
|
||||
var log = new LogEntry
|
||||
{
|
||||
Guid = Guid.NewGuid(),
|
||||
Guid = _guidUtils.NewGuid(),
|
||||
RequestMessage = request,
|
||||
ResponseMessage = response,
|
||||
|
||||
@@ -187,10 +205,10 @@ namespace WireMock.Owin
|
||||
|
||||
try
|
||||
{
|
||||
if (_options.SaveUnmatchedRequests == true && result.Match?.RequestMatchResult.IsPerfectMatch != true)
|
||||
if (_options.SaveUnmatchedRequests == true && result.Match?.RequestMatchResult is not { IsPerfectMatch: true })
|
||||
{
|
||||
var filename = $"{log.Guid}.LogEntry.json";
|
||||
_options.FileSystemHandler?.WriteUnmatchedRequest(filename, Util.JsonUtils.Serialize(log));
|
||||
_options.FileSystemHandler?.WriteUnmatchedRequest(filename, JsonUtils.Serialize(log));
|
||||
}
|
||||
}
|
||||
catch
|
||||
|
||||
@@ -4,11 +4,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Newtonsoft.Json;
|
||||
#if USE_ASPNETCORE
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
#endif
|
||||
using Stef.Validation;
|
||||
using WireMock.Http;
|
||||
using WireMock.Models;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Types;
|
||||
@@ -80,6 +80,7 @@ public class RequestMessage : IRequestMessage
|
||||
|
||||
#if MIMEKIT
|
||||
/// <inheritdoc />
|
||||
[JsonIgnore] // Issue 1001
|
||||
public object? BodyAsMimeMessage { get; }
|
||||
#endif
|
||||
|
||||
@@ -112,7 +113,7 @@ public class RequestMessage : IRequestMessage
|
||||
/// <summary>
|
||||
/// Used for Unit Testing
|
||||
/// </summary>
|
||||
public RequestMessage(
|
||||
internal RequestMessage(
|
||||
UrlDetails urlDetails,
|
||||
string method,
|
||||
string clientIP,
|
||||
@@ -122,19 +123,6 @@ public class RequestMessage : IRequestMessage
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">The<seealso cref="IWireMockMiddlewareOptions"/>.</param>
|
||||
/// <param name="urlDetails">The original url details.</param>
|
||||
/// <param name="method">The HTTP method.</param>
|
||||
/// <param name="clientIP">The client IP Address.</param>
|
||||
/// <param name="bodyData">The BodyData.</param>
|
||||
/// <param name="headers">The headers.</param>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
#if USE_ASPNETCORE
|
||||
/// <param name="clientCertificate">The client certificate</param>
|
||||
#endif
|
||||
internal RequestMessage(
|
||||
IWireMockMiddlewareOptions? options,
|
||||
UrlDetails urlDetails, string method,
|
||||
|
||||
@@ -17,7 +17,10 @@ internal static class MimeKitUtils
|
||||
{
|
||||
Guard.NotNull(requestMessage);
|
||||
|
||||
if (requestMessage.BodyData != null && requestMessage.Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentTypeHeader) == true && contentTypeHeader.Any())
|
||||
if (requestMessage.BodyData != null &&
|
||||
requestMessage.Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentTypeHeader) == true &&
|
||||
StartsWithMultiPart(contentTypeHeader) // Only parse when "multipart/mixed"
|
||||
)
|
||||
{
|
||||
var bytes = requestMessage.BodyData?.DetectedBodyType switch
|
||||
{
|
||||
@@ -40,6 +43,11 @@ internal static class MimeKitUtils
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool StartsWithMultiPart(WireMockList<string> contentTypeHeader)
|
||||
{
|
||||
return contentTypeHeader.Any(ct => ct.TrimStart().StartsWith("multipart/", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static byte[] FixBytes(byte[] bytes, WireMockList<string> contentType)
|
||||
{
|
||||
var contentTypeBytes = Encoding.UTF8.GetBytes($"{HttpKnownHeaderNames.ContentType}: {contentType}\r\n\r\n");
|
||||
|
||||
Reference in New Issue
Block a user