Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Print a warning when a non-standard Faraday stack is used.

When the Faraday stack includes a middleware after the HTTP adapter it gets "first dibs" on modifying the response and thus can prevent VCR from recording the response properly.  This is a problem with the instagram gem.

For #159.
  • Loading branch information...
commit e536a51f5c45270b61ae02b5542d014c5ae5f12c 1 parent 6723279
@myronmarston authored
View
12 lib/vcr/library_hooks/faraday.rb
@@ -25,8 +25,20 @@ def lock!(*args)
def insert_vcr_middleware
return if handlers.any? { |h| h.klass == VCR::Middleware::Faraday }
adapter_index = handlers.index { |h| h.klass < ::Faraday::Adapter }
+ warn_about_after_adapter_middleware(adapter_index)
insert_before(adapter_index, VCR::Middleware::Faraday)
end
+
+ def warn_about_after_adapter_middleware(adapter_index)
+ after_adapter_middleware_count = (handlers.size - adapter_index - 1)
+ return if after_adapter_middleware_count.zero?
+
+ after_adapter_middlewares = handlers.last(after_adapter_middleware_count)
+ warn "WARNING: The Faraday connection stack contains middleware after " +
+ "the HTTP adapter (#{after_adapter_middlewares.map(&:inspect).join(', ')}). " +
+ "This is a non-standard configuration and VCR may not be able to " +
+ "record the HTTP requests made through this Faraday connection."
+ end
end
end
end
View
10 spec/vcr/library_hooks/faraday_spec.rb
@@ -40,5 +40,15 @@
Faraday::Adapter::NetHttp
])
end
+
+ it 'prints a warning if the faraday connection stack contains a middleware after the HTTP adapter' do
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
+ builder.use Faraday::Adapter::NetHttp
+ builder.use Faraday::Response::Logger
+ end
+
+ conn.builder.should_receive(:warn).with(/Faraday::Response::Logger/)
+ conn.builder.lock!
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.