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
@@ -14,28 +14,25 @@ Feature: Usage with Shoulda
14
14
 
15
15
  Scenario: Use `VCR.insert_cassette` and `VCR.eject_cassette`
16
16
  Given a file named "test/test_server.rb" with:
17
- """
18
- require 'vcr_cucumber_helpers'
19
-
17
+ """ruby
20
18
  start_sinatra_app(:port => 7777) do
21
19
  get('/') { "Hello" }
22
20
  end
23
21
  """
24
22
  Given a file named "test/test_helper.rb" with:
25
- """
23
+ """ruby
26
24
  require 'test/test_server' if ENV['SERVER'] == 'true'
27
25
  require 'test/unit'
28
26
  require 'shoulda'
29
27
  require 'vcr'
30
28
 
31
- VCR.config do |c|
32
- c.stub_with :webmock
29
+ VCR.configure do |c|
30
+ c.hook_into :webmock
33
31
  c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
34
- c.default_cassette_options = { :record => :new_episodes }
35
32
  end
36
33
  """
37
34
  And a file named "test/vcr_example_test.rb" with:
38
- """
35
+ """ruby
39
36
  require 'test_helper'
40
37
 
41
38
  class VCRExampleTest < Test::Unit::TestCase
@@ -5,27 +5,24 @@ Feature: Usage with Test::Unit
5
5
 
6
6
  Scenario: Use `VCR.use_cassette` in a test
7
7
  Given a file named "test/test_server.rb" with:
8
- """
9
- require 'vcr_cucumber_helpers'
10
-
8
+ """ruby
11
9
  start_sinatra_app(:port => 7777) do
12
10
  get('/') { "Hello" }
13
11
  end
14
12
  """
15
13
  Given a file named "test/test_helper.rb" with:
16
- """
14
+ """ruby
17
15
  require 'test/test_server' if ENV['SERVER'] == 'true'
18
16
  require 'test/unit'
19
17
  require 'vcr'
20
18
 
21
- VCR.config do |c|
22
- c.stub_with :webmock
19
+ VCR.configure do |c|
20
+ c.hook_into :webmock
23
21
  c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
24
- c.default_cassette_options = { :record => :new_episodes }
25
22
  end
26
23
  """
27
24
  And a file named "test/vcr_example_test.rb" with:
28
- """
25
+ """ruby
29
26
  require 'test_helper'
30
27
 
31
28
  class VCRExampleTest < Test::Unit::TestCase
data/lib/vcr.rb CHANGED
@@ -1,44 +1,28 @@
1
- require 'vcr/util/regexes'
2
1
  require 'vcr/util/variable_args_block_caller'
3
2
  require 'vcr/util/yaml'
4
3
 
5
4
  require 'vcr/cassette'
6
- require 'vcr/config'
7
- require 'vcr/request_matcher'
5
+ require 'vcr/configuration'
6
+ require 'vcr/deprecations'
7
+ require 'vcr/errors'
8
+ require 'vcr/library_hooks'
9
+ require 'vcr/request_ignorer'
10
+ require 'vcr/request_matcher_registry'
8
11
  require 'vcr/version'
9
12
 
10
- require 'vcr/http_stubbing_adapters/common'
11
-
12
- require 'vcr/deprecations/cassette'
13
- require 'vcr/deprecations/config'
14
- require 'vcr/deprecations/http_stubbing_adapters/common'
15
-
16
- require 'vcr/structs/normalizers/body'
17
- require 'vcr/structs/normalizers/header'
18
- require 'vcr/structs/normalizers/status_message'
19
- require 'vcr/structs/normalizers/uri'
20
13
  require 'vcr/structs/http_interaction'
21
- require 'vcr/structs/request'
22
- require 'vcr/structs/response'
23
- require 'vcr/structs/response_status'
24
14
 
25
15
  module VCR
26
16
  include VariableArgsBlockCaller
17
+ include Errors
18
+
27
19
  extend self
28
20
 
29
- autoload :BasicObject, 'vcr/util/basic_object'
30
21
  autoload :CucumberTags, 'vcr/test_frameworks/cucumber'
31
22
  autoload :InternetConnection, 'vcr/util/internet_connection'
32
23
  autoload :RSpec, 'vcr/test_frameworks/rspec'
33
24
 
34
- LOCALHOST_ALIASES = %w( localhost 127.0.0.1 0.0.0.0 )
35
-
36
- class CassetteInUseError < StandardError; end
37
- class TurnedOffError < StandardError; end
38
-
39
25
  module Middleware
40
- autoload :CassetteArguments, 'vcr/middleware/cassette_arguments'
41
- autoload :Common, 'vcr/middleware/common'
42
26
  autoload :Faraday, 'vcr/middleware/faraday'
