Add near-miss diagnostics and request count/unmatched endpoints to the admin API (#1521)

* Add near-miss diagnostics and request count/unmatched admin endpoints (#171)

Add IWireMockServer.FindUnmatchedLogEntries, FindNearMisses and
FindNearMissesForUnmatchedRequests, which rank the mappings that
partially match a request by score.

Expose them through the admin API (POST requests/count,
GET requests/unmatched, GET requests/unmatched/near-misses and
GET requests/{guid}/near-misses), the RestClient and the OpenAPI document.

* Make scenario delay tests independent of server warm-up time

The two *_StateTransitions_BeforeDelayCompletes tests waited a fixed
100 ms for the scenario state to change, which fails on a cold server.
Poll server.Scenarios until the expected state is reached instead.

---------

Co-authored-by: Mattias <13809209+Quattordici@users.noreply.github.com>
This commit is contained in:
Mattias SundströmandMattias authored and GitHub committed 2026-10-23 17:48:54 +02:00
1 parent c94824bd64
commit dc14136492
18 files changed
+1104 -62

No files matched your search

@@ -259,6 +259,96 @@ public class MappingMatcherTests
mappingMock.Verify(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>()), Times.Never);
}
[Fact]
public void MappingMatcher_FindNearMisses_ShouldExcludePerfectMatchesAndMismatches_AndOrderByBestMatchFirst()
{
// Assign
var perfect = Guid.Parse("00000000-0000-0000-0000-000000000001");
var mismatch = Guid.Parse("00000000-0000-0000-0000-000000000002");
var low = Guid.Parse("00000000-0000-0000-0000-000000000003");
var high = Guid.Parse("00000000-0000-0000-0000-000000000004");
var mappings = InitMappings
(
(perfect, [1.0], null),
(mismatch, [0.0], null),
(low, [0.4], null),
(high, [0.8], null)
);
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
// Act
var result = _sut.FindNearMisses(request, 10);
// Assert
result.Select(r => r.Mapping.Guid).Should().Equal(high, low);
}
[Fact]
public void MappingMatcher_FindNearMisses_ShouldRespectMaxResults()
{
// Assign
var low = Guid.Parse("00000000-0000-0000-0000-000000000001");
var high = Guid.Parse("00000000-0000-0000-0000-000000000002");
var mappings = InitMappings
(
(low, [0.4], null),
(high, [0.8], null)
);
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
// Act
var result = _sut.FindNearMisses(request, 1);
// Assert
result.Should().ContainSingle().Which.Mapping.Guid.Should().Be(high);
}
[Fact]
public void MappingMatcher_FindNearMisses_ShouldIgnoreProbability()
{
// Assign
var withProbability = Guid.Parse("00000000-0000-0000-0000-000000000001");
var mappings = InitMappings
(
(withProbability, [0.5], 0.1)
);
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
// Act
var result = _sut.FindNearMisses(request, 3);
// Assert
result.Should().ContainSingle().Which.Mapping.Guid.Should().Be(withProbability);
_randomizerDoubleBetween0And1Mock.Verify(r => r.Generate(), Times.Never);
}
[Fact]
public void MappingMatcher_FindNearMisses_WhenMappingThrowsException_ShouldSkipMapping()
{
// Assign
var mappingMock = new Mock<IMapping>();
mappingMock.SetupGet(m => m.IsDisabled).Returns(false);
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<IRequestMessage>(), It.IsAny<string>())).Throws<Exception>();
var mappings = new ConcurrentDictionary<Guid, IMapping>();
mappings.TryAdd(Guid.NewGuid(), mappingMock.Object);
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
// Act
var result = _sut.FindNearMisses(request, 3);
// Assert
result.Should().BeEmpty();
}
private static ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores, double? probability)[] matches)
{
var mappings = new ConcurrentDictionary<Guid, IMapping>();