Allow users to globally handle all requests without needing to wrap code in VCR.use_cassette #91

Closed
myronmarston opened this Issue Oct 14, 2011 · 0 comments

1 participant

@myronmarston
VCR member

Now that VCR has a global hook into every request, it makes it possible to do something like this:

VCR.configure do |c|
  c.before_cassetteless_request do |request|
    VCR.insert_cassette(URI(request.uri).host)
  end
  c.after_cassetteless_request do |request|
    VCR.eject_cassette
  end
end

Open questions:

  • Is this the right API for this?
  • I could provide an around_cassetteless_request callback, too, but that would only work with fibers on 1.9. Is it worth it? (I'm leaning towards yes).
  • Should the argument yielded to the after_cassetteless_request be the whole HTTPInteraction?
  • What else could users use this for beside inserting/ejecting a cassette? One idea is to allow them to call request.ignore! in the before hook, which would cause the request to be ignored.
@myronmarston myronmarston added a commit that referenced this issue Nov 20, 2011
@myronmarston myronmarston Add before_http_request hook.
For issue #91.
a8110df
@myronmarston myronmarston added a commit that referenced this issue Nov 20, 2011
@myronmarston myronmarston Add after_http_request hook.
For issue #91.

Note that when using Typhoeus or WebMock, it's not guaranteed that the after_request hook will fire for every request; if an error (such as a socket error) occurs before the request completes then the Typhoeus/WebMock after_request hook will not fire, which means VCR will not be able to fire its after_http_request hook in turn.  There's not much I can do about this, unfortunately.

For Excon, Faraday and FakeWeb, I use an ensure block to guarantee that the hook will be invoked.  This is possible because VCR wraps the entire request for these library hooks.
34d6536
@myronmarston myronmarston added a commit that referenced this issue Nov 23, 2011
@myronmarston myronmarston Add around_http_request hook.
For issue #91.
0938c61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment