forked from vcr/vcr
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure all request hooks are only called once per request.
Previously, the before_http_request hooks were being called twice for one request in some cases when using FakeWeb and Net::HTTP. Also, in the VCR test environment there could be many hook invocations from WebMock/Typhoeus due to double-registration of hooks and such.
- Loading branch information
1 parent
6ea465d
commit 7401fa7
Showing
9 changed files
with
130 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
spec/support/shared_example_groups/after_http_request_hook.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
shared_examples_for "request hooks" do |library_hook_name| | ||
let(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" } | ||
|
||
before(:each) do | ||
# ensure that all the other library hooks are disabled so that we don't | ||
# get double-hookage (such as for WebMock and Typhoeus both invoking the | ||
# hooks for a typhoeus request) | ||
VCR.library_hooks.stub(:disabled?) { |lib_name| lib_name != library_hook_name } | ||
end | ||
|
||
def make_request(disabled = false) | ||
make_http_request(:get, request_url) | ||
end | ||
|
||
def assert_expected_response(response) | ||
response.status.code.should eq(200) | ||
response.body.should eq('FOO!') | ||
end | ||
|
||
[:before_http_request, :after_http_request].each do |hook| | ||
specify "the #{hook} hook is only called once per request" do | ||
call_count = 0 | ||
VCR.configuration.send(hook) { |r| call_count += 1 } | ||
|
||
make_request | ||
call_count.should eq(1) | ||
end | ||
|
||
specify "the #{hook} hook yields the request" do | ||
request = nil | ||
VCR.configuration.send(hook) { |r| request = r } | ||
|
||
make_request | ||
request.method.should be(:get) | ||
request.uri.should eq(request_url) | ||
end | ||
|
||
specify "the #{hook} hook is not called if the library hook is disabled" do | ||
VCR.library_hooks.should respond_to(:disabled?) | ||
VCR.library_hooks.stub(:disabled? => true) | ||
|
||
hook_called = false | ||
VCR.configuration.send(hook) { |r| hook_called = true } | ||
|
||
make_request(:disabled) | ||
hook_called.should be_false | ||
end | ||
end | ||
|
||
specify "the after_http_request hook yields the response if there is one and the second block arg is given" do | ||
response = nil | ||
VCR.configuration.after_http_request { |req, res| response = res } | ||
|
||
make_request | ||
assert_expected_response(response) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters