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.
Files changed (166) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +6 -2
  3. data/CHANGELOG.md +49 -1
  4. data/Gemfile +1 -5
  5. data/Guardfile +0 -5
  6. data/README.md +3 -2
  7. data/Rakefile +11 -16
  8. data/cucumber.yml +0 -4
  9. data/features/.nav +14 -2
  10. data/features/cassettes/automatic_re_recording.feature +4 -6
  11. data/features/cassettes/dynamic_erb.feature +6 -8
  12. data/features/cassettes/exclusive.feature +111 -0
  13. data/features/cassettes/format.feature +16 -14
  14. data/features/cassettes/naming.feature +4 -6
  15. data/features/cassettes/no_cassette.feature +25 -28
  16. data/features/cassettes/update_content_length_header.feature +9 -9
  17. data/features/configuration/allow_http_connections_when_no_cassette.feature +6 -8
  18. data/features/configuration/cassette_library_dir.feature +4 -6
  19. data/features/configuration/default_cassette_options.feature +12 -10
  20. data/features/configuration/filter_sensitive_data.feature +12 -17
  21. data/features/configuration/{stub_with.feature → hook_into.feature} +63 -62
  22. data/features/configuration/hooks.feature +23 -33
  23. data/features/configuration/ignore_hosts.feature +16 -17
  24. data/features/configuration/ignore_localhost.feature +33 -42
  25. data/features/http_libraries/em_http_request.feature +7 -8
  26. data/features/http_libraries/net_http.feature +26 -28
  27. data/features/middleware/faraday.feature +17 -56
  28. data/features/middleware/rack.feature +8 -11
  29. data/features/record_modes/all.feature +5 -7
  30. data/features/record_modes/new_episodes.feature +5 -7
  31. data/features/record_modes/none.feature +6 -6
  32. data/features/record_modes/once.feature +6 -8
  33. data/features/request_matching/README.md +28 -0
  34. data/features/request_matching/body.feature +81 -0
  35. data/features/request_matching/custom_matcher.feature +125 -0
  36. data/features/request_matching/headers.feature +85 -0
  37. data/features/request_matching/host.feature +85 -0
  38. data/features/request_matching/identical_request_sequence.feature +79 -0
  39. data/features/request_matching/method.feature +86 -0
  40. data/features/request_matching/path.feature +86 -0
  41. data/features/request_matching/playback_repeats.feature +87 -0
  42. data/features/request_matching/uri.feature +84 -0
  43. data/features/request_matching/uri_without_param.feature +85 -0
  44. data/features/step_definitions/cli_steps.rb +4 -28
  45. data/features/support/env.rb +11 -9
  46. data/features/support/http_lib_filters.rb +2 -11
  47. data/features/support/vcr_cucumber_helpers.rb +4 -5
  48. data/features/test_frameworks/cucumber.feature +17 -18
  49. data/features/test_frameworks/rspec.feature +8 -12
  50. data/features/test_frameworks/shoulda.feature +5 -8
  51. data/features/test_frameworks/test_unit.feature +5 -8
  52. data/lib/vcr.rb +38 -58
  53. data/lib/vcr/cassette.rb +41 -60
  54. data/lib/vcr/cassette/http_interaction_list.rb +56 -0
  55. data/lib/vcr/cassette/reader.rb +29 -31
  56. data/lib/vcr/configuration.rb +76 -0
  57. data/lib/vcr/deprecations.rb +39 -0
  58. data/lib/vcr/errors.rb +22 -0
  59. data/lib/vcr/library_hooks.rb +19 -0
  60. data/lib/vcr/library_hooks/excon.rb +136 -0
  61. data/lib/vcr/library_hooks/fakeweb.rb +110 -0
  62. data/lib/vcr/library_hooks/faraday.rb +3 -0
  63. data/lib/vcr/library_hooks/typhoeus.rb +98 -0
  64. data/lib/vcr/library_hooks/webmock.rb +100 -0
  65. data/lib/vcr/middleware/faraday.rb +43 -36
  66. data/lib/vcr/middleware/rack.rb +28 -4
  67. data/lib/vcr/request_handler.rb +43 -0
  68. data/lib/vcr/request_ignorer.rb +31 -0
  69. data/lib/vcr/request_matcher_registry.rb +86 -0
  70. data/lib/vcr/structs/http_interaction.rb +24 -18
  71. data/lib/vcr/structs/normalizers/body.rb +1 -1
  72. data/lib/vcr/structs/normalizers/header.rb +1 -1
  73. data/lib/vcr/structs/normalizers/status_message.rb +1 -1
  74. data/lib/vcr/structs/normalizers/uri.rb +1 -1
  75. data/lib/vcr/structs/request.rb +0 -13
  76. data/lib/vcr/structs/response.rb +2 -9
  77. data/lib/vcr/structs/response_status.rb +0 -4
  78. data/lib/vcr/test_frameworks/cucumber.rb +1 -1
  79. data/lib/vcr/test_frameworks/rspec.rb +1 -1
  80. data/lib/vcr/util/hooks.rb +28 -19
  81. data/lib/vcr/util/internet_connection.rb +29 -2
  82. data/lib/vcr/util/version_checker.rb +60 -0
  83. data/lib/vcr/version.rb +1 -1
  84. data/script/FullBuildRakeFile +0 -7
  85. data/script/full_build +1 -1
  86. data/spec/capture_warnings.rb +36 -31
  87. data/spec/fixtures/{1.9.1/cassette_spec → cassette_spec}/empty.yml +0 -0
  88. data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/example.yml +0 -0
  89. data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/with_localhost_requests.yml +0 -0
  90. data/spec/fixtures/{not_1.9.1/fake_example.com_responses.yml → fake_example.com_responses.yml} +0 -0
  91. data/spec/fixtures/{not_1.9.1/match_requests_on.yml → match_requests_on.yml} +0 -0
  92. data/spec/monkey_patches.rb +40 -11
  93. data/spec/spec_helper.rb +7 -43
  94. data/spec/support/http_library_adapters.rb +3 -13
  95. data/spec/support/shared_example_groups/{http_library.rb → hook_into_http_library.rb} +39 -111
  96. data/spec/support/shared_example_groups/version_checking.rb +9 -9
  97. data/spec/vcr/cassette/http_interaction_list_spec.rb +178 -0
  98. data/spec/vcr/cassette/reader_spec.rb +2 -2
  99. data/spec/vcr/cassette_spec.rb +121 -156
  100. data/spec/vcr/configuration_spec.rb +143 -0
  101. data/spec/vcr/deprecations_spec.rb +91 -0
  102. data/spec/vcr/{http_stubbing_adapters → library_hooks}/excon_spec.rb +6 -9
  103. data/spec/vcr/library_hooks/fakeweb_spec.rb +83 -0
  104. data/spec/vcr/{http_stubbing_adapters → library_hooks}/typhoeus_spec.rb +7 -11
  105. data/spec/vcr/library_hooks/webmock_spec.rb +17 -0
  106. data/spec/vcr/library_hooks_spec.rb +51 -0
  107. data/spec/vcr/middleware/faraday_spec.rb +17 -44
  108. data/spec/vcr/middleware/rack_spec.rb +94 -58
  109. data/spec/vcr/request_ignorer_spec.rb +54 -0
  110. data/spec/vcr/request_matcher_registry_spec.rb +223 -0
  111. data/spec/vcr/structs/request_spec.rb +0 -33
  112. data/spec/vcr/structs/response_spec.rb +0 -24
  113. data/spec/vcr/structs/response_status_spec.rb +0 -9
  114. data/spec/vcr/util/hooks_spec.rb +3 -5
  115. data/spec/vcr/version_spec.rb +1 -1
  116. data/spec/vcr_spec.rb +79 -91
  117. data/vcr.gemspec +1 -1
  118. metadata +83 -103
  119. data/features/cassettes/request_matching.feature +0 -383
  120. data/lib/vcr/config.rb +0 -84
  121. data/lib/vcr/deprecations/cassette.rb +0 -29
  122. data/lib/vcr/deprecations/config.rb +0 -18
  123. data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +0 -9
  124. data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +0 -11
  125. data/lib/vcr/extensions/net_http.rb +0 -32
  126. data/lib/vcr/http_stubbing_adapters/common.rb +0 -202
  127. data/lib/vcr/http_stubbing_adapters/excon.rb +0 -178
  128. data/lib/vcr/http_stubbing_adapters/fakeweb.rb +0 -107
  129. data/lib/vcr/http_stubbing_adapters/faraday.rb +0 -26
  130. data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +0 -43
  131. data/lib/vcr/http_stubbing_adapters/typhoeus.rb +0 -115
  132. data/lib/vcr/http_stubbing_adapters/webmock.rb +0 -120
  133. data/lib/vcr/middleware/cassette_arguments.rb +0 -19
  134. data/lib/vcr/middleware/common.rb +0 -20
  135. data/lib/vcr/request_matcher.rb +0 -94
  136. data/lib/vcr/rspec.rb +0 -2
  137. data/lib/vcr/util/basic_object.rb +0 -43
  138. data/lib/vcr/util/ping.rb +0 -30
  139. data/lib/vcr/util/regexes.rb +0 -37
  140. data/spec/fixtures/1.9.1/0_3_1_cassette.yml +0 -29
  141. data/spec/fixtures/1.9.1/cassette_spec/example.yml +0 -110
  142. data/spec/fixtures/1.9.1/cassette_spec/with_localhost_requests.yml +0 -109
  143. data/spec/fixtures/1.9.1/example_net_http.yml +0 -14
  144. data/spec/fixtures/1.9.1/example_net_http_request.yml +0 -12
  145. data/spec/fixtures/1.9.1/example_net_http_response.yml +0 -25
  146. data/spec/fixtures/1.9.1/fake_example.com_responses.yml +0 -108
  147. data/spec/fixtures/1.9.1/match_requests_on.yml +0 -185
  148. data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +0 -29
  149. data/spec/fixtures/not_1.9.1/cassette_spec/empty.yml +0 -0
  150. data/spec/fixtures/not_1.9.1/example_net_http.yml +0 -14
  151. data/spec/fixtures/not_1.9.1/example_net_http_request.yml +0 -12
  152. data/spec/fixtures/not_1.9.1/example_net_http_response.yml +0 -25
  153. data/spec/support/shared_example_groups/http_stubbing_adapter.rb +0 -133
  154. data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +0 -28
  155. data/spec/vcr/config_spec.rb +0 -181
  156. data/spec/vcr/deprecations/cassette_spec.rb +0 -57
  157. data/spec/vcr/deprecations/config_spec.rb +0 -30
  158. data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +0 -7
  159. data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +0 -16
  160. data/spec/vcr/extensions/net_http_spec.rb +0 -80
  161. data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +0 -19
  162. data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +0 -76
  163. data/spec/vcr/http_stubbing_adapters/multi_object_proxy_spec.rb +0 -101
  164. data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +0 -17
  165. data/spec/vcr/middleware/cassette_arguments_spec.rb +0 -32
  166. data/spec/vcr/request_matcher_spec.rb +0 -230
