You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using MockHttpMessageHandler.WithFormData() to match form data that use spaces, matching does not work correctly because + sign in the data is not unescaped correctly to space.
var data = new Dictionary<string, string>() { {"param", "Parameter with space" } };
var dataForHttpClient = new FormUrlEncodedContent(data);
// query string produced by FormUrlEncodedContent.Encode():
// param=Parameter+with+space
var httpMock = new MockHttpMessageHandler();
httpMock.Expect(HttpMethod.Post, "https://server.org/api/command")
.WithFormData(data);
// will throw exception because FormDataMatcher will not convert + to back to space
httpMock.VerifyNoOutstandingExpectation();
// FormUrlEncodedContent from HttpClient library
public class FormUrlEncodedContent : ByteArrayContent
{
private static string Encode(string data)
{
if (string.IsNullOrEmpty(data))
return string.Empty;
return Uri.EscapeDataString(data).Replace("%20", "+");
}
}
FormDataMatcher is using QueryStringMatcher.ParseQueryString() to decode posted data. That method uses only Uri.UnescapeDataString() which does not follow URL spec and does not convert + back to space.
When using
MockHttpMessageHandler.WithFormData()
to match form data that use spaces, matching does not work correctly because+
sign in the data is not unescaped correctly to space.FormDataMatcher
is usingQueryStringMatcher.ParseQueryString()
to decode posted data. That method uses onlyUri.UnescapeDataString()
which does not follow URL spec and does not convert+
back to space.More info on URLs and URIs in .NET: Don't use .NET System.Uri.UnescapeDataString in URL Decoding
The text was updated successfully, but these errors were encountered: