vcr 1.11.3 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +6 -2
- data/CHANGELOG.md +49 -1
- data/Gemfile +1 -5
- data/Guardfile +0 -5
- data/README.md +3 -2
- data/Rakefile +11 -16
- data/cucumber.yml +0 -4
- data/features/.nav +14 -2
- data/features/cassettes/automatic_re_recording.feature +4 -6
- data/features/cassettes/dynamic_erb.feature +6 -8
- data/features/cassettes/exclusive.feature +111 -0
- data/features/cassettes/format.feature +16 -14
- data/features/cassettes/naming.feature +4 -6
- data/features/cassettes/no_cassette.feature +25 -28
- data/features/cassettes/update_content_length_header.feature +9 -9
- data/features/configuration/allow_http_connections_when_no_cassette.feature +6 -8
- data/features/configuration/cassette_library_dir.feature +4 -6
- data/features/configuration/default_cassette_options.feature +12 -10
- data/features/configuration/filter_sensitive_data.feature +12 -17
- data/features/configuration/{stub_with.feature → hook_into.feature} +63 -62
- data/features/configuration/hooks.feature +23 -33
- data/features/configuration/ignore_hosts.feature +16 -17
- data/features/configuration/ignore_localhost.feature +33 -42
- data/features/http_libraries/em_http_request.feature +7 -8
- data/features/http_libraries/net_http.feature +26 -28
- data/features/middleware/faraday.feature +17 -56
- data/features/middleware/rack.feature +8 -11
- data/features/record_modes/all.feature +5 -7
- data/features/record_modes/new_episodes.feature +5 -7
- data/features/record_modes/none.feature +6 -6
- data/features/record_modes/once.feature +6 -8
- data/features/request_matching/README.md +28 -0
- data/features/request_matching/body.feature +81 -0
- data/features/request_matching/custom_matcher.feature +125 -0
- data/features/request_matching/headers.feature +85 -0
- data/features/request_matching/host.feature +85 -0
- data/features/request_matching/identical_request_sequence.feature +79 -0
- data/features/request_matching/method.feature +86 -0
- data/features/request_matching/path.feature +86 -0
- data/features/request_matching/playback_repeats.feature +87 -0
- data/features/request_matching/uri.feature +84 -0
- data/features/request_matching/uri_without_param.feature +85 -0
- data/features/step_definitions/cli_steps.rb +4 -28
- data/features/support/env.rb +11 -9
- data/features/support/http_lib_filters.rb +2 -11
- data/features/support/vcr_cucumber_helpers.rb +4 -5
- data/features/test_frameworks/cucumber.feature +17 -18
- data/features/test_frameworks/rspec.feature +8 -12
- data/features/test_frameworks/shoulda.feature +5 -8
- data/features/test_frameworks/test_unit.feature +5 -8
- data/lib/vcr.rb +38 -58
- data/lib/vcr/cassette.rb +41 -60
- data/lib/vcr/cassette/http_interaction_list.rb +56 -0
- data/lib/vcr/cassette/reader.rb +29 -31
- data/lib/vcr/configuration.rb +76 -0
- data/lib/vcr/deprecations.rb +39 -0
- data/lib/vcr/errors.rb +22 -0
- data/lib/vcr/library_hooks.rb +19 -0
- data/lib/vcr/library_hooks/excon.rb +136 -0
- data/lib/vcr/library_hooks/fakeweb.rb +110 -0
- data/lib/vcr/library_hooks/faraday.rb +3 -0
- data/lib/vcr/library_hooks/typhoeus.rb +98 -0
- data/lib/vcr/library_hooks/webmock.rb +100 -0
- data/lib/vcr/middleware/faraday.rb +43 -36
- data/lib/vcr/middleware/rack.rb +28 -4
- data/lib/vcr/request_handler.rb +43 -0
- data/lib/vcr/request_ignorer.rb +31 -0
- data/lib/vcr/request_matcher_registry.rb +86 -0
- data/lib/vcr/structs/http_interaction.rb +24 -18
- data/lib/vcr/structs/normalizers/body.rb +1 -1
- data/lib/vcr/structs/normalizers/header.rb +1 -1
- data/lib/vcr/structs/normalizers/status_message.rb +1 -1
- data/lib/vcr/structs/normalizers/uri.rb +1 -1
- data/lib/vcr/structs/request.rb +0 -13
- data/lib/vcr/structs/response.rb +2 -9
- data/lib/vcr/structs/response_status.rb +0 -4
- data/lib/vcr/test_frameworks/cucumber.rb +1 -1
- data/lib/vcr/test_frameworks/rspec.rb +1 -1
- data/lib/vcr/util/hooks.rb +28 -19
- data/lib/vcr/util/internet_connection.rb +29 -2
- data/lib/vcr/util/version_checker.rb +60 -0
- data/lib/vcr/version.rb +1 -1
- data/script/FullBuildRakeFile +0 -7
- data/script/full_build +1 -1
- data/spec/capture_warnings.rb +36 -31
- data/spec/fixtures/{1.9.1/cassette_spec → cassette_spec}/empty.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/example.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/with_localhost_requests.yml +0 -0
- data/spec/fixtures/{not_1.9.1/fake_example.com_responses.yml → fake_example.com_responses.yml} +0 -0
- data/spec/fixtures/{not_1.9.1/match_requests_on.yml → match_requests_on.yml} +0 -0
- data/spec/monkey_patches.rb +40 -11
- data/spec/spec_helper.rb +7 -43
- data/spec/support/http_library_adapters.rb +3 -13
- data/spec/support/shared_example_groups/{http_library.rb → hook_into_http_library.rb} +39 -111
- data/spec/support/shared_example_groups/version_checking.rb +9 -9
- data/spec/vcr/cassette/http_interaction_list_spec.rb +178 -0
- data/spec/vcr/cassette/reader_spec.rb +2 -2
- data/spec/vcr/cassette_spec.rb +121 -156
- data/spec/vcr/configuration_spec.rb +143 -0
- data/spec/vcr/deprecations_spec.rb +91 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/excon_spec.rb +6 -9
- data/spec/vcr/library_hooks/fakeweb_spec.rb +83 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/typhoeus_spec.rb +7 -11
- data/spec/vcr/library_hooks/webmock_spec.rb +17 -0
- data/spec/vcr/library_hooks_spec.rb +51 -0
- data/spec/vcr/middleware/faraday_spec.rb +17 -44
- data/spec/vcr/middleware/rack_spec.rb +94 -58
- data/spec/vcr/request_ignorer_spec.rb +54 -0
- data/spec/vcr/request_matcher_registry_spec.rb +223 -0
- data/spec/vcr/structs/request_spec.rb +0 -33
- data/spec/vcr/structs/response_spec.rb +0 -24
- data/spec/vcr/structs/response_status_spec.rb +0 -9
- data/spec/vcr/util/hooks_spec.rb +3 -5
- data/spec/vcr/version_spec.rb +1 -1
- data/spec/vcr_spec.rb +79 -91
- data/vcr.gemspec +1 -1
- metadata +83 -103
- data/features/cassettes/request_matching.feature +0 -383
- data/lib/vcr/config.rb +0 -84
- data/lib/vcr/deprecations/cassette.rb +0 -29
- data/lib/vcr/deprecations/config.rb +0 -18
- data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +0 -9
- data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +0 -11
- data/lib/vcr/extensions/net_http.rb +0 -32
- data/lib/vcr/http_stubbing_adapters/common.rb +0 -202
- data/lib/vcr/http_stubbing_adapters/excon.rb +0 -178
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +0 -107
- data/lib/vcr/http_stubbing_adapters/faraday.rb +0 -26
- data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +0 -43
- data/lib/vcr/http_stubbing_adapters/typhoeus.rb +0 -115
- data/lib/vcr/http_stubbing_adapters/webmock.rb +0 -120
- data/lib/vcr/middleware/cassette_arguments.rb +0 -19
- data/lib/vcr/middleware/common.rb +0 -20
- data/lib/vcr/request_matcher.rb +0 -94
- data/lib/vcr/rspec.rb +0 -2
- data/lib/vcr/util/basic_object.rb +0 -43
- data/lib/vcr/util/ping.rb +0 -30
- data/lib/vcr/util/regexes.rb +0 -37
- data/spec/fixtures/1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/1.9.1/cassette_spec/example.yml +0 -110
- data/spec/fixtures/1.9.1/cassette_spec/with_localhost_requests.yml +0 -109
- data/spec/fixtures/1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/1.9.1/example_net_http_response.yml +0 -25
- data/spec/fixtures/1.9.1/fake_example.com_responses.yml +0 -108
- data/spec/fixtures/1.9.1/match_requests_on.yml +0 -185
- data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/not_1.9.1/cassette_spec/empty.yml +0 -0
- data/spec/fixtures/not_1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/not_1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/not_1.9.1/example_net_http_response.yml +0 -25
- data/spec/support/shared_example_groups/http_stubbing_adapter.rb +0 -133
- data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +0 -28
- data/spec/vcr/config_spec.rb +0 -181
- data/spec/vcr/deprecations/cassette_spec.rb +0 -57
- data/spec/vcr/deprecations/config_spec.rb +0 -30
- data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +0 -7
- data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +0 -16
- data/spec/vcr/extensions/net_http_spec.rb +0 -80
- data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +0 -19
- data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +0 -76
- data/spec/vcr/http_stubbing_adapters/multi_object_proxy_spec.rb +0 -101
- data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +0 -17
- data/spec/vcr/middleware/cassette_arguments_spec.rb +0 -32
- data/spec/vcr/request_matcher_spec.rb +0 -230
@@ -1,28 +1,28 @@
|
|
1
|
-
shared_examples_for "version checking" do |options|
|
2
|
-
|
1
|
+
shared_examples_for "version checking" do |library, options|
|
2
|
+
file = options[:file] || "vcr/library_hooks/#{library.downcase}.rb"
|
3
3
|
|
4
|
-
|
4
|
+
context 'when loading the library hook file', :disable_warnings => true do
|
5
5
|
options[:valid].each do |version|
|
6
6
|
it "does nothing when #{library}'s version is #{version}" do
|
7
7
|
stub_version(version)
|
8
|
-
|
9
|
-
expect {
|
8
|
+
Kernel.should_not_receive(:warn)
|
9
|
+
expect { load file }.to_not raise_error
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
options[:too_low].each do |version|
|
14
14
|
it "raises an error when #{library}'s version is #{version}" do
|
15
15
|
stub_version(version)
|
16
|
-
|
17
|
-
expect {
|
16
|
+
Kernel.should_not_receive(:warn)
|
17
|
+
expect { load file }.to raise_error(/You are using #{library} #{version}. VCR requires version/)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
options[:too_high].each do |version|
|
22
22
|
it "does nothing when #{library}'s version is #{version}" do
|
23
23
|
stub_version(version)
|
24
|
-
|
25
|
-
expect {
|
24
|
+
Kernel.should_receive(:warn).with(/VCR is known to work with #{library}/)
|
25
|
+
expect { load file }.to_not raise_error
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'vcr/cassette/http_interaction_list'
|
2
|
+
require 'vcr/request_matcher_registry'
|
3
|
+
require 'vcr/structs/http_interaction'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module VCR
|
7
|
+
class Cassette
|
8
|
+
|
9
|
+
shared_examples_for "an HTTP interaction finding method" do |method|
|
10
|
+
it 'returns nil when the list is empty' do
|
11
|
+
HTTPInteractionList.new([], [:method]).send(method, stub).should respond_with(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns nil when there is no matching interaction' do
|
15
|
+
HTTPInteractionList.new([
|
16
|
+
interaction('foo', :method => :post),
|
17
|
+
interaction('foo', :method => :put)
|
18
|
+
], [:method]).send(method,
|
19
|
+
request_with(:method => :get)
|
20
|
+
).should respond_with(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns the first matching interaction' do
|
24
|
+
list = HTTPInteractionList.new([
|
25
|
+
interaction('put response', :method => :put),
|
26
|
+
interaction('post response 1', :method => :post),
|
27
|
+
interaction('post response 2', :method => :post)
|
28
|
+
], [:method])
|
29
|
+
|
30
|
+
list.send(method, request_with(:method => :post)).should respond_with("post response 1")
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'invokes each matcher block to find the matching interaction' do
|
34
|
+
VCR.request_matchers.register(:foo) { |r1, r2| true }
|
35
|
+
VCR.request_matchers.register(:bar) { |r1, r2| true }
|
36
|
+
|
37
|
+
calls = 0
|
38
|
+
VCR.request_matchers.register(:baz) { |r1, r2| calls += 1; calls == 2 }
|
39
|
+
|
40
|
+
list = HTTPInteractionList.new([
|
41
|
+
interaction('response', :method => :put)
|
42
|
+
], [:foo, :bar, :baz])
|
43
|
+
|
44
|
+
list.send(method, request_with(:method => :post)).should respond_with(nil)
|
45
|
+
list.send(method, request_with(:method => :post)).should respond_with('response')
|
46
|
+
end
|
47
|
+
|
48
|
+
it "delegates to the parent list when it can't find a matching interaction" do
|
49
|
+
parent_list = mock(method => response('parent'))
|
50
|
+
HTTPInteractionList.new(
|
51
|
+
[], [:method], false, parent_list
|
52
|
+
).send(method, stub).should respond_with('parent')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe HTTPInteractionList do
|
57
|
+
before(:each) do
|
58
|
+
VCR.stub(:request_matchers => VCR::RequestMatcherRegistry.new)
|
59
|
+
end
|
60
|
+
|
61
|
+
def request_with(values)
|
62
|
+
VCR::Request.new.tap do |request|
|
63
|
+
values.each do |name, value|
|
64
|
+
request.send("#{name}=", value)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def response(body)
|
70
|
+
VCR::Response.new.tap { |r| r.body = body }
|
71
|
+
end
|
72
|
+
|
73
|
+
def interaction(body, request_values)
|
74
|
+
VCR::HTTPInteraction.new \
|
75
|
+
request_with(request_values),
|
76
|
+
response(body)
|
77
|
+
end
|
78
|
+
|
79
|
+
let(:original_list_array) do [
|
80
|
+
interaction('put response', :method => :put),
|
81
|
+
interaction('post response 1', :method => :post),
|
82
|
+
interaction('post response 2', :method => :post)
|
83
|
+
] end
|
84
|
+
|
85
|
+
let(:allow_playback_repeats) { false } # the default
|
86
|
+
let(:list) { HTTPInteractionList.new(original_list_array, [:method], allow_playback_repeats) }
|
87
|
+
|
88
|
+
describe "#has_interaction_matching?" do
|
89
|
+
it_behaves_like "an HTTP interaction finding method", :has_interaction_matching? do
|
90
|
+
def respond_with(value)
|
91
|
+
::RSpec::Matchers::Matcher.new :respond_with, value do |expected|
|
92
|
+
match { |a| expected ? a : !a }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'does not consume the first matching interaction' do
|
98
|
+
10.times do
|
99
|
+
list.has_interaction_matching?(request_with(:method => :post)).should be_true
|
100
|
+
end
|
101
|
+
list.response_for(request_with(:method => :post)).body.should eq("post response 1")
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when allow_playback_repeats is set to false' do
|
105
|
+
let(:allow_playback_repeats) { false }
|
106
|
+
|
107
|
+
it 'returns false when there is a used (but no unused) matching interactions' do
|
108
|
+
list.response_for(request_with(:method => :put))
|
109
|
+
|
110
|
+
10.times.map {
|
111
|
+
list.has_interaction_matching?(request_with(:method => :put))
|
112
|
+
}.should eq([false] * 10)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context 'when allow_playback_repeats is set to true' do
|
117
|
+
let(:allow_playback_repeats) { true }
|
118
|
+
|
119
|
+
it 'returns true when there is a used (but no unused) matching interactions' do
|
120
|
+
list.response_for(request_with(:method => :put))
|
121
|
+
|
122
|
+
10.times.map {
|
123
|
+
list.has_interaction_matching?(request_with(:method => :put))
|
124
|
+
}.should eq([true] * 10)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "#response_for" do
|
130
|
+
it_behaves_like "an HTTP interaction finding method", :response_for do
|
131
|
+
def respond_with(value)
|
132
|
+
::RSpec::Matchers::Matcher.new :respond_with, value do |expected|
|
133
|
+
match { |a| expected.nil? ? a.nil? : a.body == expected }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'consumes the first matching interaction so that it will not be used again' do
|
139
|
+
list.response_for(request_with(:method => :post)).body.should eq("post response 1")
|
140
|
+
list.response_for(request_with(:method => :post)).body.should eq("post response 2")
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'when allow_playback_repeats is set to true' do
|
144
|
+
let(:allow_playback_repeats) { true }
|
145
|
+
|
146
|
+
it 'continues to return the response from the last matching interaction when there are no more' do
|
147
|
+
list.response_for(request_with(:method => :post))
|
148
|
+
|
149
|
+
10.times.map {
|
150
|
+
response = list.response_for(request_with(:method => :post))
|
151
|
+
response ? response.body : nil
|
152
|
+
}.should eq(["post response 2"] * 10)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'when allow_playback_repeats is set to false' do
|
157
|
+
let(:allow_playback_repeats) { false }
|
158
|
+
|
159
|
+
it 'returns nil when there are no more unused interactions' do
|
160
|
+
list.response_for(request_with(:method => :post))
|
161
|
+
list.response_for(request_with(:method => :post))
|
162
|
+
|
163
|
+
10.times.map {
|
164
|
+
list.response_for(request_with(:method => :post))
|
165
|
+
}.should eq([nil] * 10)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'does not modify the original interaction array the list was initialized with' do
|
170
|
+
original_dup = original_list_array.dup
|
171
|
+
list.response_for(request_with(:method => :post))
|
172
|
+
original_list_array.should == original_dup
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
@@ -34,7 +34,7 @@ describe VCR::Cassette::Reader do
|
|
34
34
|
it 'raises an appropriate error when the ERB template needs variables' do
|
35
35
|
expect {
|
36
36
|
read('vars', true)
|
37
|
-
}.to raise_error(VCR::
|
37
|
+
}.to raise_error(VCR::Errors::MissingERBVariableError,
|
38
38
|
%{The ERB in the vars cassette file references undefined variable var1. } +
|
39
39
|
%{Pass it to the cassette using :erb => #{ {:var1=>"some value"}.inspect }.}
|
40
40
|
)
|
@@ -49,7 +49,7 @@ describe VCR::Cassette::Reader do
|
|
49
49
|
it 'raises an appropriate error when one or more of the needed variables are not passed' do
|
50
50
|
expect {
|
51
51
|
read('vars', :var1 => 'foo')
|
52
|
-
}.to raise_error(VCR::
|
52
|
+
}.to raise_error(VCR::Errors::MissingERBVariableError,
|
53
53
|
%{The ERB in the vars cassette file references undefined variable var2. } +
|
54
54
|
%{Pass it to the cassette using :erb => #{ {:var1 => "foo", :var2 => "some value"}.inspect }.}
|
55
55
|
)
|
data/spec/vcr/cassette_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe VCR::Cassette do
|
|
4
4
|
describe '#file' do
|
5
5
|
it 'combines the cassette_library_dir with the cassette name' do
|
6
6
|
cassette = VCR::Cassette.new('the_file')
|
7
|
-
cassette.file.should eq(File.join(VCR
|
7
|
+
cassette.file.should eq(File.join(VCR.configuration.cassette_library_dir, 'the_file.yml'))
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'strips out disallowed characters so that it is a valid file name with no spaces' do
|
@@ -17,10 +17,12 @@ describe VCR::Cassette do
|
|
17
17
|
cassette.file.should =~ /#{Regexp.escape('dir/file_name.yml')}$/
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
VCR::Cassette::VALID_RECORD_MODES.each do |mode|
|
21
|
+
it "returns nil if the cassette_library_dir is not set (when the record mode is :#{mode})" do
|
22
|
+
VCR.configuration.cassette_library_dir = nil
|
23
|
+
cassette = VCR::Cassette.new('the_file', :record => mode)
|
24
|
+
cassette.file.should be_nil
|
25
|
+
end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
@@ -33,8 +35,48 @@ describe VCR::Cassette do
|
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
38
|
+
describe "#recording?" do
|
39
|
+
[:all, :new_episodes].each do |mode|
|
40
|
+
it "returns true when the record mode is :#{mode}" do
|
41
|
+
cassette = VCR::Cassette.new("foo", :record => mode)
|
42
|
+
cassette.should be_recording
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns false when the record mode is :none" do
|
47
|
+
cassette = VCR::Cassette.new("foo", :record => :none)
|
48
|
+
cassette.should_not be_recording
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when the record mode is :once' do
|
52
|
+
before(:each) do
|
53
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns false when there is an existing cassette file with content' do
|
57
|
+
cassette = VCR::Cassette.new("example", :record => :once)
|
58
|
+
File.should exist(cassette.file)
|
59
|
+
File.size?(cassette.file).should be_true
|
60
|
+
cassette.should_not be_recording
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'returns true when there is an empty existing cassette file' do
|
64
|
+
cassette = VCR::Cassette.new("empty", :record => :once)
|
65
|
+
File.should exist(cassette.file)
|
66
|
+
File.size?(cassette.file).should be_false
|
67
|
+
cassette.should be_recording
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'returns true when there is no existing cassette file' do
|
71
|
+
cassette = VCR::Cassette.new("non_existant_file", :record => :once)
|
72
|
+
File.should_not exist(cassette.file)
|
73
|
+
cassette.should be_recording
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
36
78
|
describe '#match_requests_on' do
|
37
|
-
before(:each) { VCR
|
79
|
+
before(:each) { VCR.configuration.default_cassette_options.merge!(:match_requests_on => [:uri, :method]) }
|
38
80
|
|
39
81
|
it "returns the provided options" do
|
40
82
|
c = VCR::Cassette.new('example', :match_requests_on => [:uri])
|
@@ -48,11 +90,6 @@ describe VCR::Cassette do
|
|
48
90
|
end
|
49
91
|
|
50
92
|
describe '.new' do
|
51
|
-
it 'raises an error with a helpful message when loading an old unsupported cassette' do
|
52
|
-
VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}"
|
53
|
-
expect { VCR::Cassette.new('0_3_1_cassette') }.to raise_error(/The VCR cassette 0_3_1_cassette.yml uses an old format that is now deprecated/)
|
54
|
-
end
|
55
|
-
|
56
93
|
it "raises an error if given an invalid record mode" do
|
57
94
|
expect { VCR::Cassette.new(:test, :record => :not_a_record_mode) }.to raise_error(ArgumentError)
|
58
95
|
end
|
@@ -64,20 +101,10 @@ describe VCR::Cassette do
|
|
64
101
|
end
|
65
102
|
|
66
103
|
it 'does not raise an error in the case of an empty file' do
|
67
|
-
VCR
|
104
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
68
105
|
VCR::Cassette.new('empty', :record => :none).recorded_interactions.should eq([])
|
69
106
|
end
|
70
107
|
|
71
|
-
it 'creates a stubs checkpoint on the http_stubbing_adapter' do
|
72
|
-
cassette = nil
|
73
|
-
|
74
|
-
VCR.http_stubbing_adapter.should_receive(:create_stubs_checkpoint) do |c|
|
75
|
-
cassette = c
|
76
|
-
end
|
77
|
-
|
78
|
-
VCR::Cassette.new('example').should equal(cassette)
|
79
|
-
end
|
80
|
-
|
81
108
|
describe "reading the file from disk" do
|
82
109
|
before(:each) do
|
83
110
|
File.stub(:size? => true)
|
@@ -85,7 +112,7 @@ describe VCR::Cassette do
|
|
85
112
|
|
86
113
|
it 'reads the appropriate file from disk using a VCR::Cassette::Reader' do
|
87
114
|
VCR::Cassette::Reader.should_receive(:new).with(
|
88
|
-
"#{VCR
|
115
|
+
"#{VCR.configuration.cassette_library_dir}/foo.yml", anything
|
89
116
|
).and_return(mock('reader', :read => VCR::YAML.dump([])))
|
90
117
|
|
91
118
|
VCR::Cassette.new('foo', :record => :new_episodes)
|
@@ -94,7 +121,7 @@ describe VCR::Cassette do
|
|
94
121
|
[true, false, nil, { }].each do |erb|
|
95
122
|
it "passes #{erb.inspect} to the VCR::Cassette::Reader when given as the :erb option" do
|
96
123
|
# test that it overrides the default
|
97
|
-
VCR
|
124
|
+
VCR.configuration.default_cassette_options = { :erb => true }
|
98
125
|
|
99
126
|
VCR::Cassette::Reader.should_receive(:new).with(
|
100
127
|
anything, erb
|
@@ -104,7 +131,7 @@ describe VCR::Cassette do
|
|
104
131
|
end
|
105
132
|
|
106
133
|
it "passes #{erb.inspect} to the VCR::Cassette::Reader when it is the default :erb option and none is given" do
|
107
|
-
VCR
|
134
|
+
VCR.configuration.default_cassette_options = { :erb => erb }
|
108
135
|
|
109
136
|
VCR::Cassette::Reader.should_receive(:new).with(
|
110
137
|
anything, erb
|
@@ -116,49 +143,23 @@ describe VCR::Cassette do
|
|
116
143
|
end
|
117
144
|
|
118
145
|
VCR::Cassette::VALID_RECORD_MODES.each do |record_mode|
|
119
|
-
http_connections_allowed = (record_mode != :none)
|
120
146
|
stub_requests = (record_mode != :all)
|
121
147
|
|
122
|
-
context "when VCR
|
123
|
-
before(:each) { VCR
|
148
|
+
context "when VCR.configuration.default_cassette_options[:record] is :#{record_mode}" do
|
149
|
+
before(:each) { VCR.configuration.default_cassette_options = { :record => record_mode } }
|
124
150
|
|
125
|
-
it "defaults the record mode to #{record_mode} when VCR
|
151
|
+
it "defaults the record mode to #{record_mode} when VCR.configuration.default_cassette_options[:record] is #{record_mode}" do
|
126
152
|
cassette = VCR::Cassette.new(:test)
|
127
153
|
cassette.record_mode.should eq(record_mode)
|
128
154
|
end
|
129
155
|
end
|
130
156
|
|
131
157
|
context "when :#{record_mode} is passed as the record option" do
|
132
|
-
if record_mode == :none
|
133
|
-
it 'does not allow http connections when there is an existing cassette file with recorded interactions' do
|
134
|
-
VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
|
135
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed=).with(false)
|
136
|
-
c = VCR::Cassette.new('example', :record => :once)
|
137
|
-
File.should exist(c.file)
|
138
|
-
File.size?(c.file).should be_true
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'allows http connections when there is an empty existing cassette file' do
|
142
|
-
VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
|
143
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed=).with(true)
|
144
|
-
c = VCR::Cassette.new('empty', :record => :once)
|
145
|
-
File.should exist(c.file)
|
146
|
-
File.size?(c.file).should be_false
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'allows http connections when there is not an existing cassette file' do
|
150
|
-
VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
|
151
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed=).with(true)
|
152
|
-
c = VCR::Cassette.new('non_existant_file', :record => :once)
|
153
|
-
File.should_not exist(c.file)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
158
|
unless record_mode == :all
|
158
159
|
let(:interaction_1) { VCR::HTTPInteraction.new(VCR::Request.new(:get, 'http://example.com/'), VCR::Response.new(VCR::ResponseStatus.new)) }
|
159
160
|
let(:interaction_2) { VCR::HTTPInteraction.new(VCR::Request.new(:get, 'http://example.com/'), VCR::Response.new(VCR::ResponseStatus.new)) }
|
160
161
|
let(:interactions) { [interaction_1, interaction_2] }
|
161
|
-
before(:each) { VCR
|
162
|
+
before(:each) { VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec" }
|
162
163
|
|
163
164
|
it 'updates the content_length headers when given :update_content_length_header => true' do
|
164
165
|
VCR::YAML.stub(:load => interactions)
|
@@ -179,7 +180,7 @@ describe VCR::Cassette do
|
|
179
180
|
end
|
180
181
|
|
181
182
|
context "and re_record_interval is 7.days" do
|
182
|
-
let(:file_name) { File.join(VCR
|
183
|
+
let(:file_name) { File.join(VCR.configuration.cassette_library_dir, "cassette_name.yml") }
|
183
184
|
subject { VCR::Cassette.new(File.basename(file_name).gsub('.yml', ''), :record => record_mode, :re_record_interval => 7.days) }
|
184
185
|
|
185
186
|
context 'when the cassette file does not exist' do
|
@@ -221,23 +222,18 @@ describe VCR::Cassette do
|
|
221
222
|
end
|
222
223
|
end
|
223
224
|
|
224
|
-
it "sets http_connections_allowed to #{http_connections_allowed} on the http stubbing adapter" do
|
225
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed=).with(http_connections_allowed)
|
226
|
-
VCR::Cassette.new(:name, :record => record_mode)
|
227
|
-
end
|
228
|
-
|
229
225
|
it 'does not load ignored interactions' do
|
230
|
-
VCR
|
231
|
-
uri
|
226
|
+
VCR.request_ignorer.stub(:ignore?) do |request|
|
227
|
+
request.uri !~ /example\.com/
|
232
228
|
end
|
233
229
|
|
234
|
-
VCR
|
230
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
235
231
|
cassette = VCR::Cassette.new('with_localhost_requests', :record => record_mode)
|
236
232
|
cassette.recorded_interactions.map { |i| URI.parse(i.uri).host }.should eq(%w[example.com])
|
237
233
|
end
|
238
234
|
|
239
235
|
it "loads the recorded interactions from the library yml file" do
|
240
|
-
VCR
|
236
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
241
237
|
cassette = VCR::Cassette.new('example', :record => record_mode)
|
242
238
|
|
243
239
|
cassette.should have(3).recorded_interactions
|
@@ -257,9 +253,31 @@ describe VCR::Cassette do
|
|
257
253
|
i3.response.body.should =~ /Another example\.com response/
|
258
254
|
end
|
259
255
|
|
256
|
+
[true, false].each do |value|
|
257
|
+
it "instantiates the http_interactions with allow_playback_repeats = #{value} if given :allow_playback_repeats => #{value}" do
|
258
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
259
|
+
cassette = VCR::Cassette.new('example', :record => record_mode, :allow_playback_repeats => value)
|
260
|
+
cassette.http_interactions.allow_playback_repeats.should eq(value)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
it "instantiates the http_interactions with parent_list set to a null list if given :exclusive => true" do
|
265
|
+
VCR.stub(:http_interactions => stub)
|
266
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
267
|
+
cassette = VCR::Cassette.new('example', :record => record_mode, :exclusive => true)
|
268
|
+
cassette.http_interactions.parent_list.should be_a(VCR::Cassette::HTTPInteractionList::NullList)
|
269
|
+
end
|
270
|
+
|
271
|
+
it "instantiates the http_interactions with parent_list set to VCR.http_interactions if given :exclusive => false" do
|
272
|
+
VCR.stub(:http_interactions => stub)
|
273
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
274
|
+
cassette = VCR::Cassette.new('example', :record => record_mode, :exclusive => false)
|
275
|
+
cassette.http_interactions.parent_list.should be(VCR.http_interactions)
|
276
|
+
end
|
277
|
+
|
260
278
|
if stub_requests
|
261
279
|
it 'invokes the appropriately tagged before_playback hooks' do
|
262
|
-
VCR
|
280
|
+
VCR.configuration.should_receive(:invoke_hook).with(
|
263
281
|
:before_playback,
|
264
282
|
:foo,
|
265
283
|
an_instance_of(VCR::HTTPInteraction),
|
@@ -271,31 +289,27 @@ describe VCR::Cassette do
|
|
271
289
|
end
|
272
290
|
|
273
291
|
it 'does not playback any interactions that are ignored in a before_playback hook' do
|
274
|
-
VCR.
|
292
|
+
VCR.configure do |c|
|
275
293
|
c.before_playback { |i| i.ignore! if i.request.uri =~ /foo/ }
|
276
294
|
end
|
277
295
|
|
278
|
-
VCR
|
296
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
279
297
|
cassette = VCR::Cassette.new('example', :record => record_mode)
|
280
298
|
cassette.should have(2).recorded_interactions
|
281
299
|
end
|
282
300
|
|
283
|
-
it
|
284
|
-
VCR
|
285
|
-
VCR.
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
it "passes the :match_request_on option to #stub_requests" do
|
290
|
-
VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
|
291
|
-
VCR.http_stubbing_adapter.should_receive(:stub_requests).with(anything, [:body, :headers])
|
292
|
-
VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])
|
301
|
+
it 'instantiates the http_interactions with the loaded interactions and the request matchers' do
|
302
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
303
|
+
cassette = VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])
|
304
|
+
cassette.http_interactions.interactions.should have(3).interactions
|
305
|
+
cassette.http_interactions.request_matchers.should == [:body, :headers]
|
293
306
|
end
|
294
307
|
else
|
295
|
-
it
|
296
|
-
VCR
|
297
|
-
VCR.
|
298
|
-
|
308
|
+
it 'instantiates the http_interactions with the no interactions and the request matchers' do
|
309
|
+
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
310
|
+
cassette = VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])
|
311
|
+
cassette.http_interactions.interactions.should have(0).interactions
|
312
|
+
cassette.http_interactions.request_matchers.should == [:body, :headers]
|
299
313
|
end
|
300
314
|
end
|
301
315
|
end
|
@@ -303,15 +317,6 @@ describe VCR::Cassette do
|
|
303
317
|
end
|
304
318
|
|
305
319
|
describe '#eject' do
|
306
|
-
[true, false].each do |orig_http_connections_allowed|
|
307
|
-
it "resets #{orig_http_connections_allowed} on the http stubbing adapter if it was originally #{orig_http_connections_allowed}" do
|
308
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed?).and_return(orig_http_connections_allowed)
|
309
|
-
cassette = VCR::Cassette.new(:name)
|
310
|
-
VCR.http_stubbing_adapter.should_receive(:http_connections_allowed=).with(orig_http_connections_allowed)
|
311
|
-
cassette.eject
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
320
|
it "writes the recorded interactions to disk as yaml" do
|
316
321
|
recorded_interactions = [
|
317
322
|
VCR::HTTPInteraction.new(:req_sig_1, :response_1),
|
@@ -337,7 +342,7 @@ describe VCR::Cassette do
|
|
337
342
|
cassette.stub!(:new_recorded_interactions).and_return(interactions)
|
338
343
|
|
339
344
|
interactions.each do |i|
|
340
|
-
VCR
|
345
|
+
VCR.configuration.should_receive(:invoke_hook).with(
|
341
346
|
:before_record,
|
342
347
|
:foo,
|
343
348
|
i,
|
@@ -388,13 +393,8 @@ describe VCR::Cassette do
|
|
388
393
|
subject { VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:uri]) }
|
389
394
|
|
390
395
|
before(:each) do
|
391
|
-
base_dir = "#{VCR::SPEC_ROOT}/fixtures
|
392
|
-
FileUtils.cp(base_dir + "/example.yml", VCR
|
393
|
-
end
|
394
|
-
|
395
|
-
it "restore the stubs checkpoint on the http stubbing adapter" do
|
396
|
-
VCR.http_stubbing_adapter.should_receive(:restore_stubs_checkpoint).with(subject)
|
397
|
-
subject.eject
|
396
|
+
base_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"
|
397
|
+
FileUtils.cp(base_dir + "/example.yml", VCR.configuration.cassette_library_dir + "/example.yml")
|
398
398
|
end
|
399
399
|
|
400
400
|
it "does not re-write to disk the previously recorded interactions if there are no new ones" do
|
@@ -404,74 +404,39 @@ describe VCR::Cassette do
|
|
404
404
|
end
|
405
405
|
|
406
406
|
context 'when some new interactions have been recorded' do
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
let(:old_interaction_1) { subject.recorded_interactions[0] }
|
412
|
-
let(:old_interaction_2) { subject.recorded_interactions[1] }
|
413
|
-
let(:old_interaction_3) { subject.recorded_interactions[2] }
|
414
|
-
|
415
|
-
let(:saved_recorded_interactions) { VCR::YAML.load_file(subject.file) }
|
416
|
-
|
417
|
-
def stub_matcher_for(interaction, matcher)
|
418
|
-
# There are issues with serializing an object stubbed w/ rspec-mocks using Psych.
|
419
|
-
# So we manually define the method here.
|
420
|
-
class << interaction.request; self; end.class_eval do
|
421
|
-
define_method(:matcher) { |*a| matcher }
|
407
|
+
def interaction(response, request_attributes)
|
408
|
+
request = VCR::Request.new
|
409
|
+
request_attributes.each do |key, value|
|
410
|
+
request.send("#{key}=", value)
|
422
411
|
end
|
412
|
+
VCR::HTTPInteraction.new(request, response)
|
423
413
|
end
|
424
414
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
stub_matcher_for(old_interaction_3, :matcher_c)
|
415
|
+
let(:interaction_foo_1) { interaction("foo 1", :uri => 'http://foo.com/') }
|
416
|
+
let(:interaction_foo_2) { interaction("foo 2", :uri => 'http://foo.com/') }
|
417
|
+
let(:interaction_bar) { interaction("bar", :uri => 'http://bar.com/') }
|
429
418
|
|
430
|
-
|
431
|
-
stub_matcher_for(new_interaction_2, :matcher_b)
|
432
|
-
stub_matcher_for(new_interaction_3, :matcher_c)
|
419
|
+
let(:saved_recorded_interactions) { VCR::YAML.load_file(subject.file) }
|
433
420
|
|
434
|
-
|
435
|
-
|
436
|
-
|
421
|
+
before(:each) do
|
422
|
+
subject.stub(:recorded_interactions => [interaction_foo_1])
|
423
|
+
subject.record_http_interaction(interaction_foo_2)
|
424
|
+
subject.record_http_interaction(interaction_bar)
|
425
|
+
subject.eject
|
437
426
|
end
|
438
427
|
|
439
428
|
if record_mode == :all
|
440
|
-
it '
|
441
|
-
|
442
|
-
|
443
|
-
saved_recorded_interactions.should eq([
|
444
|
-
old_interaction_2,
|
445
|
-
new_interaction_1,
|
446
|
-
new_interaction_2,
|
447
|
-
new_interaction_3
|
448
|
-
])
|
429
|
+
it 'replaces previously recorded interactions with new ones when the requests match' do
|
430
|
+
saved_recorded_interactions.first.should eq(interaction_foo_2)
|
431
|
+
saved_recorded_interactions.should_not include(interaction_foo_1)
|
449
432
|
end
|
450
433
|
|
451
|
-
it
|
452
|
-
|
453
|
-
|
454
|
-
old_interaction_1, old_interaction_2, old_interaction_3,
|
455
|
-
new_interaction_1, new_interaction_2, new_interaction_3
|
456
|
-
].each do |i|
|
457
|
-
i.request.should_receive(:matcher).with(subject.match_requests_on).and_return(:the_matcher)
|
458
|
-
end
|
459
|
-
|
460
|
-
subject.eject
|
461
|
-
end
|
462
|
-
end unless ENV['CI']
|
434
|
+
it 'appends new recorded interactions that do not match existing ones' do
|
435
|
+
saved_recorded_interactions.last.should eq(interaction_bar)
|
436
|
+
end
|
463
437
|
else
|
464
|
-
it '
|
465
|
-
|
466
|
-
|
467
|
-
saved_recorded_interactions.should eq([
|
468
|
-
old_interaction_1,
|
469
|
-
old_interaction_2,
|
470
|
-
old_interaction_3,
|
471
|
-
new_interaction_1,
|
472
|
-
new_interaction_2,
|
473
|
-
new_interaction_3
|
474
|
-
])
|
438
|
+
it 'appends new recorded interactions after existing ones' do
|
439
|
+
saved_recorded_interactions.should eq([interaction_foo_1, interaction_foo_2, interaction_bar])
|
475
440
|
end
|
476
441
|
end
|
477
442
|
end
|