Add PartialMatch to logging / logentries (#482)

* .

* FluentAssertions

* .

* .
This commit is contained in:
Stef Heyenrath
2020-07-04 11:39:50 +02:00
committed by GitHub
parent d8c708e97c
commit c484b48c35
14 changed files with 502 additions and 250 deletions

View File

@@ -69,6 +69,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.OpenApiParser"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.OpenApiParser.ConsoleApp", "examples\WireMock.Net.OpenApiParser.ConsoleApp\WireMock.Net.OpenApiParser.ConsoleApp.csproj", "{5C09FB93-1535-4F92-AF26-21E8A061EE4A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.OpenApiParser.ConsoleApp", "examples\WireMock.Net.OpenApiParser.ConsoleApp\WireMock.Net.OpenApiParser.ConsoleApp.csproj", "{5C09FB93-1535-4F92-AF26-21E8A061EE4A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.FluentAssertions", "src\WireMock.Net.FluentAssertions\WireMock.Net.FluentAssertions.csproj", "{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -167,6 +169,10 @@ Global
{5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.Build.0 = Release|Any CPU {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.Build.0 = Release|Any CPU
{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -195,6 +201,7 @@ Global
{02082E34-DEF2-47D0-AF0B-3326FAA908CE} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {02082E34-DEF2-47D0-AF0B-3326FAA908CE} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
{D3804228-91F4-4502-9595-39584E5AADAD} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} {D3804228-91F4-4502-9595-39584E5AADAD} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
{5C09FB93-1535-4F92-AF26-21E8A061EE4A} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {5C09FB93-1535-4F92-AF26-21E8A061EE4A} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458} SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}

View File

@@ -36,5 +36,20 @@ namespace WireMock.Admin.Requests
/// The request match result. /// The request match result.
/// </summary> /// </summary>
public LogRequestMatchModel RequestMatchResult { get; set; } public LogRequestMatchModel RequestMatchResult { get; set; }
/// <summary>
/// The partial mapping unique identifier.
/// </summary>
public Guid? PartialMappingGuid { get; set; }
/// <summary>
/// The partial mapping unique title.
/// </summary>
public string PartialMappingTitle { get; set; }
/// <summary>
/// The partial request match result.
/// </summary>
public LogRequestMatchModel PartialRequestMatchResult { get; set; }
} }
} }

View File

@@ -0,0 +1,22 @@
using WireMock.Server;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions
{
public class WireMockANumberOfCallsAssertions
{
private readonly WireMockServer _server;
private readonly int _callsCount;
public WireMockANumberOfCallsAssertions(WireMockServer server, int callsCount)
{
_server = server;
_callsCount = callsCount;
}
public WireMockAssertions Calls()
{
return new WireMockAssertions(_server, _callsCount);
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
using WireMock.Server;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions
{
public class WireMockAssertions
{
private readonly WireMockServer _instance;
public WireMockAssertions(WireMockServer instance, int? callsCount)
{
_instance = instance;
}
[CustomAssertion]
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _instance.LogEntries.Select(x => x.RequestMessage).ToList())
.ForCondition(requests => requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
absoluteUrl)
.Then
.ForCondition(x => x.Any(y => y.AbsoluteUrl == absoluteUrl))
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but didn't find it among the calls to {1}.",
_ => absoluteUrl, requests => requests.Select(request => request.AbsoluteUrl));
return new AndConstraint<WireMockAssertions>(this);
}
}
}

View File

@@ -0,0 +1,26 @@
using FluentAssertions.Primitives;
using WireMock.Server;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions
{
public class WireMockReceivedAssertions : ReferenceTypeAssertions<WireMockServer, WireMockReceivedAssertions>
{
public WireMockReceivedAssertions(WireMockServer server)
{
Subject = server;
}
public WireMockAssertions HaveReceivedACall()
{
return new WireMockAssertions(Subject, null);
}
public WireMockANumberOfCallsAssertions HaveReceived(int callsCount)
{
return new WireMockANumberOfCallsAssertions(Subject, callsCount);
}
protected override string Identifier => "wiremockserver";
}
}

