Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data escaped space (+) was not parsing into space char. #68

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading