Skip to content

3.38.0

Latest

Choose a tag to compare

@bblimke bblimke released this 21 Nov 23:08
  • Allow or disable real requests only within a block (#1110)

    WebMock.allow_net_connect! and WebMock.disable_net_connect! now take a block, for example WebMock.disable_net_connect!(allow: "api.example.com") { ... }. The settings apply only while the block runs. When it ends, even with an exception, the settings from before the call come back.

    Previously, either method ignored a block. In WebMock.allow_net_connect! { Net::HTTP.get(uri) }, the request never ran, and real requests stayed allowed until the settings changed again. Code in such a block now runs.

    The settings are global, so other threads see them while the block runs. Don't run such blocks in several threads at once, because they can restore each other's settings in the wrong order.

    Thanks to Yegor Bugayenko for the suggestion

  • Record the form Content-Type of Typhoeus requests with a hash body (#463)

    Previously, Typhoeus.post(url, body: {page: 2}) sent Content-Type: application/x-www-form-urlencoded, but WebMock recorded the request without it. A stub with with(body: {page: 2}) didn't match, and neither did a stub that required that header.

    WebMock now records the header whenever Typhoeus sends it for a hash body: for a POST, even with an empty hash, and for any other method except PUT and HEAD, unless the form is empty.

    A request that sets its own Content-Type keeps it. Multipart forms, such as a hash with a file or a request with multipart: true, still have no Content-Type recorded, except an empty POST, which Typhoeus sends URL encoded even then. Requests with nobody: true have none either.

    Callbacks, WebMock.executed_requests and VCR see the header too, and the stub suggested for an unmatched request includes it and shows a form body as a hash. Update assertions that compare all the headers of such a request. If VCR, hooked into WebMock, matches requests on their headers, record the affected cassettes again.

  • Keep a with block when with is called again with options only

    Previously, stub_request(:post, url).with { |request| request.body == "yes" }.with(headers: {"X-A" => "1"}) dropped the block, so the stub also matched a request with that header and the body "no". A later with call already kept the options it didn't give, such as body:, and now keeps the block too. The same applies to a_request.

    A stub or request expectation that matched only because the block was dropped no longer matches. Calling with again with a block, or with an option given before, still replaces it.

  • Match numbers and boolean values in query patterns (#371)

    Previously, with(query: hash_including(page: 2)) never matched ?page=2, because query values in a request are strings and WebMock compared them with the Integer 2. A plain hash had the same problem when the stub's URI was a regexp, a template or a lambda, as in stub_request(:get, /example/).with(query: {page: 2}). With a string URI, an array of numbers such as {tag: [2, 10]} didn't match ?tag[]=2&tag[]=10.

    WebMock now compares numbers, true and false in query patterns as strings, including inside nested hash_including and hash_excluding matchers.

    This also makes hash_excluding work with such values. Previously, with(query: hash_excluding(page: 2)) matched ?page=2. A test that passed only because of this will now fail.

  • Fix nested hash_including and hash_excluding with symbol keys in RSpec (#900)

    Previously, in RSpec, with(body: {user: hash_including(role: "admin")}) never matched the JSON body {"user":{"role":"admin"}}. WebMock converted the keys of the outer hash to strings, but not those of a matcher inside it, and the parsed body has string keys. For the same reason, a nested hash_excluding with symbol keys never excluded anything, so with(body: {user: hash_excluding(role: "admin")}) matched that body too.

    The same applied to query patterns: with(query: hash_including(filter: hash_excluding(role: "admin"))) matched ?filter[role]=admin.

    Nested matchers now get string keys too, as do hashes inside RSpec's array_including and array_excluding. A test that passed only because a nested hash_excluding excluded nothing will now fail.

  • Fix hash_including and hash_excluding nested in a query hash

    Previously, stub_request(:get, "www.example.com").with(query: {filter: hash_including("role" => "admin")}) never matched ?filter[role]=admin&filter[active]=true, because WebMock wrote the matcher into the stub's URL as text. With a regexp, a template or a lambda URI, such a stub never matched in RSpec, where hash_including and hash_excluding are RSpec's own matchers.

    Such stubs now match with every kind of URI.

  • Ignore letter case in allowed host names (#819)

    Previously, WebMock.disable_net_connect!(allow: "Anachronic.local") blocked requests to that host, because WebMock compared the allowed name with the request's host, which it writes in lowercase. The same applied to a host with a port, such as "Anachronic.local:8080", and to host names given as net_http_connect_on_start.

    Such requests are now allowed and reach the real server. Host names are not case sensitive, and allowed URLs have ignored the letter case of the host since 3.30.0.

  • Fix URI templates not matching encoded characters in query values (#468)

    Previously, stub_request(:get, Addressable::Template.new("https://example.com/test{?q}")) didn't match https://example.com/test?q=env%3Aproduction, although the template itself matches that URL. The same happened with ?q=env:production, and with other punctuation such as /, @ or =, encoded or not.

    Both now match. As with string URIs, a stub can't tell such an encoded character from the character itself.

  • Fix stubs with a hash body pattern raising on binary request bodies (#333)

    Previously, a stub such as stub_request(:post, url).with(body: {"name" => "x"}) raised ArgumentError: invalid byte sequence in UTF-8 when the request body was binary data, such as an uploaded image. Since WebMock checks the newest stub first, this also broke requests that an older stub would have answered.

    Such a body now doesn't match the hash pattern, so WebMock moves on to the next stub, as it already did for a body that isn't valid JSON.

  • Fix unstubbed requests with some form bodies raising ArgumentError instead of WebMock's error

    Previously, when a request with a URL encoded body matched no stub, WebMock could raise ArgumentError while it built the suggested stub for its error message. This happened when the body was binary data, or when it repeated a key, as in a=1&a=2, with query_values_notation set to :flat.

    WebMock now raises its usual error, and the suggested stub shows such a body as a string.

  • Fix Net::HTTP OPTIONS * requests raising Addressable::URI::InvalidURIError (#982)

    Previously, http.request(Net::HTTP::Options.new("*")) raised Invalid port number, even with real connections allowed, because WebMock could not build a URI from the * request target.

    WebMock now records such a request with the path /*, so stub it as stub_request(:options, "www.example.com/*"). Real requests still send OPTIONS *. Stubs and assert_requested can't tell such a request from an OPTIONS request for the path /*.

  • Apply a changed query_values_notation to URLs that WebMock has already seen

    Previously, after WebMock::Config.instance.query_values_notation = :flat_array, a URL that WebMock had already seen kept the query it got with the earlier notation, so ?a=1&a=2 still became ?a=2. Whether a stub matched depended on which tests ran first.

    WebMock now reads each request's URL with the current notation. A stub keeps the query as WebMock read it when the stub was declared, so set the notation before declaring stubs.

  • Match with(query: "...") pairs in any order with :flat_array

    Previously, with WebMock::Config.instance.query_values_notation = :flat_array, with(query: "b=2&a=1") didn't match a request for ?b=2&a=1, and with(query: "a=2&a=1") didn't match ?a=2&a=1. Only patterns with their pairs in sorted order matched.

    Such patterns now match their pairs in any order, as a query in the stub's URL already did. A stub can't tell ?a=1&a=2 from ?a=2&a=1.

  • Fix URLs that repeat a query key without a value raising with :flat_array

    Previously, with :flat_array, any request for a URL such as ?a&a=2 raised ArgumentError. It now matches with(query: "a&a=2"), but not with(query: "a=&a=2"), since the key has no value rather than an empty one.