alexliesenfeld / httpmock

HTTP mocking library for Rust
https://httpmock.rs
MIT License
488 stars 45 forks source link

binary query param converted to lossy string #107

Open iwinux opened 5 months ago

iwinux commented 5 months ago

It seems impossible to assert binary query params encoded by url::form_urlencoded::byte_serialize with when.query_param("key", "value"), because Url::parse (introduced in https://github.com/alexliesenfeld/httpmock/pull/59) converts all query param values into String, while all non-utf8 characters are replaced by U+FFFD REPLACEMENT CHARACTER (see https://github.com/servo/rust-url/issues/219 for details).

A possible workaround might be exposing the raw request URL from HttpMockRequest, which can be checked by other methods without decoding the query params (e.g.: regexp matching).

alexliesenfeld commented 5 months ago

Thanks for this issue.

Yes, URIs are limited to containing characters as per rfc3986, section 2.3, hence the entire URI, including the query, is represented as a string. Is UTF-8 not enough to represent the characters you want to verify or what is your scenario?

As per living standard though, in fact binary content should be possible to represent in the query. So I'd say exposing it in its binary representation in HttpMockRequest could be an option for such cases.

iwinux commented 5 months ago

Non UTF-8 query params are rarely used in modern RESTful APIs. However, the BitTorrent Protocol Specification requires info_hash to be:

The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. This value will almost certainly have to be escaped.

So far this is the only situation where I ever want to access the query param in binary representation :(