Skip to content

Commit

Permalink
add vcr adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kehra committed Aug 30, 2024
1 parent 70ba32e commit 2992014
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add VCR adapter. ([@kehra][])

## 1.1.0 (2024-08-12)

- Add support for `ActiveJob::Base.enqueue_after_transaction_commit`. ([@joshuay03][])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Isolator has a bunch of built-in adapters:
- `:mailer`
- `:webmock` – track mocked HTTP requests (unseen by Sniffer) in tests
- `:action_cable`
- `:vcr`
You can dynamically enable/disable adapters, e.g.:
Expand Down
1 change: 1 addition & 0 deletions isolator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "uniform_notifier"
spec.add_development_dependency "webrick"
spec.add_development_dependency "net-smtp"
spec.add_development_dependency "vcr"
end
1 change: 1 addition & 0 deletions lib/isolator/adapters/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

require "isolator/adapters/http/sniffer"

require "isolator/adapters/http/vcr" if defined?(::VCR)
require "isolator/adapters/http/webmock" if defined?(::WebMock)
14 changes: 14 additions & 0 deletions lib/isolator/adapters/http/vcr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

adapter = Isolator.isolate :vcr,
exception_class: Isolator::HTTPError,
details_message: ->(request) {
"#{request.method.to_s.upcase} #{request.uri}"
}

VCR.configure do |c|
c.after_http_request do |request, _response|
# check if we are even notifying before calling `caller`, which is well known to be slow
adapter.notify(caller, request) if adapter.notify?(request)
end
end
35 changes: 35 additions & 0 deletions spec/integrations/fixtures/vcr_cassettes/example.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions spec/isolator/adapters/http/vcr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "spec_helper"

describe "VCR adapter" do
let(:uri) { URI.parse("http://localhost:4567/?lang=ruby&author=matz") }

before do
allow(Isolator).to receive(:within_transaction?) { true }
end

describe "#get" do
specify do
VCR.use_cassette "example" do
expect { Net::HTTP.get(uri) }.to raise_error(Isolator::HTTPError, %r{GET http://localhost:4567/\?author=matz&lang=ruby})
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
require "net/http"
require "uri"

require "vcr"

begin
require "debug" unless ENV["CI"] == "true"
rescue LoadError # rubocop:disable Lint/HandleExceptions
Expand Down
7 changes: 7 additions & 0 deletions spec/support/vcr_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

VCR.configure do |config|
config.cassette_library_dir = "spec/integrations/fixtures/vcr_cassettes"
config.hook_into :webmock
config.allow_http_connections_when_no_cassette = false
end

0 comments on commit 2992014

Please sign in to comment.