diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs index 1564ef32d..604f0880d 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs @@ -27,6 +27,14 @@ public class RequestMessageParamMatcher : IRequestMatcher /// public IEnumerable Values { get; } + /// + /// Initializes a new instance of the class. + /// + /// The key. + public RequestMessageParamMatcher([NotNull] string key) : this(key, null) + { + } + /// /// Initializes a new instance of the class. /// @@ -66,13 +74,19 @@ private double IsMatch(RequestMessage requestMessage) } var values = requestMessage.GetParameter(Key); - if (values == null && !Values.Any()) + if (values == null) + { + // Key is not present, just return Mismatch + return MatchScores.Mismatch; + } + + if (values.Count == 0 && (Values == null || !Values.Any())) { - // Key is present, but no values, just return match + // Key is present, but no values or null, just return Perfect return MatchScores.Perfect; } - var matches = Values.Select(v => values != null && values.Contains(v)); + var matches = Values.Select(v => values.Contains(v)); return MatchScores.ToScore(matches); } } diff --git a/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs index 590eefa80..3b1799aa3 100644 --- a/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs @@ -6,12 +6,19 @@ namespace WireMock.RequestBuilders { /// - /// The ParametersRequestBuilder interface. + /// The ParamsRequestBuilder interface. /// public interface IParamsRequestBuilder { /// - /// The with parameters. + /// WithParam (key only) + /// + /// The key. + /// The . + IRequestBuilder WithParam([NotNull] string key); + + /// + /// WithParam (values) /// /// The key. /// The values. @@ -19,7 +26,7 @@ public interface IParamsRequestBuilder IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values); /// - /// The with parameters. + /// WithParam (funcs) /// /// The funcs. /// The . diff --git a/src/WireMock.Net/RequestBuilders/Request.cs b/src/WireMock.Net/RequestBuilders/Request.cs index 8d527935e..f26e7ce7e 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -291,6 +291,15 @@ public IRequestBuilder WithBody(Func func) return this; } + /// + public IRequestBuilder WithParam(string key) + { + Check.NotNull(key, nameof(key)); + + _requestMatchers.Add(new RequestMessageParamMatcher(key)); + return this; + } + /// public IRequestBuilder WithParam(string key, params string[] values) { diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs index 8540c5330..990e03ba9 100644 --- a/src/WireMock.Net/RequestMessage.cs +++ b/src/WireMock.Net/RequestMessage.cs @@ -206,7 +206,7 @@ private static IDictionary> ParseQuery(string query /// /// The key. /// The query parameter. - public List GetParameter(string key) + public WireMockList GetParameter(string key) { if (Query == null) { diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs index c4879ea8a..87560bafa 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs @@ -51,5 +51,35 @@ public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent() // Assert Check.That(score).IsEqualTo(0.0d); } + + [Fact] + public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithNull() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1"); + var matcher = new RequestMessageParamMatcher("key"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithEmptyArray() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1"); + var matcher = new RequestMessageParamMatcher("key", new string[] { }); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } } } \ No newline at end of file