alexliesenfeld / httpmock

HTTP mocking library for Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle url encoding in query param match

StarpTech opened this issue · comments

Hi, first of all, thanks for the great library. We found an issue with query_param. According to the documentation, it's not necessary to encode the query value.

Sets a query parameter that needs to be provided.
Attention!: The request query keys and values are implicitly allowed, but is not required to be urlencoded! The value you pass here, however, must be in plain text (i.e. not encoded)!

In order to match, we need to replace spaces with +. I'd expect that this library can handle it for me so I don't need to care about encoding internals.

Reference: https://url.spec.whatwg.org/#url-parsing (spaceAsPlus)

let m = server.mock(|when, then|{
    when.query_param("query", "Metallica is cool".replace(" ", "+"));
    then.status(200);
});

let req = hyper::Request::builder()
    .method(Method::GET)
    .uri(
        Url::parse_with_params(
            "http://example.com/",
            &[
                ("query", "Metallica is cool")
            ],
        )
        .unwrap()
        .to_string(),
    )
    .header("content-type", "application/json")
    .body(Body::empty())
    .unwrap();

Hi @StarpTech, thanks fur submitting this issue.httpmock uses qstring to convert query strings into key/value pairs (see here). It seems that it doesn't handle that situation correctly as it only treats %20 as a space but not +.

The reason seems to be the usage of percent_decode, which in turn only handles "percent" values as per https://url.spec.whatwg.org/#percent-decode.

It seems to be a known open issue in qstring though: algesten/qstring#3. The library seems unmaintained, so it might be a candidate for a replacement in future.

Hi @alexliesenfeld thanks for the detailed response. We could switch to https://github.com/servo/rust-url.

Should be fixed in release v0.6.6.