View File

@@ -0,0 +1,13 @@
using WireMock.Server;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions
{
public static class WireMockExtensions
{
public static WireMockReceivedAssertions Should(this WireMockServer instance)
{
return new WireMockReceivedAssertions(instance);
}
}
}

View File

@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.2.13-preview-01</Version>
<Description>FluentAssertions extensions for WireMock.Net</Description>
<AssemblyTitle>WireMock.Net.FluentAssertions</AssemblyTitle>
<Authors>Mahmoud Ali;Stef Heyenrath</Authors>
<TargetFrameworks>netstandard1.3;netstandard2.0;netstandard2.1;net45</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>WireMock.Net.FluentAssertions</AssemblyName>
<PackageId>WireMock.Net.FluentAssertions</PackageId>
<PackageTags>wiremock;FluentAssertions;UnitTest;Assert;Assertions</PackageTags>
<RootNamespace>WireMock.FluentAssertions</RootNamespace>
<ProjectGuid>{B6269AAC-170A-4346-8B9A-579DED3D9A95}</ProjectGuid>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
<!--<DelaySign>true</DelaySign>-->
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
</ItemGroup>
</Project>

View File

@@ -55,5 +55,29 @@ namespace WireMock.Logging
/// The mapping unique title. /// The mapping unique title.
/// </value> /// </value>
public string MappingTitle { get; set; } public string MappingTitle { get; set; }
/// <summary>
/// Gets or sets the partial mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier.
/// </value>
public Guid? PartialMappingGuid { get; set; }
/// <summary>
/// Gets or sets the partial mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
public string PartialMappingTitle { get; set; }
/// <summary>
/// Gets or sets the partial match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
public RequestMatchResult PartialMatchResult { get; set; }
} }
} }

View File

@@ -2,6 +2,6 @@
{ {
internal interface IMappingMatcher internal interface IMappingMatcher
{ {
MappingMatcherResult FindBestMatch(RequestMessage request); (MappingMatcherResult Match, MappingMatcherResult Partial) FindBestMatch(RequestMessage request);
} }
} }

View File

@@ -16,7 +16,7 @@ namespace WireMock.Owin
_options = options; _options = options;
} }
public MappingMatcherResult FindBestMatch(RequestMessage request) public (MappingMatcherResult Match, MappingMatcherResult Partial) FindBestMatch(RequestMessage request)
{ {
var mappings = new List<MappingMatcherResult>(); var mappings = new List<MappingMatcherResult>();
foreach (var mapping in _options.Mappings.Values) foreach (var mapping in _options.Mappings.Values)
@@ -37,21 +37,24 @@ namespace WireMock.Owin
} }
} }
var partialMappings = mappings
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
.OrderBy(m => m.RequestMatchResult)
.ThenBy(m => m.Mapping.Priority)
.ToList();
var partialMatch = partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0);
if (_options.AllowPartialMapping == true) if (_options.AllowPartialMapping == true)
{ {
var partialMappings = mappings return (partialMatch, partialMatch);
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
.OrderBy(m => m.RequestMatchResult)
.ThenBy(m => m.Mapping.Priority)
.ToList();
return partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0);
} }
return mappings var match = mappings
.Where(m => m.RequestMatchResult.IsPerfectMatch) .Where(m => m.RequestMatchResult.IsPerfectMatch)
.OrderBy(m => m.Mapping.Priority).ThenBy(m => m.RequestMatchResult) .OrderBy(m => m.Mapping.Priority).ThenBy(m => m.RequestMatchResult)
.FirstOrDefault(); .FirstOrDefault();
return (match, partialMatch);
} }
} }
} }

View File

@@ -2,6 +2,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using WireMock.Logging; using WireMock.Logging;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using WireMock.Matchers; using WireMock.Matchers;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Http; using WireMock.Http;
@@ -73,7 +74,7 @@ namespace WireMock.Owin
bool logRequest = false; bool logRequest = false;
ResponseMessage response = null; ResponseMessage response = null;
MappingMatcherResult result = null; (MappingMatcherResult Match, MappingMatcherResult Partial) result = (null, null);
try try
{ {
foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null)) foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
@@ -90,7 +91,7 @@ namespace WireMock.Owin
result = _mappingMatcher.FindBestMatch(request); result = _mappingMatcher.FindBestMatch(request);
var targetMapping = result?.Mapping; var targetMapping = result.Match?.Mapping;
if (targetMapping == null) if (targetMapping == null)
{ {
logRequest = true; logRequest = true;
@@ -128,7 +129,7 @@ namespace WireMock.Owin
} }
catch (Exception ex) catch (Exception ex)
{ {
_options.Logger.Error($"Providing a Response for Mapping '{result?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}"); _options.Logger.Error($"Providing a Response for Mapping '{result.Match?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
response = ResponseMessageBuilder.Create(ex.Message, 500); response = ResponseMessageBuilder.Create(ex.Message, 500);
} }
finally finally
@@ -138,9 +139,14 @@ namespace WireMock.Owin
Guid = Guid.NewGuid(), Guid = Guid.NewGuid(),
RequestMessage = request, RequestMessage = request,
ResponseMessage = response, ResponseMessage = response,
MappingGuid = result?.Mapping?.Guid,
MappingTitle = result?.Mapping?.Title, MappingGuid = result.Match?.Mapping?.Guid,
RequestMatchResult = result?.RequestMatchResult MappingTitle = result.Match?.Mapping?.Title,
RequestMatchResult = result.Match?.RequestMatchResult,
PartialMappingGuid = result.Partial?.Mapping?.Guid,
PartialMappingTitle = result.Partial?.Mapping?.Title,
PartialMatchResult = result.Partial?.RequestMatchResult
}; };
LogRequest(log, logRequest); LogRequest(log, logRequest);

View File

@@ -2,6 +2,7 @@
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Admin.Requests; using WireMock.Admin.Requests;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
using WireMock.Types; using WireMock.Types;
@@ -110,22 +111,37 @@ namespace WireMock.Serialization
return new LogEntryModel return new LogEntryModel
{ {
Guid = logEntry.Guid, Guid = logEntry.Guid,
MappingGuid = logEntry.MappingGuid,
MappingTitle = logEntry.MappingTitle,
Request = logRequestModel, Request = logRequestModel,
Response = logResponseModel, Response = logResponseModel,
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
MappingGuid = logEntry.MappingGuid,
MappingTitle = logEntry.MappingTitle,
RequestMatchResult = Map(logEntry.RequestMatchResult),
PartialMappingGuid = logEntry.PartialMappingGuid,
PartialMappingTitle = logEntry.PartialMappingTitle,
PartialRequestMatchResult = Map(logEntry.PartialMatchResult)
};
}
private static LogRequestMatchModel Map(RequestMatchResult matchResult)
{
if (matchResult == null)
{
return null;
}
return new LogRequestMatchModel
{
IsPerfectMatch = matchResult.IsPerfectMatch,
TotalScore = matchResult.TotalScore,
TotalNumber = matchResult.TotalNumber,
AverageTotalScore = matchResult.AverageTotalScore,
MatchDetails = matchResult.MatchDetails.Select(md => new
{ {
IsPerfectMatch = logEntry.RequestMatchResult.IsPerfectMatch, Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty),
TotalScore = logEntry.RequestMatchResult.TotalScore, Score = md.Score
TotalNumber = logEntry.RequestMatchResult.TotalNumber, } as object).ToList()
AverageTotalScore = logEntry.RequestMatchResult.AverageTotalScore,
MatchDetails = logEntry.RequestMatchResult.MatchDetails.Select(md => new
{
Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty),
Score = md.Score
} as object).ToList()
} : null
}; };
} }
} }

View File

