From 83d71bb24e30bbee0e360f18ec59b20b020bbbe2 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 11 Mar 2018 12:18:27 +0100 Subject: [PATCH] More fixes for #106 --- .../Request/RequestMessageParamMatcher.cs | 20 +++++++++++-- .../RequestBuilders/IParamsRequestBuilder.cs | 13 ++++++-- src/WireMock.Net/RequestBuilders/Request.cs | 9 ++++++ src/WireMock.Net/RequestMessage.cs | 2 +- .../RequestMessageParamMatcherTests.cs | 30 +++++++++++++++++++ 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs index 1564ef32..604f0880 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs @@ -27,6 +27,14 @@ namespace WireMock.Matchers.Request /// 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 @@ namespace WireMock.Matchers.Request } var values = requestMessage.GetParameter(Key); - if (values == null && !Values.Any()) + if (values == null) { - // Key is present, but no values, just return match + // Key is not present, just return Mismatch + return MatchScores.Mismatch; + } + + if (values.Count == 0 && (Values == null || !Values.Any())) + { + // 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 590eefa8..3b1799aa 100644 --- a/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs @@ -6,12 +6,19 @@ using WireMock.Util; 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 @@ namespace WireMock.RequestBuilders 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 8d527935..f26e7ce7 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -291,6 +291,15 @@ namespace WireMock.RequestBuilders 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 8540c533..990e03ba 100644 --- a/src/WireMock.Net/RequestMessage.cs +++ b/src/WireMock.Net/RequestMessage.cs @@ -206,7 +206,7 @@ namespace WireMock /// /// 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 c4879ea8..87560baf 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs @@ -51,5 +51,35 @@ namespace WireMock.Net.Tests.RequestMatchers // 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