Skip to content

Commit

Permalink
fix: data escaped space (+) was not parsing space char correctly. (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer authored Aug 12, 2023
1 parent 45b8ebe commit a8870d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/MockHttp/Http/DataEscapingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal static IEnumerable<KeyValuePair<string, IEnumerable<string>>> 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<string, string?>(key, value);
})
// Group values for same key.
Expand All @@ -48,6 +48,11 @@ internal static IEnumerable<KeyValuePair<string, IEnumerable<string>>> Parse(str
.ToList();
}

private static string UnescapeData(string v)
{
return Uri.UnescapeDataString(v).Replace('+', ' ');
}

internal static string Format(IEnumerable<KeyValuePair<string, string>> items)
{
return Format(items.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new[] { kvp.Value })));
Expand Down
20 changes: 19 additions & 1 deletion test/MockHttp.Tests/Http/DataEscapingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ public void Given_escapedString_contains_multiple_same_keys_when_parsing_should_
var expected = new Dictionary<string, IEnumerable<string>> { { "key", new[] { "value", "$%^ &*", "another value" } } };

// Act
IEnumerable<KeyValuePair<string, IEnumerable<string>>> actual = DataEscapingHelper.Parse("key=value&key=%24%25%5E%20%26%2A&key=another%20value");
IEnumerable<KeyValuePair<string, IEnumerable<string>>> 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<string, IEnumerable<string>> { { expectedKey, new[] { expectedValue } } };

// Act
IEnumerable<KeyValuePair<string, IEnumerable<string>>> actual = DataEscapingHelper.Parse($"{key}={value}");

// Assert
actual.Should().BeEquivalentTo(expected);
Expand Down

0 comments on commit a8870d7

Please sign in to comment.