@@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using FluentAssertions;
using Moq; using Moq;
using NFluent;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.Models; using WireMock.Models;
@@ -41,8 +41,9 @@ namespace WireMock.Net.Tests.Owin
// Act // Act
var result = _sut.FindBestMatch(request); var result = _sut.FindBestMatch(request);
// Assert and Verify // Assert
Check.That(result).IsNull(); result.Match.Should().BeNull();
result.Partial.Should().BeNull();
} }
[Fact] [Fact]
@@ -62,17 +63,20 @@ namespace WireMock.Net.Tests.Owin
// Act // Act
var result = _sut.FindBestMatch(request); var result = _sut.FindBestMatch(request);
// Assert and Verify // Assert
Check.That(result).IsNull(); result.Match.Should().BeNull();
result.Partial.Should().BeNull();
} }
[Fact] [Fact]
public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsFalse_ShouldReturnExactMatch() public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsFalse_ShouldReturnExactMatch()
{ {
// Assign // Assign
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
var mappings = InitMappings( var mappings = InitMappings(
(Guid.Parse("00000000-0000-0000-0000-000000000001"), new[] { 0.1 }), (guid1, new[] { 0.1 }),
(Guid.Parse("00000000-0000-0000-0000-000000000002"), new[] { 1.0 }) (guid2, new[] { 1.0 })
); );
_optionsMock.Setup(o => o.Mappings).Returns(mappings); _optionsMock.Setup(o => o.Mappings).Returns(mappings);
@@ -81,19 +85,47 @@ namespace WireMock.Net.Tests.Owin
// Act // Act
var result = _sut.FindBestMatch(request); var result = _sut.FindBestMatch(request);
// Assert and Verify // Assert
Check.That(result.Mapping.Guid).IsEqualTo(Guid.Parse("00000000-0000-0000-0000-000000000002")); result.Match.Mapping.Guid.Should().Be(guid2);
Check.That(result.RequestMatchResult.AverageTotalScore).IsEqualTo(1.0); result.Match.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
result.Partial.Mapping.Guid.Should().Be(guid2);
result.Partial.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
}
[Fact]
public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsFalse_AndNoExactmatch_ShouldReturnNullExactMatch_And_PartialMatch()
{
// Assign
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
var mappings = InitMappings(
(guid1, new[] { 0.1 }),
(guid2, new[] { 0.9 })
);
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
// Act
var result = _sut.FindBestMatch(request);
// Assert
result.Match.Should().BeNull();
result.Partial.Mapping.Guid.Should().Be(guid2);
result.Partial.RequestMatchResult.AverageTotalScore.Should().Be(0.9);
} }
[Fact] [Fact]
public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsTrue_ShouldReturnAnyMatch() public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsTrue_ShouldReturnAnyMatch()
{ {
// Assign // Assign
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
_optionsMock.SetupGet(o => o.AllowPartialMapping).Returns(true); _optionsMock.SetupGet(o => o.AllowPartialMapping).Returns(true);
var mappings = InitMappings( var mappings = InitMappings(
(Guid.Parse("00000000-0000-0000-0000-000000000001"), new[] { 0.1 }), (guid1, new[] { 0.1 }),
(Guid.Parse("00000000-0000-0000-0000-000000000002"), new[] { 0.9 }) (guid2, new[] { 0.9 })
); );
_optionsMock.Setup(o => o.Mappings).Returns(mappings); _optionsMock.Setup(o => o.Mappings).Returns(mappings);
@@ -102,18 +134,22 @@ namespace WireMock.Net.Tests.Owin
// Act // Act
var result = _sut.FindBestMatch(request); var result = _sut.FindBestMatch(request);
// Assert and Verify // Assert
Check.That(result.Mapping.Guid).IsEqualTo(Guid.Parse("00000000-0000-0000-0000-000000000002")); result.Match.Mapping.Guid.Should().Be(guid2);
Check.That(result.RequestMatchResult.AverageTotalScore).IsEqualTo(0.9); result.Match.RequestMatchResult.AverageTotalScore.Should().Be(0.9);
result.Partial.Mapping.Guid.Should().Be(guid2);
result.Partial.RequestMatchResult.AverageTotalScore.Should().Be(0.9);
} }
[Fact] [Fact]
public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsFalse_And_WithSameAverageScoreButMoreMatchers_ReturnsMatchWithMoreMatchers() public void MappingMatcher_FindBestMatch_WhenAllowPartialMappingIsFalse_And_WithSameAverageScoreButMoreMatchers_ReturnsMatchWithMoreMatchers()
{ {
// Assign // Assign
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
var mappings = InitMappings( var mappings = InitMappings(
(Guid.Parse("00000000-0000-0000-0000-000000000001"), new[] { 1.0 }), (guid1, new[] { 1.0 }),
(Guid.Parse("00000000-0000-0000-0000-000000000002"), new[] { 1.0, 1.0 }) (guid2, new[] { 1.0, 1.0 })
); );
_optionsMock.Setup(o => o.Mappings).Returns(mappings); _optionsMock.Setup(o => o.Mappings).Returns(mappings);
@@ -123,8 +159,10 @@ namespace WireMock.Net.Tests.Owin
var result = _sut.FindBestMatch(request); var result = _sut.FindBestMatch(request);
// Assert and Verify // Assert and Verify
Check.That(result.Mapping.Guid).IsEqualTo(Guid.Parse("00000000-0000-0000-0000-000000000002")); result.Match.Mapping.Guid.Should().Be(guid2);
Check.That(result.RequestMatchResult.AverageTotalScore).IsEqualTo(1.0); result.Match.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
result.Partial.Mapping.Guid.Should().Be(guid2);
result.Partial.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
} }
private ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores)[] matches) private ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores)[] matches)