@@ -1,28 +1,28 @@
1
- shared_examples_for "version checking" do |options|
2
- library = described_class.library_name
1
+ shared_examples_for "version checking" do |library, options|
2
+ file = options[:file] || "vcr/library_hooks/#{library.downcase}.rb"
3
3
 
4
- describe '.check_version!', :disable_warnings => true do
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
- described_class.should_not_receive(:warn)
9
- expect { described_class.check_version! }.to_not raise_error
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
- described_class.should_not_receive(:warn)
17
- expect { described_class.check_version! }.to raise_error(/You are using #{library} #{version}. VCR requires version/)
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
- described_class.should_receive(:warn).with(/VCR is known to work with #{library}/)
25
- expect { described_class.check_version! }.to_not raise_error
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::Cassette::MissingERBVariableError,
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::Cassette::MissingERBVariableError,
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
  )
@@ -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::Config.cassette_library_dir, 'the_file.yml'))
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
- it 'returns nil if the cassette_library_dir is not set' do
21
- VCR::Config.cassette_library_dir = nil
22
- cassette = VCR::Cassette.new('the_file')
23
- cassette.file.should be_nil
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::Config.default_cassette_options.merge!(:match_requests_on => [:uri, :method]) }
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::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
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::Config.cassette_library_dir}/foo.yml", anything
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::Config.default_cassette_options = { :erb => true }
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::Config.default_cassette_options = { :erb => erb }
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::Config.default_cassette_options[:record] is :#{record_mode}" do
123
- before(:each) { VCR::Config.default_cassette_options = { :record => record_mode } }
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::Config.default_cassette_options[:record] is #{record_mode}" do
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::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec" }
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::Config.cassette_library_dir, "cassette_name.yml") }
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::Config.stub(:uri_should_be_ignored?) do |uri|
231
- uri.to_s !~ /example\.com/
226
+ VCR.request_ignorer.stub(:ignore?) do |request|
227
+ request.uri !~ /example\.com/
232
228
  end
