webmock: Request with query containing array is incorrectly altered

I have a test that is failing because the expectation can’t be properly verified. The request has a query that contains a value with an indexed array:

"http://example.test:80/call.cgi?street%5B0%5D=1234+SW+1st+Street&street%5B1%5D=Apt+3&title=Dog+Walker"

Basically the parameter street is passed as it follows:

Faraday.get("http://example.test:80/call.cgi", "street[0]" => "foo", "street[1]" => "bar")

However, when the request is stored in the registry, it is altered and the values 0 and 1 in street[X] are stripped and replaced with simply street[].

This is the portion of code where the issue happens:

        if normalized_uri.query_values
          sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: Config.instance.query_values_notation) || {})
          normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: WebMock::Config.instance.query_values_notation)
        end

specifically, the bug is caused by query_to_values and values_to_query:

(byebug) WebMock::Util::QueryMapper.query_to_values(normalized_uri.query)
{"street"=>["1234 SW 1st Street", "Apt 3"], "title"=>"Dog Walker"}
(byebug) WebMock::Util::QueryMapper.values_to_query(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query))
"street%5B%5D=1234%20SW%201st%20Street&street%5B%5D=Apt%203&title=Dog%20Walker"

It should be

(byebug) WebMock::Util::QueryMapper.values_to_query(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query))
"street%5B0%5D=1234%20SW%201st%20Street&street%5B1%5D=Apt%203&title=Dog%20Walker"

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Looks like I am also affected by this issue. My tests are working against Shopify REST API. Particularty this call. In my test I want to verify, that products in the collection were sorted in the right order, but because of this automatic parameter sortnig my tests are faiilng, even though the passed in product ids are in the right order. Any suggestions?