Repository navigation
-
Allow or disable real requests only within a block (#1110)
WebMock.allow_net_connect!andWebMock.disable_net_connect!now take a block, for exampleWebMock.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})sentContent-Type: application/x-www-form-urlencoded, but WebMock recorded the request without it. A stub withwith(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 withnobody: truehave none either.Callbacks,
WebMock.executed_requestsand 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
withblock whenwithis called again with options onlyPreviously,
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 laterwithcall already kept the options it didn't give, such asbody:, and now keeps the block too. The same applies toa_request.A stub or request expectation that matched only because the block was dropped no longer matches. Calling
withagain 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 Integer2. A plain hash had the same problem when the stub's URI was a regexp, a template or a lambda, as instub_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,
trueandfalsein query patterns as strings, including inside nestedhash_includingandhash_excludingmatchers.This also makes
hash_excludingwork 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_includingandhash_excludingwith 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 nestedhash_excludingwith symbol keys never excluded anything, sowith(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_includingandarray_excluding. A test that passed only because a nestedhash_excludingexcluded nothing will now fail. -
Fix
hash_includingandhash_excludingnested in a query hashPreviously,
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, wherehash_includingandhash_excludingare 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 asnet_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 matchhttps://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"})raisedArgumentError: invalid byte sequence in UTF-8when 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
ArgumentErrorinstead of WebMock's errorPreviously, when a request with a URL encoded body matched no stub, WebMock could raise
ArgumentErrorwhile it built the suggested stub for its error message. This happened when the body was binary data, or when it repeated a key, as ina=1&a=2, withquery_values_notationset to:flat.WebMock now raises its usual error, and the suggested stub shows such a body as a string.
-
Fix Net::HTTP
OPTIONS *requests raisingAddressable::URI::InvalidURIError(#982)Previously,
http.request(Net::HTTP::Options.new("*"))raisedInvalid 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 asstub_request(:options, "www.example.com/*"). Real requests still sendOPTIONS *. Stubs andassert_requestedcan't tell such a request from anOPTIONSrequest for the path/*. -
Apply a changed
query_values_notationto URLs that WebMock has already seenPreviously, 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=2still 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_arrayPreviously, 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, andwith(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=2from?a=2&a=1. -
Fix URLs that repeat a query key without a value raising with
:flat_arrayPreviously, with
:flat_array, any request for a URL such as?a&a=2raisedArgumentError. It now matcheswith(query: "a&a=2"), but notwith(query: "a=&a=2"), since the key has no value rather than an empty one.