Skip to content

Commit ab0825f

Browse files
committed
Fix #1496: WithParam RejectOnMatch inverting the match result
1 parent 7a8582f commit ab0825f

3 files changed

Lines changed: 127 additions & 3 deletions

File tree

‎src/WireMock.Net.Minimal/Matchers/Request/RequestMessageParamMatcher.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright © WireMock.Net
22

3-
using System.Linq;
43
using Stef.Validation;
54
using WireMock.Types;
65

@@ -54,7 +53,10 @@ public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, boo
5453
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
5554
/// <param name="values">The values.</param>
5655
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, bool ignoreCase, params string[]? values) :
57-
this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, ignoreCase, MatchOperator.And, value)).Cast<IStringMatcher>().ToArray())
56+
// Note: the inner ExactMatcher must use AcceptOnMatch.
57+
// The MatchBehaviour (e.g. RejectOnMatch) is applied once by this matcher's GetMatchingScore.
58+
// Passing matchBehaviour here as well would apply the conversion twice and invert RejectOnMatch.
59+
this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(MatchBehaviour.AcceptOnMatch, ignoreCase, MatchOperator.And, value)).Cast<IStringMatcher>().ToArray())
5860
{
5961
}
6062

‎test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,79 @@ public void RequestMessageParamMatcher_With1ParamContainingComma_Using_QueryPara
209209
// Assert
210210
score.Should().Be(1.0);
211211
}
212-
}
213212

213+
[Fact]
214+
public void RequestMessageParamMatcher_RejectOnMatch_WhenValuePresentMatchesPattern_ReturnsMismatch()
215+
{
216+
// Assign: the param value in the URL matches the reject pattern -> the mapping must be rejected (score 0.0).
217+
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=abc"), "GET", "127.0.0.1");
218+
var matcher = new RequestMessageParamMatcher(MatchBehaviour.RejectOnMatch, "key", false, new[] { "abc" });
219+
220+
// Act
221+
var result = new RequestMatchResult();
222+
var score = matcher.GetMatchingScore(requestMessage, result);
223+
224+
// Assert
225+
score.Should().Be(0.0d);
226+
}
227+
228+
[Fact]
229+
public void RequestMessageParamMatcher_RejectOnMatch_WhenValuePresentDoesNotMatchPattern_ReturnsPerfect()
230+
{
231+
// Assign: the param value in the URL does NOT match the reject pattern -> the mapping is accepted (score 1.0).
232+
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=xyz"), "GET", "127.0.0.1");
233+
var matcher = new RequestMessageParamMatcher(MatchBehaviour.RejectOnMatch, "key", false, new[] { "abc" });
234+
235+
// Act
236+
var result = new RequestMatchResult();
237+
var score = matcher.GetMatchingScore(requestMessage, result);
238+
239+
// Assert
240+
score.Should().Be(1.0d);
241+
}
242+
243+
[Fact]
244+
public void RequestMessageParamMatcher_RejectOnMatch_WithIgnoreCase_WhenValueMatchesCaseInsensitively_ReturnsMismatch()
245+
{
246+
// Assign: ignoreCase must still be honored on the inner matcher after the fix.
247+
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=ABC"), "GET", "127.0.0.1");
248+
var matcher = new RequestMessageParamMatcher(MatchBehaviour.RejectOnMatch, "key", true, new[] { "abc" });
249+
250+
// Act
251+
var result = new RequestMatchResult();
252+
var score = matcher.GetMatchingScore(requestMessage, result);
253+
254+
// Assert
255+
score.Should().Be(0.0d);
256+
}
257+
258+
[Fact]
259+
public void RequestMessageParamMatcher_AcceptOnMatch_WhenValuePresentMatchesPattern_ReturnsPerfect()
260+
{
261+
// Assign
262+
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=abc"), "GET", "127.0.0.1");
263+
var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", false, new[] { "abc" });
264+
265+
// Act
266+
var result = new RequestMatchResult();
267+
var score = matcher.GetMatchingScore(requestMessage, result);
268+
269+
// Assert
270+
score.Should().Be(1.0d);
271+
}
272+
273+
[Fact]
274+
public void RequestMessageParamMatcher_AcceptOnMatch_WhenValuePresentDoesNotMatchPattern_ReturnsMismatch()
275+
{
276+
// Assign
277+
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=xyz"), "GET", "127.0.0.1");
278+
var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", false, new[] { "abc" });
279+
280+
// Act
281+
var result = new RequestMatchResult();
282+
var score = matcher.GetMatchingScore(requestMessage, result);
283+
284+
// Assert
285+
score.Should().Be(0.0d);
286+
}
287+
}

‎test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,54 @@ public async Task WireMockServer_WithParam_RejectOnMatch_OnNonMatchingParam_Shou
9393
server.Stop();
9494
}
9595

96+
[Fact]
97+
public async Task WireMockServer_WithParam_RejectOnMatch_WithValue_WhenValueMatches_ShouldNotMatch_Returns404()
98+
{
99+
// Arrange
100+
var cancelationToken = TestContext.Current.CancellationToken;
101+
var server = WireMockServer.Start();
102+
server.Given(
103+
Request.Create()
104+
.WithPath("/x")
105+
.WithParam("k", MatchBehaviour.RejectOnMatch, "abc")
106+
.UsingGet()
107+
)
108+
.ThenRespondWithOK();
109+
110+
// Act
111+
var requestUri = new Uri($"http://localhost:{server.Port}/x?k=abc");
112+
var response = await server.CreateClient().GetAsync(requestUri, cancelationToken);
113+
114+
// Assert
115+
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
116+
117+
server.Stop();
118+
}
119+
120+
[Fact]
121+
public async Task WireMockServer_WithParam_RejectOnMatch_WithValue_WhenValueDoesNotMatch_ShouldMatch_Returns200()
122+
{
123+
// Arrange
124+
var cancelationToken = TestContext.Current.CancellationToken;
125+
var server = WireMockServer.Start();
126+
server.Given(
127+
Request.Create()
128+
.WithPath("/x")
129+
.WithParam("k", MatchBehaviour.RejectOnMatch, "abc")
130+
.UsingGet()
131+
)
132+
.ThenRespondWithOK();
133+
134+
// Act
135+
var requestUri = new Uri($"http://localhost:{server.Port}/x?k=xyz");
136+
var response = await server.CreateClient().GetAsync(requestUri, cancelationToken);
137+
138+
// Assert
139+
response.StatusCode.Should().Be(HttpStatusCode.OK);
140+
141+
server.Stop();
142+
}
143+
96144
[Fact]
97145
public async Task WireMockServer_WithParam_AcceptOnMatch_OnNonMatchingParam_ShouldReturnMappingOk()
98146
{

0 commit comments

Comments
 (0)