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

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);
}