Skip to content

Instantly share code, notes, and snippets.

@hisapy
Last active August 29, 2015 14:11
Show Gist options
  • Save hisapy/5d5b04aa894f21e15788 to your computer and use it in GitHub Desktop.
Save hisapy/5d5b04aa894f21e15788 to your computer and use it in GitHub Desktop.
RSpec: call VCR registered request matcher from inside custom matcher
# This is an excerpt from a spec_helper file with a section to configure VCR
#
# In lines 30 and 32 you can see examples of how to call a VCR registered request matcher from inside another.
# This is useful when running an example that makes a request to multiple external resources or APIs.
# Without this the requests were recorded the 1st time but subsequent requests raised VCR::Errors::UnhandledHTTPRequestError
VCR.configure do |c|
c.ignore_localhost = true
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
c.allow_http_connections_when_no_cassette = false
c.configure_rspec_metadata!
#Custom Matchers
c.register_request_matcher :w3_css_validator, &VCR.request_matchers.uri_without_params(:output, :profile, :text)
c.register_request_matcher :s3_paperclip do |r1, r2|
#these .match might not be needed
r1_path = URI(r1.uri).path.match("#{CONFIG[:amazon][:s3][:bucket]}/").to_s
r2_path = URI(r2.uri).path.match("#{CONFIG[:amazon][:s3][:bucket]}/").to_s
if URI(r1.uri).host != "s3.amazonaws.com" || !r1_path
false
else
URI(r1.uri).host == URI(r2.uri).host && r1_path == r2_path
end
end
c.register_request_matcher :css_validator_and_s3_requests do | r1, r2|
if URI(r1.uri).host == "jigsaw.w3.org"
VCR.request_matchers[:w3_css_validator].callable.call(r1, r2)
elsif URI(r1.uri).host == "s3.amazonaws.com"
VCR.request_matchers[:s3_paperclip].callable.call(r1, r2)
end
end
end
@hisapy
Copy link
Author

hisapy commented Dec 15, 2014

Thanks for taking your time to comment on this gist Myron. I'm using record: :once and have been using :w3_css_validator and :s3_paperclip separately before creating the :css_validator_and_s3_requests to handle requests of a model that queries those APIs at the same time. I'll read your suggestions carefully and see if I can simplify these requests matching logic.

Thanks a lot again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment