-
Fix matching of indexed arrays nested inside arrays of hashes (#1144)
Previously, WebMock read indexed arrays nested inside an array of hashes, such as
a[][b][0][c]=1, as hashes with"0","1"keys. WebMock writes that form itself, sowith(query: {"a" => [{"b" => [{"c" => "1"}]}]})with a regexp URI did not match?a[][b][][c]=1, and a Typhoeus request with that hash as its body did not match a stub with the same hash.If a stub uses the old shape, such as
{"a" => [{"b" => {"0" => {"c" => "1"}}}]}, for example one copied from WebMock's suggested stub, use arrays instead.Thanks to Hamza Tarar
-
Fix matching of arrays of hashes that repeat a nested key
Previously, WebMock put a nested key repeated under
[]into the first hash of the array, soitems[][address][city]=X&items[][address][city]=Ywas read as one item with the cityY. A stub listing both items did not match, and a stub forXandYalso matched a request forZandY. Rails and Faraday encode arrays of nested hashes this way.Such keys now go into a new hash, as in Rack. If a stub lists such an array as one item, for example one copied from WebMock's suggested stub, list every item.
Queries that mix a value and nested keys under the same name, such as
a[][b]=1&a[][b][c]=2ora[c]=1&a[c][]=2, no longer raiseIndexErroror join the values into one string. -
Apply Net::HTTP
response_body_encodingto stubbed response bodies (#1138)Previously, stubbed Net::HTTP responses ignored
Net::HTTP#response_body_encoding=andNet::HTTPResponse#body_encoding=. A body stubbed as binary, for example from a raw response file, stayedASCII-8BITeven withhttp.response_body_encoding = trueand aContent-Typeoftext/plain; charset=utf-8.Stubbed bodies now get the encoding real responses get: with
true, the charset from theContent-Typeheader or an HTML<meta>tag, and otherwise the encoding the setting names. Without the setting, a body keeps the encoding it was stubbed with.Thanks to Yang Bai
-
Give each stubbed Net::HTTP response its own copy of the body
Previously, a stubbed Net::HTTP response returned the stubbed body string itself. A body stubbed with a frozen string, such as a literal in a file with
# frozen_string_literal: true, maderesponse.body.force_encoding("UTF-8")raiseFrozenError, which a real response never does. Changing the body in place, for example withresponse.body << "!", changed it for later requests to the same stub too. -
Keep the size of async-http responses to HEAD requests (#1143)
async-http gives the size of a response to a HEAD request as
response.body.length, not as a header. Previously, a response stubbed withstub_request(:head, url).to_return(headers: {"Content-Length" => "42"})ignored theContent-Length: its body wasnilover HTTP/1 and had a length of 0 over HTTP/2. A real response lost its size, with anilbody, when aWebMock.after_requestcallback was registered, for example while VCR was recording, and the recorded response had noContent-Length.A stubbed response now takes its size from the stubbed
Content-Length, or from the stubbed body without one, as before. A real response keeps its size when a callback is registered, and the recorded response has it as itsContent-Length.Thanks to Andrew K. Fader
-
Keep the URI query of HTTPClient requests that pass a query argument (#1145)
Previously, WebMock dropped the query in the URI when an HTTPClient request also passed the query argument.
HTTPClient.new.get("http://example.com/?a=1", {"b" => "2"})requests/?a=1&b=2, but WebMock saw/?b=2, and with{}as the query argument it saw no query at all. A stub ora_requestexpectation could match a URL that was never requested.If a stub or expectation for such a request leaves out the query in the URI, add it, for example
stub_request(:get, "http://example.com/?a=1&b=2"). Record VCR cassettes with such requests again.Thanks to AmeenKhan12345