From 7e4918b86d28a03719a72abe6c0524282525fd7e Mon Sep 17 00:00:00 2001 From: Kyle McDowell Date: Wed, 13 Dec 2023 17:08:17 -0500 Subject: [PATCH 1/2] Handle empty query string in QueryStringMatcher Added a condition check for empty input in the ParseQueryString method of the QueryStringMatcher class. Now it returns an empty Enumerable instead of throwing an error, improving error handling. --- RichardSzalay.MockHttp/Matchers/QueryStringMatcher.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/RichardSzalay.MockHttp/Matchers/QueryStringMatcher.cs b/RichardSzalay.MockHttp/Matchers/QueryStringMatcher.cs index 70c3d2d..b80c812 100644 --- a/RichardSzalay.MockHttp/Matchers/QueryStringMatcher.cs +++ b/RichardSzalay.MockHttp/Matchers/QueryStringMatcher.cs @@ -69,7 +69,12 @@ public bool Matches(System.Net.Http.HttpRequestMessage message) internal static IEnumerable> ParseQueryString(string input) { - return input.TrimStart('?').Split('&') + if (input.Length == 0) + { + return Enumerable.Empty>(); + } + return input.TrimStart('?') + .Split('&') .Select(pair => StringUtil.Split(pair, '=', 2)) .Select(pair => new KeyValuePair( UrlDecode(pair[0]), From 6385789cfdabd0922d976d0abc935862131f3269 Mon Sep 17 00:00:00 2001 From: Kyle McDowell Date: Wed, 13 Dec 2023 17:08:32 -0500 Subject: [PATCH 2/2] Add test for matching with empty dictionary data --- .../Matchers/QueryStringMatcherTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/RichardSzalay.MockHttp.Tests/Matchers/QueryStringMatcherTests.cs b/RichardSzalay.MockHttp.Tests/Matchers/QueryStringMatcherTests.cs index c8b9afa..c2b1710 100644 --- a/RichardSzalay.MockHttp.Tests/Matchers/QueryStringMatcherTests.cs +++ b/RichardSzalay.MockHttp.Tests/Matchers/QueryStringMatcherTests.cs @@ -111,6 +111,18 @@ public void Should_support_matching_dictionary_data_with_url_encoded_values() Assert.True(actualMatch, "QueryStringMatcher.Matches() should match dictionary data with URL encoded query string values."); } + [Fact] + public void Should_support_matching_with_empty_dictionary_data_when_exact_is_true() + { + var data = new Dictionary(); + + var sut = new QueryStringMatcher(data, true); + + var actualMatch = sut.Matches(new HttpRequestMessage(HttpMethod.Get, "http://tempuri.org/home")); + + Assert.True(actualMatch, "QueryStringMatcher.Matches() should match empty dictionary data."); + } + private bool Test(string expected, string actual, bool exact = false) { var sut = new QueryStringMatcher(expected, exact);