43
27
  autoload :Rack, 'vcr/middleware/rack'
44
28
  end
@@ -79,35 +63,44 @@ module VCR
79
63
  end
80
64
  end
81
65
 
82
- def config
83
- yield VCR::Config
84
- http_stubbing_adapter.check_version!
85
- http_stubbing_adapter.set_http_connections_allowed_to_default
86
- http_stubbing_adapter.ignored_hosts = VCR::Config.ignored_hosts
66
+ def http_interactions
67
+ return current_cassette.http_interactions if current_cassette
68
+ VCR::Cassette::HTTPInteractionList::NullList.new
87
69
  end
88
70
 
89
- def cucumber_tags(&block)
90
- main_object = eval('self', block.binding)
91
- yield VCR::CucumberTags.new(main_object)
71
+ def real_http_connections_allowed?
72
+ return current_cassette.recording? if current_cassette
73
+ configuration.allow_http_connections_when_no_cassette? || @turned_off
92
74
  end
93
75
 
94
- def http_stubbing_adapter
95
- @http_stubbing_adapter ||= begin
96
- if [:fakeweb, :webmock].all? { |l| VCR::Config.http_stubbing_libraries.include?(l) }
97
- raise ArgumentError.new("You have configured VCR to use both :fakeweb and :webmock. You cannot use both.")
98
- end
76
+ def request_matchers
77
+ @request_matchers ||= RequestMatcherRegistry.new
78
+ end
99
79
 
100
- adapters = VCR::Config.http_stubbing_libraries.map { |l| adapter_for(l) }
101
- raise ArgumentError.new("The http stubbing library is not configured.") if adapters.empty?
102
- adapter = HttpStubbingAdapters::MultiObjectProxy.for(*adapters)
103
- adapter.after_adapters_loaded
104
- adapter
105
- end
80
+ def request_ignorer
81
+ @request_ignorer ||= RequestIgnorer.new
82
+ end
83
+
84
+ def library_hooks
85
+ @library_hooks ||= LibraryHooks.new
86
+ end
87
+
88
+ def configuration
89
+ @configuration ||= Configuration.new
90
+ end
91
+
92
+ def configure
93
+ yield configuration
94
+ end
95
+
96
+ def cucumber_tags(&block)
97
+ main_object = eval('self', block.binding)
98
+ yield VCR::CucumberTags.new(main_object)
106
99
  end
107
100
 
108
101
  def record_http_interaction(interaction)
109
102
  return unless cassette = current_cassette
110
- return if VCR::Config.uri_should_be_ignored?(interaction.uri)
103
+ return if VCR.request_ignorer.ignore?(interaction.request)
111
104
 
112
105
  cassette.record_http_interaction(interaction)
113
106
  end
@@ -133,12 +126,10 @@ module VCR
133
126
  raise ArgumentError.new("You passed some invalid options: #{invalid_options.inspect}")
134
127
  end
135
128
 
136
- VCR.http_stubbing_adapter.http_connections_allowed = true
137
129
  @turned_off = true
138
130
  end
139
131
 
140
132
  def turn_on!
141
- VCR.http_stubbing_adapter.set_http_connections_allowed_to_default
142
133
  @turned_off = false
143
134
  end
144
135
 
@@ -150,18 +141,7 @@ module VCR
150
141
  @ignore_cassettes
151
142
  end
152
143
 
153
- private
154
-
155
- def adapter_for(lib)
156
- case lib
157
- when :excon; HttpStubbingAdapters::Excon
158
- when :fakeweb; HttpStubbingAdapters::FakeWeb
159
- when :faraday; HttpStubbingAdapters::Faraday
160
- when :typhoeus; HttpStubbingAdapters::Typhoeus
161
- when :webmock; HttpStubbingAdapters::WebMock
162
- else raise ArgumentError.new("#{lib.inspect} is not a supported HTTP stubbing library.")
163
- end
164
- end
144
+ private
165
145
 
166
146
  def cassettes
167
147
  @cassettes ||= []
data/lib/vcr/cassette.rb CHANGED
@@ -3,50 +3,43 @@ require 'erb'
3
3
  require 'set'
4
4
 
5
5
  require 'vcr/cassette/reader'
6
+ require 'vcr/cassette/http_interaction_list'
6
7
 
7
8
  module VCR
8
9
  class Cassette
9
10
  VALID_RECORD_MODES = [:all, :none, :new_episodes, :once]
10
11
 
11
- attr_reader :name, :record_mode, :match_requests_on, :erb, :re_record_interval, :tag
12
+ attr_reader :name, :record_mode, :match_requests_on, :erb, :re_record_interval, :tag, :http_interactions
12
13
 
