mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-04 06:04:16 +02:00
* feat(request matchers): Add support for early mismatch in mapping processing * test(request matchers): Add unit test for early mismatch functionality * test(grpc): Add test for grpc requests early mismatch and error logging (Issue #1442) * feat(request matchers): RequestMatcherType Add `RequestMatcherType` to request matchers for improved type identification Closes #1442 * refactor(request matchers): Request Replace `EarlyMatcherSelector` with `EarlyMatcherType` for improved clarity and consistency Closes #1442 * feat(request): conversion Add EarlyMatcherType support in request models and mapping conversion Closes #1442 * test(mapping): new tests add unit tests for EarlyMatcherType in mapping conversion and serialization Closes #1442 * refactor(request matchers): RequestMessageEarlyMatcher Replaced inline `EarlyMatcherType` logic with the new `RequestMessageEarlyMatcher` class to support cases when several matchers of the same type are present. For instance - Header, Cookie, Param Closes #1442 * test(request matchers): Early Mismatch add unit tests for early mismatch scenarios with several matchers of same type. Currently, headers and parameters Closes #1442 * refactor(mapping): RequestModel.EarlyMatcherType use fully qualified enum for EarlyMatcherType in serialization Closes #1442 * style(review): fixes - removed unused method - added missing curly brackets Closes #1442
157 lines
6.3 KiB
C#
157 lines
6.3 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using Moq;
|
|
using WireMock.Constants;
|
|
using WireMock.Matchers;
|
|
using WireMock.Matchers.Request;
|
|
using WireMock.Models;
|
|
using WireMock.RequestBuilders;
|
|
|
|
namespace WireMock.Net.Tests.RequestMatchers;
|
|
|
|
public class RequestMessageCompositeMatcherTests
|
|
{
|
|
private class Helper : RequestMessageCompositeMatcher
|
|
{
|
|
public Helper(
|
|
IEnumerable<IRequestMatcher> requestMatchers,
|
|
CompositeMatcherType type = CompositeMatcherType.And,
|
|
RequestMatcherType? earlyMatcherType = null) : base(requestMatchers, type)
|
|
{
|
|
EarlyMatcherType = earlyMatcherType;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_EmptyArray()
|
|
{
|
|
// Assign
|
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
|
|
var matcher = new Helper(Enumerable.Empty<IRequestMatcher>());
|
|
|
|
// Act
|
|
var result = new RequestMatchResult();
|
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
|
|
|
// Assert
|
|
score.Should().Be(0.0d);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_CompositeMatcherType_And()
|
|
{
|
|
// Assign
|
|
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
|
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
|
|
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
|
|
var matcher = new Helper(new[] { requestMatcher1Mock.Object, requestMatcher2Mock.Object });
|
|
|
|
// Act
|
|
var result = new RequestMatchResult();
|
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
|
|
|
// Assert
|
|
score.Should().Be(0.9d);
|
|
|
|
// Verify
|
|
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
|
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_CompositeMatcherType_Or()
|
|
{
|
|
// Assign
|
|
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
|
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
|
|
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
|
|
var matcher = new Helper(new[] { requestMatcher1Mock.Object, requestMatcher2Mock.Object }, CompositeMatcherType.Or);
|
|
|
|
// Act
|
|
var result = new RequestMatchResult();
|
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
|
|
|
// Assert
|
|
score.Should().Be(1.0d);
|
|
|
|
// Verify
|
|
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
|
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_EarlyMismatch()
|
|
{
|
|
// Assign
|
|
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
|
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
|
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
|
var postMatcher = new RequestMessageMethodMatcher(HttpRequestMethod.POST);
|
|
|
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), HttpRequestMethod.GET, "127.0.0.1");
|
|
var matcher = new Helper(
|
|
[requestMatcher1Mock.Object, requestMatcher2Mock.Object, postMatcher],
|
|
earlyMatcherType: RequestMatcherType.Method);
|
|
|
|
// Act
|
|
var result = new RequestMatchResult();
|
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
|
|
|
// Assert
|
|
score.Should().Be(0.0d);
|
|
|
|
// Verify
|
|
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Never);
|
|
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_SeveralHeadersEarlyMismatch()
|
|
{
|
|
// Assign
|
|
var headers = new Dictionary<string, string[]>
|
|
{
|
|
{ "teST", new[] { "x" } },
|
|
{ "teST2", new[] { "z" } }
|
|
};
|
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
|
|
var request = Request.Create()
|
|
.WithEarlyMismatch(RequestMatcherType.Header)
|
|
.UsingAnyMethod()
|
|
.WithHeader("teST", "x")
|
|
.WithHeader("teST1", ["xx", "yy"])
|
|
.WithHeader("teST2", ["y", "z"], matchOperator: MatchOperator.And);
|
|
|
|
// Act
|
|
var score = request.GetMatchingScore(requestMessage, new RequestMatchResult());
|
|
|
|
// Assert
|
|
score.Should().Be(0.0d);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequestMessageCompositeMatcher_GetMatchingScore_SeveralParamEarlyMismatchSuccess()
|
|
{
|
|
// Assign
|
|
var uriWithParams = new Uri("http://localhost?test1=1&test2=2");
|
|
var requestMessage = new RequestMessage(new UrlDetails(uriWithParams), "GET", "127.0.0.1");
|
|
var request = Request.Create()
|
|
.WithEarlyMismatch(RequestMatcherType.Param)
|
|
.UsingAnyMethod()
|
|
.WithParam("test1", "1")
|
|
.WithParam("test2", "2");
|
|
|
|
// Act
|
|
var score = request.GetMatchingScore(requestMessage, new RequestMatchResult());
|
|
|
|
// Assert
|
|
score.Should().Be(1.0d);
|
|
}
|
|
} |