From 3523dcbce497583dda37074ce8d82b6bb83cef61 Mon Sep 17 00:00:00 2001 From: skwasjer <11424653+skwasjer@users.noreply.github.com> Date: Sat, 12 Aug 2023 15:51:09 +0200 Subject: [PATCH] fix: data escaped space (+) was not parsing space char correctly. --- src/MockHttp/Http/DataEscapingHelper.cs | 9 +++++++-- .../Http/DataEscapingHelperTests.cs | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/MockHttp/Http/DataEscapingHelper.cs b/src/MockHttp/Http/DataEscapingHelper.cs index e7ed6c40..2564f33a 100644 --- a/src/MockHttp/Http/DataEscapingHelper.cs +++ b/src/MockHttp/Http/DataEscapingHelper.cs @@ -34,8 +34,8 @@ internal static IEnumerable>> Parse(str throw new FormatException("The escaped data string format is invalid."); } - string key = Uri.UnescapeDataString(kvp[0]); - string? value = kvp.Length > 1 ? Uri.UnescapeDataString(kvp[1]) : null; + string key = UnescapeData(kvp[0]); + string? value = kvp.Length > 1 ? UnescapeData(kvp[1]) : null; return new KeyValuePair(key, value); }) // Group values for same key. @@ -48,6 +48,11 @@ internal static IEnumerable>> Parse(str .ToList(); } + private static string UnescapeData(string v) + { + return Uri.UnescapeDataString(v).Replace('+', ' '); + } + internal static string Format(IEnumerable> items) { return Format(items.Select(kvp => new KeyValuePair>(kvp.Key, new[] { kvp.Value }))); diff --git a/test/MockHttp.Tests/Http/DataEscapingHelperTests.cs b/test/MockHttp.Tests/Http/DataEscapingHelperTests.cs index 601cb46d..a15bf9ab 100644 --- a/test/MockHttp.Tests/Http/DataEscapingHelperTests.cs +++ b/test/MockHttp.Tests/Http/DataEscapingHelperTests.cs @@ -44,7 +44,25 @@ public void Given_escapedString_contains_multiple_same_keys_when_parsing_should_ var expected = new Dictionary> { { "key", new[] { "value", "$%^ &*", "another value" } } }; // Act - IEnumerable>> actual = DataEscapingHelper.Parse("key=value&key=%24%25%5E%20%26%2A&key=another%20value"); + IEnumerable>> actual = DataEscapingHelper.Parse("key=value&key=%24%25%5E+%26%2A&key=another%20value"); + + // Assert + actual.Should().BeEquivalentTo(expected); + } + + [Theory] + [InlineData("key", "key", "a%20b", "a b")] + [InlineData("key", "key", "a+b", "a b")] + [InlineData("key", "key", "a b", "a b")] + [InlineData("a%20b", "a b", "value", "value")] + [InlineData("a+b", "a b", "value", "value")] + [InlineData("a b", "a b", "value", "value")] + public void Given_escapedString_contains_space_when_parsing_it_should_return_expected(string key, string expectedKey, string value, string expectedValue) + { + var expected = new Dictionary> { { expectedKey, new[] { expectedValue } } }; + + // Act + IEnumerable>> actual = DataEscapingHelper.Parse($"{key}={value}"); // Assert actual.Should().BeEquivalentTo(expected);