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

FormDataMatcher does not decode + into space correctly #5

Closed
jozefizso opened this issue Feb 12, 2015 · 0 comments · Fixed by #6
Closed

FormDataMatcher does not decode + into space correctly #5

jozefizso opened this issue Feb 12, 2015 · 0 comments · Fixed by #6

Comments

@jozefizso
Copy link
Contributor

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.

More info on URLs and URIs in .NET: Don't use .NET System.Uri.UnescapeDataString in URL Decoding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant