mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 02:08:29 +02:00
Rewrite some unit-integration-tests to unit-tests (#206)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -8,51 +7,73 @@ using WireMock.Matchers;
|
||||
using WireMock.Util;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Http;
|
||||
using WireMock.Owin.Mappers;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Validation;
|
||||
#if !USE_ASPNETCORE
|
||||
using Microsoft.Owin;
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using OwinMiddleware = Microsoft.Owin.OwinMiddleware;
|
||||
using Next = Microsoft.Owin.OwinMiddleware;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using OwinMiddleware = System.Object;
|
||||
using IContext = Microsoft.AspNetCore.Http.HttpContext;
|
||||
using Next = Microsoft.AspNetCore.Http.RequestDelegate;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Owin
|
||||
{
|
||||
#if !USE_ASPNETCORE
|
||||
internal class WireMockMiddleware : OwinMiddleware
|
||||
#else
|
||||
internal class WireMockMiddleware
|
||||
#endif
|
||||
{
|
||||
private static readonly Task CompletedTask = Task.FromResult(false);
|
||||
private readonly WireMockMiddlewareOptions _options;
|
||||
|
||||
private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
|
||||
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
||||
private readonly IWireMockMiddlewareOptions _options;
|
||||
private readonly IOwinRequestMapper _requestMapper;
|
||||
private readonly IOwinResponseMapper _responseMapper;
|
||||
private readonly IMappingMatcher _mappingMatcher;
|
||||
|
||||
#if !USE_ASPNETCORE
|
||||
public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
|
||||
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;
|
||||
}
|
||||
#else
|
||||
public WireMockMiddleware(RequestDelegate next, WireMockMiddlewareOptions options)
|
||||
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));
|
||||
|
||||
_options = options;
|
||||
_requestMapper = requestMapper;
|
||||
_responseMapper = responseMapper;
|
||||
_mappingMatcher = mappingMatcher;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !USE_ASPNETCORE
|
||||
public override async Task Invoke(IOwinContext ctx)
|
||||
public override Task Invoke(IContext ctx)
|
||||
#else
|
||||
public async Task Invoke(HttpContext ctx)
|
||||
public Task Invoke(IContext ctx)
|
||||
#endif
|
||||
{
|
||||
return InvokeInternal(ctx);
|
||||
}
|
||||
|
||||
private async Task InvokeInternal(IContext ctx)
|
||||
{
|
||||
var request = await _requestMapper.MapAsync(ctx.Request);
|
||||
|
||||
bool logRequest = false;
|
||||
ResponseMessage response = null;
|
||||
Mapping targetMapping = null;
|
||||
RequestMatchResult requestMatchResult = null;
|
||||
(IMapping TargetMapping, RequestMatchResult RequestMatchResult) result = (null, null);
|
||||
try
|
||||
{
|
||||
foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
|
||||
@@ -67,36 +88,8 @@ namespace WireMock.Owin
|
||||
}
|
||||
}
|
||||
|
||||
var mappings = _options.Mappings.Values
|
||||
.Select(m => new
|
||||
{
|
||||
Mapping = m,
|
||||
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey(m.Scenario) ? _options.Scenarios[m.Scenario].NextState : null)
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (_options.AllowPartialMapping)
|
||||
{
|
||||
var partialMappings = mappings
|
||||
.Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdminInterface)
|
||||
.OrderBy(m => m.MatchResult)
|
||||
.ThenBy(m => m.Mapping.Priority)
|
||||
.ToList();
|
||||
|
||||
var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0);
|
||||
|
||||
targetMapping = bestPartialMatch?.Mapping;
|
||||
requestMatchResult = bestPartialMatch?.MatchResult;
|
||||
}
|
||||
else
|
||||
{
|
||||
var perfectMatch = mappings
|
||||
.OrderBy(m => m.Mapping.Priority)
|
||||
.FirstOrDefault(m => m.MatchResult.IsPerfectMatch);
|
||||
|
||||
targetMapping = perfectMatch?.Mapping;
|
||||
requestMatchResult = perfectMatch?.MatchResult;
|
||||
}
|
||||
result = _mappingMatcher.Match(request);
|
||||
var targetMapping = result.TargetMapping;
|
||||
|
||||
if (targetMapping == null)
|
||||
{
|
||||
@@ -145,9 +138,9 @@ namespace WireMock.Owin
|
||||
Guid = Guid.NewGuid(),
|
||||
RequestMessage = request,
|
||||
ResponseMessage = response,
|
||||
MappingGuid = targetMapping?.Guid,
|
||||
MappingTitle = targetMapping?.Title,
|
||||
RequestMatchResult = requestMatchResult
|
||||
MappingGuid = result.TargetMapping?.Guid,
|
||||
MappingTitle = result.TargetMapping?.Title,
|
||||
RequestMatchResult = result.RequestMatchResult
|
||||
};
|
||||
|
||||
LogRequest(log, logRequest);
|
||||
|
||||
Reference in New Issue
Block a user