Read more

Heads up: network requests `Kernel#open` are not mocked with VCR

Michael Leimstädtner
December 13, 2022Software engineer at makandra GmbH

We usually rely on VCR Show archive.org snapshot and WebMock Show archive.org snapshot to prevent any real network connection when running our unit tests.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This is not entirely true: They are both limited to a set of HTTP libraries listed below (as of 2022). Direct calls to Kernel#open or OpenURI#open_uri are not mocked and will trigger real network requests even in tests. This might bite you e.g. in older versions of CarrierWave Show archive.org snapshot with remote file URLs.

Supported libraries

VCR:

  • Patron (when using WebMock)
  • Curb (when using WebMock -- only supports Curl::Easy at the moment)
  • HTTPClient (when using WebMock)
  • em-http-request (when using WebMock)
  • Net::HTTP (when using WebMock)
  • Typhoeus (Typhoeus::Hydra, but not Typhoeus::Easy or Typhoeus::Multi)
  • Excon
  • Faraday
  • Net::HTTP (inlcudes Mechanize, HTTParty, Rest Client, ..)

WebMock

  • Async::HTTP::Client
  • Curb (currently only Curb::Easy)
  • EM-HTTP-Request
  • Excon
  • HTTPClient
  • HTTP Gem (also known as http.rb)
  • httpx
  • Manticore
  • Net::HTTP and other libraries based on Net::HTTP, e.g.:
  • HTTParty
  • REST Client
  • Patron
  • Typhoeus (currently only Typhoeus::Hydra)
Posted by Michael Leimstädtner to makandra dev (2022-12-13 17:21)