Fix RequestMessageParamMatcher : RejectOnMatch (#1006)

This commit is contained in:
Stef Heyenrath
2023-10-09 18:28:22 +02:00
committed by GitHub
parent 6254ee3950
commit bc75db8c8c
3 changed files with 66 additions and 15 deletions

View File

@@ -53,7 +53,7 @@ public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
public JsonMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false)
{
Guard.NotNull(value, nameof(value));
Guard.NotNull(value);
MatchBehaviour = matchBehaviour;
IgnoreCase = ignoreCase;

View File

@@ -85,11 +85,11 @@ public class RequestMessageParamMatcher : IRequestMatcher
/// <inheritdoc />
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
{
var (score, exception) = GetMatchResult(requestMessage).Expand();
return requestMatchResult.AddScore(GetType(), score, exception);
var score = GetMatchScore(requestMessage);
return requestMatchResult.AddScore(GetType(), MatchBehaviourHelper.Convert(MatchBehaviour, score), null);
}
private MatchResult GetMatchResult(IRequestMessage requestMessage)
private double GetMatchScore(IRequestMessage requestMessage)
{
if (Funcs != null)
{
@@ -100,7 +100,7 @@ public class RequestMessageParamMatcher : IRequestMatcher
if (valuesPresentInRequestMessage == null)
{
// Key is not present at all, just return Mismatch
return default;
return MatchScores.Mismatch;
}
if (Matchers != null && Matchers.Any())
@@ -115,10 +115,10 @@ public class RequestMessageParamMatcher : IRequestMatcher
return MatchScores.Perfect;
}
return default;
return MatchScores.Mismatch;
}
private static MatchResult CalculateScore(IReadOnlyList<IStringMatcher> matchers, WireMockList<string> valuesPresentInRequestMessage)
private static double CalculateScore(IReadOnlyList<IStringMatcher> matchers, WireMockList<string> valuesPresentInRequestMessage)
{
var total = new List<double>();

View File

@@ -2,6 +2,7 @@ using System;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
@@ -26,10 +27,10 @@ public partial class WireMockServerTests
};
var server = WireMockServer.Start(settings);
server.Given(
Request.Create()
.UsingGet()
.WithPath("/foo")
.WithParam("query", queryValue)
Request.Create()
.UsingGet()
.WithPath("/foo")
.WithParam("query", queryValue)
)
.RespondWith(
Response.Create().WithStatusCode(200)
@@ -52,10 +53,10 @@ public partial class WireMockServerTests
var queryValue = "1,2,3";
var server = WireMockServer.Start();
server.Given(
Request.Create()
.UsingGet()
.WithPath("/foo")
.WithParam("query", "1", "2", "3")
Request.Create()
.UsingGet()
.WithPath("/foo")
.WithParam("query", "1", "2", "3")
)
.RespondWith(
Response.Create().WithStatusCode(200)
@@ -70,4 +71,54 @@ public partial class WireMockServerTests
server.Stop();
}
[Fact]
public async Task WireMockServer_WithParam_RejectOnMatch_OnNonMatchingParam_ShouldReturnMappingOk()
{
// Arrange
var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/v1/person/workers")
.WithParam("delta_from", MatchBehaviour.RejectOnMatch)
.UsingGet()
)
.RespondWith(
Response.Create()
);
// Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");
var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
server.Stop();
}
[Fact]
public async Task WireMockServer_WithParam_AcceptOnMatch_OnNonMatchingParam_ShouldReturnMappingOk()
{
// Arrange
var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/v1/person/workers")
.WithParam("delta_from")
.UsingGet()
)
.RespondWith(
Response.Create()
);
// Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");
var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
server.Stop();
}
}