13
14
  def initialize(name, options = {})
14
- options = VCR::Config.default_cassette_options.merge(options)
15
+ options = VCR.configuration.default_cassette_options.merge(options)
15
16
  invalid_options = options.keys - [
16
- :record,
17
- :erb,
18
- :allow_real_http,
19
- :match_requests_on,
20
- :re_record_interval,
21
- :tag,
22
- :update_content_length_header
17
+ :record, :erb, :match_requests_on, :re_record_interval, :tag,
18
+ :update_content_length_header, :allow_playback_repeats, :exclusive
23
19
  ]
24
20
 
25
21
  if invalid_options.size > 0
26
22
  raise ArgumentError.new("You passed the following invalid options to VCR::Cassette.new: #{invalid_options.inspect}.")
27
23
  end
28
24
 
29
- @name = name
30
- @record_mode = options[:record]
31
- @erb = options[:erb]
32
- @match_requests_on = options[:match_requests_on]
33
- @re_record_interval = options[:re_record_interval]
34
- @tag = options[:tag]
35
- @record_mode = :all if should_re_record?
25
+ @name = name
26
+ @record_mode = options[:record]
27
+ @erb = options[:erb]
28
+ @match_requests_on = options[:match_requests_on]
29
+ @re_record_interval = options[:re_record_interval]
30
+ @tag = options[:tag]
31
+ @record_mode = :all if should_re_record?
36
32
  @update_content_length_header = options[:update_content_length_header]
33
+ @allow_playback_repeats = options[:allow_playback_repeats]
34
+ @exclusive = options[:exclusive]
37
35
 
38
- deprecate_old_cassette_options(options)
39
36
  raise_error_unless_valid_record_mode
40
37
 
41
- set_http_connections_allowed
42
38
  load_recorded_interactions
43
39
  end
44
40
 
45
41
  def eject
46
42
  write_recorded_interactions_to_disk
47
- VCR.http_stubbing_adapter.restore_stubs_checkpoint(self)
48
- restore_http_connections_allowed
49
- restore_ignore_localhost_for_deprecation
50
43
  end
51
44
 
52
45
  def recorded_interactions
@@ -62,14 +55,22 @@ module VCR
62
55
  end
63
56
 
64
57
  def file
65
- File.join(VCR::Config.cassette_library_dir, "#{sanitized_name}.yml") if VCR::Config.cassette_library_dir
58
+ File.join(VCR.configuration.cassette_library_dir, "#{sanitized_name}.yml") if VCR.configuration.cassette_library_dir
66
59
  end
67
60
 
68
61
  def update_content_length_header?
69
62
  @update_content_length_header
70
63
  end
71
64
 
72
- private
65
+ def recording?
66
+ case record_mode
67
+ when :none; false
68
+ when :once; file.nil? || !File.size?(file)
69
+ else true
70
+ end
71
+ end
72
+
73
+ private
73
74
 
74
75
  def sanitized_name
75
76
  name.to_s.gsub(/[^\w\-\/]+/, '_')
@@ -88,14 +89,6 @@ module VCR
88
89
  InternetConnection.available?
89
90
  end
90
91
 
91
- def should_allow_http_connections?
92
- case record_mode
93
- when :none; false
94
- when :once; !File.size?(file)
95
- else true
96
- end
97
- end
98
-
99
92
  def should_stub_requests?
100
93
  record_mode != :all
101
94
  end
@@ -104,29 +97,14 @@ module VCR
104
97
  record_mode == :all
105
98
  end
106
99
 
107
- def set_http_connections_allowed
108
- @orig_http_connections_allowed = VCR.http_stubbing_adapter.http_connections_allowed?
109
- VCR.http_stubbing_adapter.http_connections_allowed = should_allow_http_connections?
110
- end
111
-
112
- def restore_http_connections_allowed
113
- VCR.http_stubbing_adapter.http_connections_allowed = @orig_http_connections_allowed
114
- end
115
-
116
100
  def load_recorded_interactions
117
- VCR.http_stubbing_adapter.create_stubs_checkpoint(self)
118
101
  if file && File.size?(file)
119
- begin
120
- interactions = VCR::YAML.load(raw_yaml_content)
121
- rescue TypeError, ArgumentError # Syck raises TypeError, Psych raises ArgumentError
122
- raise unless raw_yaml_content =~ /VCR::RecordedResponse/
123
- raise "The VCR cassette #{sanitized_name}.yml uses an old format that is now deprecated. VCR provides a rake task to migrate your old cassettes to the new format. See http://github.com/myronmarston/vcr/blob/master/CHANGELOG.md for more info."
124
- end
102
+ interactions = VCR::YAML.load(raw_yaml_content)
125
103
 