View File

@@ -60,7 +60,7 @@ namespace WireMock.Net.Tests.Owin
_matcherMock = new Mock<IMappingMatcher>(); _matcherMock = new Mock<IMappingMatcher>();
_matcherMock.SetupAllProperties(); _matcherMock.SetupAllProperties();
_matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns(new MappingMatcherResult()); _matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns((new MappingMatcherResult(), new MappingMatcherResult()));
_contextMock = new Mock<IContext>(); _contextMock = new Mock<IContext>();
@@ -78,7 +78,7 @@ namespace WireMock.Net.Tests.Owin
// Assert and Verify // Assert and Verify
_optionsMock.Verify(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once); _optionsMock.Verify(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
Expression<Func<ResponseMessage, bool>> match = r => (int) r.StatusCode == 404 && ((StatusModel)r.BodyData.BodyAsJson).Status == "No matching mapping found"; Expression<Func<ResponseMessage, bool>> match = r => (int)r.StatusCode == 404 && ((StatusModel)r.BodyData.BodyAsJson).Status == "No matching mapping found";
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once); _responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
} }
@@ -91,7 +91,9 @@ namespace WireMock.Net.Tests.Owin
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher()); _optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true); _mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
_matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns(new MappingMatcherResult { Mapping = _mappingMock.Object });
var result = new MappingMatcherResult { Mapping = _mappingMock.Object };
_matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns((result, result));
// Act // Act
await _sut.Invoke(_contextMock.Object); await _sut.Invoke(_contextMock.Object);
@@ -99,7 +101,7 @@ namespace WireMock.Net.Tests.Owin
// Assert and Verify // Assert and Verify
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once); _optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
Expression<Func<ResponseMessage, bool>> match = r => (int) r.StatusCode == 401; Expression<Func<ResponseMessage, bool>> match = r => (int)r.StatusCode == 401;
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once); _responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
} }
@@ -112,7 +114,9 @@ namespace WireMock.Net.Tests.Owin
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher()); _optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true); _mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
_matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns(new MappingMatcherResult { Mapping = _mappingMock.Object });
var result = new MappingMatcherResult { Mapping = _mappingMock.Object };
_matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns((result, result));
// Act // Act
await _sut.Invoke(_contextMock.Object); await _sut.Invoke(_contextMock.Object);
@@ -120,7 +124,7 @@ namespace WireMock.Net.Tests.Owin
// Assert and Verify // Assert and Verify
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once); _optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
Expression<Func<ResponseMessage, bool>> match = r => (int) r.StatusCode == 401; Expression<Func<ResponseMessage, bool>> match = r => (int)r.StatusCode == 401;
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once); _responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
} }