SaveUnmatchedRequests (#703)

This commit is contained in:
Stef Heyenrath
2021-12-24 09:34:26 +01:00
committed by GitHub
parent 3e1c3598f7
commit 1d1ff4a418
18 changed files with 180 additions and 85 deletions
@@ -4,6 +4,7 @@ using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Util;
using JetBrains.Annotations;
#if !USE_ASPNETCORE
using Owin;
#else
@@ -64,5 +65,7 @@ namespace WireMock.Owin
string X509CertificatePassword { get; set; }
bool CustomCertificateDefined { get; }
bool? SaveUnmatchedRequests { get; set; }
}
}
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -23,7 +23,7 @@ namespace WireMock.Owin.Mappers
/// <inheritdoc cref="IOwinRequestMapper.MapAsync"/>
public async Task<RequestMessage> MapAsync(IRequest request, IWireMockMiddlewareOptions options)
{
(UrlDetails urldetails, string clientIP) = ParseRequest(request);
var (urlDetails, clientIP) = ParseRequest(request);
string method = request.Method;
@@ -68,7 +68,7 @@ namespace WireMock.Owin.Mappers
body = await BodyParser.Parse(bodyParserSettings);
}
return new RequestMessage(urldetails, method, clientIP, body, headers, cookies) { DateTime = DateTime.UtcNow };
return new RequestMessage(urlDetails, method, clientIP, body, headers, cookies) { DateTime = DateTime.UtcNow };
}
private (UrlDetails UrlDetails, string ClientIP) ParseRequest(IRequest request)
+27 -25
View File
@@ -2,6 +2,7 @@ using System;
using System.Threading.Tasks;
using WireMock.Logging;
using System.Linq;
using HandlebarsDotNet.Helpers.Utils;
using WireMock.Matchers;
using WireMock.Http;
using WireMock.Owin.Mappers;
@@ -35,28 +36,18 @@ namespace WireMock.Owin
#if !USE_ASPNETCORE
public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwinResponseMapper responseMapper, IMappingMatcher mappingMatcher) : base(next)
{
Check.NotNull(options, nameof(options));
Check.NotNull(requestMapper, nameof(requestMapper));
Check.NotNull(responseMapper, nameof(responseMapper));
Check.NotNull(mappingMatcher, nameof(mappingMatcher));
_options = options;
_requestMapper = requestMapper;
_responseMapper = responseMapper;
_mappingMatcher = mappingMatcher;
_options = Check.NotNull(options, nameof(options));
_requestMapper = Check.NotNull(requestMapper, nameof(requestMapper));
_responseMapper = Check.NotNull(responseMapper, nameof(responseMapper));
_mappingMatcher = Check.NotNull(mappingMatcher, nameof(mappingMatcher));
}
#else
public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwinResponseMapper responseMapper, IMappingMatcher mappingMatcher)
{
Check.NotNull(options, nameof(options));
Check.NotNull(requestMapper, nameof(requestMapper));
Check.NotNull(responseMapper, nameof(responseMapper));
Check.NotNull(mappingMatcher, nameof(mappingMatcher));
_options = options;
_requestMapper = requestMapper;
_responseMapper = responseMapper;
_mappingMatcher = mappingMatcher;
_options = Check.NotNull(options, nameof(options));
_requestMapper = Check.NotNull(requestMapper, nameof(requestMapper));
_responseMapper = Check.NotNull(responseMapper, nameof(responseMapper));
_mappingMatcher = Check.NotNull(mappingMatcher, nameof(mappingMatcher));
}
#endif
@@ -70,20 +61,18 @@ namespace WireMock.Owin
{
lock (_lock)
{
return InvokeInternal(ctx);
return InvokeInternalAsync(ctx);
}
}
else
{
return InvokeInternal(ctx);
}
return InvokeInternalAsync(ctx);
}
private async Task InvokeInternal(IContext ctx)
private async Task InvokeInternalAsync(IContext ctx)
{
var request = await _requestMapper.MapAsync(ctx.Request, _options);
bool logRequest = false;
var logRequest = false;
ResponseMessage response = null;
(MappingMatcherResult Match, MappingMatcherResult Partial) result = (null, null);
try
@@ -185,6 +174,19 @@ namespace WireMock.Owin
LogRequest(log, logRequest);
try
{
if (_options.SaveUnmatchedRequests == true && result.Match?.RequestMatchResult.IsPerfectMatch != true)
{
var filename = $"{log.Guid}.LogEntry.json";
_options.FileSystemHandler?.WriteUnmatchedRequest(filename, Util.JsonUtils.Serialize(log));
}
}
catch
{
// Empty catch
}
await _responseMapper.MapAsync(response, ctx.Response);
}
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using WireMock.Handlers;
using WireMock.Logging;
@@ -78,5 +78,8 @@ namespace WireMock.Owin
public bool CustomCertificateDefined =>
!string.IsNullOrEmpty(X509StoreName) && !string.IsNullOrEmpty(X509StoreLocation) ||
!string.IsNullOrEmpty(X509CertificateFilePath) && !string.IsNullOrEmpty(X509CertificatePassword);
/// <inheritdoc cref="IWireMockMiddlewareOptions.SaveUnmatchedRequests"/>
public bool? SaveUnmatchedRequests { get; set; }
}
}