126
104
  invoke_hook(:before_playback, interactions)
127
105
 
128
106
  interactions.reject! do |i|
129
- i.request.uri.is_a?(String) && VCR::Config.uri_should_be_ignored?(i.request.uri)
107
+ i.request.uri.is_a?(String) && VCR.request_ignorer.ignore?(i.request)
130
108
  end
131
109
 
132
110
  if update_content_length_header?
@@ -136,9 +114,17 @@ module VCR
136
114
  recorded_interactions.replace(interactions)
137
115
  end
138
116
 
139
- if should_stub_requests?
140
- VCR.http_stubbing_adapter.stub_requests(recorded_interactions, match_requests_on)
141
- end
117
+ interactions = should_stub_requests? ? recorded_interactions : []
118
+
119
+ @http_interactions = HTTPInteractionList.new(
120
+ interactions,
121
+ match_requests_on,
122
+ @allow_playback_repeats,
123
+ parent_list)
124
+ end
125
+
126
+ def parent_list
127
+ @exclusive ? HTTPInteractionList::NullList.new : VCR.http_interactions
142
128
  end
143
129
 
144
130
  def raw_yaml_content
@@ -149,14 +135,9 @@ module VCR
149
135
  old_interactions = recorded_interactions
150
136
 
151
137
  if should_remove_matching_existing_interactions?
152
- match_attributes = match_requests_on
153
-
154
- new_request_matchers = Set.new new_recorded_interactions.map do |i|
155
- i.request.matcher(match_attributes)
156
- end
157
-
138
+ new_interaction_list = HTTPInteractionList.new(new_recorded_interactions, match_requests_on)
158
139
  old_interactions = old_interactions.reject do |i|
159
- new_request_matchers.include?(i.request.matcher(match_attributes))
140
+ new_interaction_list.has_interaction_matching?(i.request)
160
141
  end
161
142
  end
162
143
 
@@ -164,7 +145,7 @@ module VCR
164
145
  end
165
146
 
166
147
  def write_recorded_interactions_to_disk
167
- return unless VCR::Config.cassette_library_dir
148
+ return unless VCR.configuration.cassette_library_dir
168
149
  return if new_recorded_interactions.empty?
169
150
 
170
151
  interactions = merged_interactions
@@ -178,7 +159,7 @@ module VCR
178
159
 
179
160
  def invoke_hook(type, interactions)
180
161
  interactions.delete_if do |i|
181
- VCR::Config.invoke_hook(type, tag, i, self)
162
+ VCR.configuration.invoke_hook(type, tag, i, self)
182
163
  i.ignored?
183
164
  end
184
165
  end
@@ -0,0 +1,56 @@
1
+ module VCR
2
+ class Cassette
3
+ class HTTPInteractionList
4
+ class NullList
5
+ def response_for(*a); nil; end
6
+ def has_interaction_matching?(*a); false; end
7
+ end
8
+
9
+ attr_reader :interactions, :request_matchers, :allow_playback_repeats, :parent_list
10
+
11
+ def initialize(interactions, request_matchers, allow_playback_repeats = false, parent_list = NullList.new)
12
+ @interactions = interactions.dup
13
+ @request_matchers = request_matchers
14
+ @allow_playback_repeats = allow_playback_repeats
15
+ @parent_list = parent_list
16
+ @used_interactions = []
17
+ end
18
+
19
+ def response_for(request)
20
+ if index = matching_interaction_index_for(request)
21
+ interaction = @interactions.delete_at(index)
22
+ @used_interactions.unshift interaction
23
+ interaction.response
24
+ elsif interaction = matching_used_interaction_for(request)
25
+ interaction.response
26
+ else
27
+ @parent_list.response_for(request)
28
+ end
29
+ end
30
+
31
+ def has_interaction_matching?(request)
32
+ !!matching_interaction_index_for(request) ||
33
+ !!matching_used_interaction_for(request) ||
34
+ @parent_list.has_interaction_matching?(request)
35
+ end
36
+
37
+ private
38
+
39
+ def matching_interaction_index_for(request)
40
+ @interactions.index { |i| interaction_matches_request?(request, i) }
41
+ end
42
+
43
+ def matching_used_interaction_for(request)
44
+ return nil unless @allow_playback_repeats
45
+ @used_interactions.find { |i| interaction_matches_request?(request, i) }
46
+ end
47
+
48
+ def interaction_matches_request?(request, interaction)
49
+ @request_matchers.all? do |matcher|
50
+ VCR.request_matchers[matcher].matches?(request, interaction.request)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+