233
229
 
234
- VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
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::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
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::Config.should_receive(:invoke_hook).with(
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.config do |c|
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::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
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 "stubs the recorded requests with the http stubbing adapter" do
284
- VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
285
- VCR.http_stubbing_adapter.should_receive(:stub_requests).with([an_instance_of(VCR::HTTPInteraction)]*3, anything)
286
- VCR::Cassette.new('example', :record => record_mode)
287
- end
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 "does not stub the recorded requests with the http stubbing adapter" do
296
- VCR::Config.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
297
- VCR.http_stubbing_adapter.should_not_receive(:stub_requests)
298
- VCR::Cassette.new('example', :record => record_mode)
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::Config.should_receive(:invoke_hook).with(
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/#{YAML_SERIALIZATION_VERSION}/cassette_spec"
392
- FileUtils.cp(base_dir + "/example.yml", VCR::Config.cassette_library_dir + "/example.yml")
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
- let(:new_interaction_1) { VCR::HTTPInteraction.new(VCR::Request.new, :response_1) }
408
- let(:new_interaction_2) { VCR::HTTPInteraction.new(VCR::Request.new, :response_2) }
409
- let(:new_interaction_3) { VCR::HTTPInteraction.new(VCR::Request.new, :response_3) }
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
- before(:each) do
426
- stub_matcher_for(old_interaction_1, :matcher_c)
427
- stub_matcher_for(old_interaction_2, :matcher_d)
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
- stub_matcher_for(new_interaction_1, :matcher_a)
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
- [new_interaction_1, new_interaction_2, new_interaction_3].each do |i|
435
- subject.record_http_interaction(i)
436
- end
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 'removes the old interactions that match new requests, and saves the new interactions follow the old ones' do
441
- subject.eject
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 "matches old requests to new ones using the cassette's match attributes" do
452
- pending("Need to fix this to work with Psych", :if => defined?(::Psych)) do
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 'saves the old interactions followed by the new ones to disk' do
465
- subject.eject
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