gems webmock 3.36.0

45 minutes ago
  • 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, so with(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, so items[][address][city]=X&items[][address][city]=Y was read as one item with the city Y. A stub listing both items did not match, and a stub for X and Y also matched a request for Z and Y. 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]=2 or a[c]=1&a[c][]=2, no longer raise IndexError or join the values into one string.

  • Apply Net::HTTP response_body_encoding to stubbed response bodies (#1138)

    Previously, stubbed Net::HTTP responses ignored Net::HTTP#response_body_encoding= and Net::HTTPResponse#body_encoding=. A body stubbed as binary, for example from a raw response file, stayed ASCII-8BIT even with http.response_body_encoding = true and a Content-Type of text/plain; charset=utf-8.

    Stubbed bodies now get the encoding real responses get: with true, the charset from the Content-Type header 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, made response.body.force_encoding("UTF-8") raise FrozenError, which a real response never does. Changing the body in place, for example with response.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 with stub_request(:head, url).to_return(headers: {"Content-Length" => "42"}) ignored the Content-Length: its body was nil over HTTP/1 and had a length of 0 over HTTP/2. A real response lost its size, with a nil body, when a WebMock.after_request callback was registered, for example while VCR was recording, and the recorded response had no Content-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 its Content-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 or a_request expectation 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

Don't miss a new webmock release

NewReleases is sending notifications on new releases.