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,26 +1,36 @@
1
+ require 'typhoeus' if RUBY_INTERPRETER == :mri
2
+
1
3
  module MonkeyPatches
2
4
  extend self
3
5
 
4
6
  NET_HTTP_SINGLETON = class << Net::HTTP; self; end
5
7
 
6
- MONKEY_PATCHES = [
8
+ NET_HTTP_MONKEY_PATCHES = [
7
9
  [Net::BufferedIO, :initialize],
8
10
  [Net::HTTP, :request],
9
11
  [Net::HTTP, :connect],
10
12
  [NET_HTTP_SINGLETON, :socket_type]
11
13
  ]
12
14
 
15
+ ALL_MONKEY_PATCHES = NET_HTTP_MONKEY_PATCHES.dup
16
+
17
+ ALL_MONKEY_PATCHES << [Typhoeus::Hydra::Stubbing::SharedMethods, :find_stub_from_request] if RUBY_INTERPRETER == :mri
18
+
13
19
  def enable!(scope)
14
20
  case scope
15
21
  when :fakeweb
16
- realias_all :with_fakeweb
17
- enable!(:vcr) # fakeweb adapter relies upon VCR's Net::HTTP monkey patch
22
+ realias_net_http :with_fakeweb
23
+ enable!(:vcr) # fakeweb hook relies upon VCR's Net::HTTP monkey patch
18
24
  when :webmock
25
+ ::WebMock.reset!
19
26
  ::WebMock::HttpLibAdapters::NetHttpAdapter.enable!
20
- ::WebMock::HttpLibAdapters::TyphoeusAdapter.enable! unless RUBY_INTERPRETER == :jruby
27
+ ::WebMock::HttpLibAdapters::TyphoeusAdapter.enable! if RUBY_INTERPRETER == :mri
21
28
  $original_webmock_callbacks.each do |cb|
22
29
  ::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block])
23
30
  end
31
+ when :typhoeus
32
+ Typhoeus::Hydra.global_hooks = $original_typhoeus_hooks
33
+ realias Typhoeus::Hydra::Stubbing::SharedMethods, :find_stub_from_request, :with_vcr
24
34
  when :vcr
25
35
  realias Net::HTTP, :request, :with_vcr
26
36
  else raise ArgumentError.new("Unexpected scope: #{scope}")
@@ -34,12 +44,17 @@ module MonkeyPatches
34
44
  ::WebMock::HttpLibAdapters::NetHttpAdapter.disable!
35
45
  ::WebMock::HttpLibAdapters::TyphoeusAdapter.disable! unless RUBY_INTERPRETER == :jruby
36
46
  ::WebMock::CallbackRegistry.reset
47
+ ::WebMock::StubRegistry.instance.request_stubs = []
48
+ end
49
+
50
+ if defined?(::Typhoeus)
51
+ Typhoeus::Hydra.clear_global_hooks
37
52
  end
38
53
  end
39
54
 
40
55
  def init
41
56
  # capture the monkey patched definitions so we can realias to them in the future
42
- MONKEY_PATCHES.each do |mp|
57
+ ALL_MONKEY_PATCHES.each do |mp|
43
58
  capture_method_definition(mp.first, mp.last, false)
44
59
  end
45
60
  end
@@ -68,7 +83,7 @@ module MonkeyPatches
68
83
 
69
84
  # capture the original method definitions before the monkey patches have been defined
70
85
  # so we can realias to the originals in the future
71
- MONKEY_PATCHES.each do |mp|
86
+ ALL_MONKEY_PATCHES.each do |mp|
72
87
  capture_method_definition(mp.first, mp.last, true)
73
88
  end
74
89
 
@@ -81,7 +96,13 @@ module MonkeyPatches
81
96
  end
82
97
 
83
98
  def realias_all(alias_extension)
84
- MONKEY_PATCHES.each do |mp|
99
+ ALL_MONKEY_PATCHES.each do |mp|
100
+ realias mp.first, mp.last, alias_extension
101
+ end
102
+ end
103
+
104
+ def realias_net_http(alias_extension)
105
+ NET_HTTP_MONKEY_PATCHES.each do |mp|
85
106
  realias mp.first, mp.last, alias_extension
86
107
  end
87
108
  end
@@ -95,10 +116,17 @@ if RUBY_INTERPRETER == :mri
95
116
  require 'patron'
96
117
  require 'em-http-request'
97
118
  require 'curb'
98
- require 'typhoeus'
119
+
120
+ require 'vcr/library_hooks/typhoeus'
121
+ $original_typhoeus_hooks = Typhoeus::Hydra.global_hooks.dup
122
+
123
+ # define an alias that we can re-alias to in the future
124
+ Typhoeus::Hydra::Stubbing::SharedMethods.class_eval do
125
+ alias find_stub_from_request_with_vcr find_stub_from_request
126
+ end
99
127
  end
100
128
 
101
- require 'vcr/http_stubbing_adapters/fakeweb'
129
+ require 'vcr/library_hooks/fakeweb'
102
130
 
103
131
  # All Net::HTTP monkey patches have now been loaded, so capture the
104
132
  # appropriate method definitions so we can disable them later.
@@ -108,16 +136,17 @@ MonkeyPatches.init
108
136
  # subclasses Net::HTTP and inherits them...
109
137
  MonkeyPatches.disable_all!
110
138
 
111
- require 'vcr/http_stubbing_adapters/webmock'
139
+ require 'vcr/library_hooks/webmock'
112
140
  $original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks
113
141
 
114
142
  # disable all by default; we'll enable specific ones when we need them
115
143
  MonkeyPatches.disable_all!
116
144
 
117
145
  RSpec.configure do |config|
118
- [:fakeweb, :webmock, :vcr].each do |scope|
146
+ [:fakeweb, :webmock, :vcr, :typhoeus].each do |scope|
119
147
  config.before(:all, :with_monkey_patches => scope) { MonkeyPatches.enable!(scope) }
120
148
  config.after(:all, :with_monkey_patches => scope) { MonkeyPatches.disable_all! }
121
149
  end
122
150
  end
123
151
 
152
+ require 'vcr/library_hooks/excon'
data/spec/spec_helper.rb CHANGED
@@ -8,9 +8,6 @@ end
8
8
 
9
9
  require 'rspec'
10
10
 
11
- # Ruby 1.9.1 has a different yaml serialization format.
12
- YAML_SERIALIZATION_VERSION = RUBY_VERSION == '1.9.1' ? '1.9.1' : 'not_1.9.1'
13
-
14
11
  Dir['./spec/support/**/*.rb'].each { |f| require f }
15
12
 
16
13
  require 'vcr'
@@ -19,55 +16,27 @@ require 'monkey_patches'
19
16
  module VCR
20
17
  SPEC_ROOT = File.dirname(__FILE__)
21
18
 
22
- module Config
23
- def reset!(stubbing_lib = :fakeweb)
24
- self.default_cassette_options = { :record => :new_episodes }
25
-
26
- if stubbing_lib
27
- stub_with stubbing_lib
28
- else
29
- http_stubbing_libraries.clear
30
- end
31
-
32
- clear_hooks
33
- @ignored_hosts = []
34
-
35
- VCR.instance_eval do
36
- instance_variables.each { |ivar| remove_instance_variable(ivar) }
37
- end
38
- VCR.send(:initialize_ivars)
19
+ def reset!(hook = :fakeweb)
20
+ instance_variables.each do |ivar|
21
+ instance_variable_set(ivar, nil)
39
22
  end
23
+ configuration.hook_into hook if hook
40
24
  end
41
25
  end
42
26
 
43
27
  RSpec.configure do |config|
44
28
  config.color_enabled = true
45
- config.debug = (using_git && RUBY_INTERPRETER == :mri && !%w[ 1.9.1 1.9.3 ].include?(RUBY_VERSION) && !ENV['CI'])
29
+ config.debug = (using_git && RUBY_INTERPRETER == :mri && !%w[ 1.9.3 ].include?(RUBY_VERSION) && !ENV['CI'])
46
30
  config.treat_symbols_as_metadata_keys_with_true_values = true
47
31
 
48
32
  tmp_dir = File.expand_path('../../tmp/cassette_library_dir', __FILE__)
49
33
  config.before(:each) do
50
- VCR::Config.cassette_library_dir = tmp_dir
51
- VCR.turn_on! unless VCR.turned_on?
52
- VCR.eject_cassette while VCR.current_cassette
53
-
54
- VCR::Config.reset!
55
-
56
- WebMock.allow_net_connect!
57
- WebMock.reset!
58
-
59
- FakeWeb.allow_net_connect = true
60
- FakeWeb.clean_registry
61
-
62
- VCR::HttpStubbingAdapters::Faraday.reset!
63
- VCR::HttpStubbingAdapters::Excon.reset!
34
+ VCR.reset!
35
+ VCR.configuration.cassette_library_dir = tmp_dir
64
36
  end
65
37
 
66
38
  config.after(:each) do
67
39
  FileUtils.rm_rf tmp_dir
68
- VCR::HttpStubbingAdapters::Common.adapters.each do |a|
69
- a.ignored_hosts = []
70
- end
71
40
  end
72
41
 
73
42
  config.before(:all, :disable_warnings => true) do
@@ -85,8 +54,3 @@ RSpec.configure do |config|
85
54
  config.alias_it_should_behave_like_to :it_performs, 'it performs'
86
55
  end
87
56
 
88
- http_stubbing_dir = File.join(File.dirname(__FILE__), '..', 'lib', 'vcr', 'http_stubbing_adapters')
89
- Dir[File.join(http_stubbing_dir, '*.rb')].each do |file|
90
- next if RUBY_INTERPRETER != :mri && file =~ /(typhoeus)/
91
- require "vcr/http_stubbing_adapters/#{File.basename(file)}"
92
- end
@@ -137,7 +137,7 @@ HTTP_LIBRARY_ADAPTERS['excon'] = Module.new do
137
137
  end
138
138
 
139
139
  %w[ net_http typhoeus patron ].each do |_faraday_adapter|
140
- HTTP_LIBRARY_ADAPTERS["faraday-#{_faraday_adapter}"] = Module.new do
140
+ HTTP_LIBRARY_ADAPTERS["faraday (w/ #{_faraday_adapter})"] = Module.new do
141
141
  class << self; self; end.class_eval do
142
142
  define_method(:http_library_name) do
143
143
  "Faraday (#{_faraday_adapter})"
@@ -175,20 +175,10 @@ end
175
175
 
176
176
  def faraday_connection(url_root)
177
177
  Faraday::Connection.new(:url => url_root) do |builder|
178
- builder.use VCR::Middleware::Faraday do |cassette|
179
- cassette.name 'faraday_example'
180
-
181
- if respond_to?(:match_requests_on)
182
- cassette.options :match_requests_on => match_requests_on
183
- end
184
-
185
- if respond_to?(:record_mode)
186
- cassette.options :record => record_mode
187
- end
188
- end
189
-
178
+ builder.use VCR::Middleware::Faraday
190
179
  builder.adapter faraday_adapter
191
180
  end
192
181
  end
193
182
  end
194
183
  end
184
+
@@ -2,22 +2,20 @@ require 'cgi'
2
2
 
3
3
  NET_CONNECT_NOT_ALLOWED_ERROR = /You can use VCR to automatically record this request and replay it later/
4
4
 
5
- shared_examples_for "an http library" do |library, supported_request_match_attributes, *other|
5
+ shared_examples_for "a hook into an HTTP library" do |library, *other|
6
6
  unless adapter_module = HTTP_LIBRARY_ADAPTERS[library]
7
7
  raise ArgumentError.new("No http library adapter module could be found for #{library}")
8
8
  end
9
9
 
10
- http_lib_unsupported = if RUBY_INTERPRETER == :mri
11
- # em-http-request is causing issues on 1.8.6 on travis like:
12
- # ruby: symbol lookup error: /home/travis/.rvm/gems/ruby-1.8.6-p420/gems/em-http-request-0.3.0/lib/http11_client.so: undefined symbol: rb_hash_lookup
13
- library =~ /em-http/ && RUBY_VERSION == '1.8.6' && ENV['TRAVIS']
14
- else
15
- library =~ /(typhoeus|curb|patron|em-http)/
16
- end
10
+ http_lib_unsupported = (RUBY_INTERPRETER != :mri && library =~ /(typhoeus|curb|patron|em-http)/)
17
11
 
18
12
  describe "using #{adapter_module.http_library_name}", :unless => http_lib_unsupported do
19
13
  include adapter_module
20
14
 
15
+ def stub_requests(*args)
16
+ VCR.stub(:http_interactions => VCR::Cassette::HTTPInteractionList.new(*args))
17
+ end
18
+
21
19
  # Necessary for ruby 1.9.2. On 1.9.2 we get an error when we use super,
22
20
  # so this gives us another alias we can use for the original method.
23
21
  alias make_request make_http_request
@@ -60,11 +58,9 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
60
58
  let(:status) { VCR::ResponseStatus.new(200, 'OK') }
61
59
  let(:interaction) { VCR::HTTPInteraction.new(request, response) }
62
60
  let(:response_body) { "The response body" }
63
- let(:match_requests_on) { [:method, :uri] }
64
- let(:record_mode) { :none }
65
61
 
66
62
  before(:each) do
67
- subject.stub_requests([interaction], match_requests_on)
63
+ stub_requests([interaction], [:method, :uri])
68
64
  end
69
65
 
70
66
  context "when the the stubbed request and response has no headers" do
@@ -98,35 +94,24 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
98
94
  end
99
95
 
100
96
  describe '.stub_requests using specific match_attributes' do
101
- before(:each) { subject.http_connections_allowed = false }
102
- let(:interactions) { VCR::YAML.load_file(File.join(VCR::SPEC_ROOT, 'fixtures', YAML_SERIALIZATION_VERSION, 'match_requests_on.yml')) }
97
+ before(:each) { VCR.stub(:real_http_connections_allowed? => false) }
98
+ let(:interactions) { VCR::YAML.load_file(File.join(VCR::SPEC_ROOT, 'fixtures', 'match_requests_on.yml')) }
103
99
 
104
- @supported_request_match_attributes = supported_request_match_attributes
105
100
  def self.matching_on(attribute, valid, invalid, &block)
106
- supported_request_match_attributes = @supported_request_match_attributes
107
-
108
101
  describe ":#{attribute}" do
109
- let(:perform_stubbing) { subject.stub_requests(interactions, match_requests_on) }
110
- let(:match_requests_on) { [attribute] }
111
- let(:record_mode) { :none }
102
+ let(:perform_stubbing) { stub_requests(interactions, [attribute]) }
112
103
 
113
- if supported_request_match_attributes.include?(attribute)
114
- before(:each) { perform_stubbing }
115
- module_eval(&block)
104
+ before(:each) { perform_stubbing }
105
+ module_eval(&block)
116
106
 
117
- valid.each do |val, response|
118
- it "returns the expected response for a #{val.inspect} request" do
119
- get_body_string(make_http_request(val)).should eq(response)
120
- end
107
+ valid.each do |val, response|
108
+ it "returns the expected response for a #{val.inspect} request" do
109
+ get_body_string(make_http_request(val)).should eq(response)
121
110
  end
111
+ end
122
112
 
123
- it "raises an error for a request with a different #{attribute}" do
124
- expect { make_http_request(invalid) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
125
- end
126
- else
127
- it 'raises an error indicating matching requests on this attribute is not supported' do
128
- expect { perform_stubbing }.to raise_error(/does not support matching requests on #{attribute}/)
129
- end
113
+ it "raises an error for a request with a different #{attribute}" do
114
+ expect { make_http_request(invalid) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
130
115
  end
131
116
  end
132
117
  end
@@ -185,11 +170,12 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
185
170
  interaction
186
171
  end
187
172
 
188
- it 'does not record the request if the adapter is disabled' do
189
- subject.stub(:enabled?).and_return(false)
190
- VCR.should_not_receive(:record_http_interaction)
191
- make_http_request(:get, url)
192
- end
173
+ it 'does not record the request if the hook is disabled' do
174
+ VCR.library_hooks.exclusively_enabled :something_else do
175
+ VCR.should_not_receive(:record_http_interaction)
176
+ make_http_request(:get, url)
177
+ end
178
+ end unless other.include?(:not_disableable)
193
179
 
194
180
  it 'records the request uri' do
195
181
  recorded_interaction.request.uri.should eq(url)
@@ -224,8 +210,6 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
224
210
  end
225
211
  end
226
212
  else
227
- let(:record_mode) { :none }
228
-
229
213
  it 'does not allow real HTTP requests or record them' do
230
214
  VCR.should_receive(:record_http_interaction).never
231
215
  expect { make_http_request(:get, url) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
@@ -233,74 +217,36 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
233
217
  end
234
218
  end
235
219
 
236
- def test_request_stubbed(method, url, expected)
237
- subject.request_stubbed?(VCR::Request.new(method, url), [:method, :uri]).should eq(expected)
238
- end
239
-
240
- it "returns false from #http_connections_allowed? when http_connections_allowed is set to nil" do
241
- subject.http_connections_allowed = nil
242
- subject.http_connections_allowed?.should eq(false)
243
- end
244
-
245
- describe '.restore_stubs_checkpoint' do
246
- it 'raises an appropriate error when there is no matching checkpoint' do
247
- expect {
248
- subject.restore_stubs_checkpoint(:some_crazy_checkpoint_that_doesnt_exist)
249
- }.to raise_error(ArgumentError, /no checkpoint .* could be found/i)
250
- end
251
- end
252
-
253
220
  [true, false].each do |http_allowed|
254
- context "when http_connections_allowed is set to #{http_allowed}" do
255
- before(:each) { subject.http_connections_allowed = http_allowed }
256
-
257
- it "returns #{http_allowed} for #http_connections_allowed?" do
258
- subject.http_connections_allowed?.should eq(http_allowed)
259
- end
221
+ context "when VCR.real_http_connections_allowed? is returning #{http_allowed}" do
222
+ before(:each) { VCR.stub(:real_http_connections_allowed? => http_allowed) }
260
223
 
261
224
  test_real_http_request(http_allowed, *other)
262
225
 
263
226
  unless http_allowed
264
227
  localhost_response = "Localhost response"
265
228
 
266
- describe '.ignore_hosts' do
267
- let(:record_mode) { :none }
268
-
269
- context 'when set to ["127.0.0.1", "localhost"]' do
270
- before(:each) do
271
- subject.ignored_hosts = ["127.0.0.1", "localhost"]
272
- end
229
+ context 'when ignore_hosts is configured to "127.0.0.1", "localhost"' do
230
+ before(:each) do
231
+ VCR.configure { |c| c.ignore_hosts "127.0.0.1", "localhost" }
232
+ end
273
233
 
274
- %w[ 127.0.0.1 localhost ].each do |localhost_alias|
275
- it "allows requests to #{localhost_alias}" do
276
- get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test")).should eq(localhost_response)
277
- end
234
+ %w[ 127.0.0.1 localhost ].each do |localhost_alias|
235
+ it "allows requests to #{localhost_alias}" do
236
+ get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test")).should eq(localhost_response)
278
237
  end
238
+ end
279
239
 
280
- it 'does not allow requests to 0.0.0.0' do
281
- expect { make_http_request(:get, "http://0.0.0.0:#{VCR::SinatraApp.port}/localhost_test") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
282
- end
240
+ it 'does not allow requests to 0.0.0.0' do
241
+ expect { make_http_request(:get, "http://0.0.0.0:#{VCR::SinatraApp.port}/localhost_test") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
283
242
  end
284
243
  end
285
244
  end
286
245
 
287
- context 'when some requests are stubbed, after setting a checkpoint' do
246
+ context 'when some requests are stubbed' do
288
247
  before(:each) do
289
- subject.create_stubs_checkpoint(:my_checkpoint)
290
- @recorded_interactions = VCR::YAML.load_file(File.join(VCR::SPEC_ROOT, 'fixtures', YAML_SERIALIZATION_VERSION, 'fake_example.com_responses.yml'))
291
- subject.stub_requests(@recorded_interactions, VCR::RequestMatcher::DEFAULT_MATCH_ATTRIBUTES)
292
- end
293
-
294
- if other.include?(:needs_net_http_extension)
295
- it 'returns true from #request_stubbed? for the requests that are stubbed' do
296
- test_request_stubbed(:post, 'http://example.com', true)
297
- test_request_stubbed(:get, 'http://example.com/foo', true)
298
- end
299
-
300
- it 'returns false from #request_stubbed? for requests that are not stubbed' do
301
- test_request_stubbed(:post, 'http://example.com/foo', false)
302
- test_request_stubbed(:get, 'http://google.com', false)
303
- end
248
+ @recorded_interactions = VCR::YAML.load_file(File.join(VCR::SPEC_ROOT, 'fixtures', 'fake_example.com_responses.yml'))
249
+ stub_requests(@recorded_interactions, VCR::RequestMatcherRegistry::DEFAULT_MATCHERS)
304
250
  end
305
251
 
306
252
  it 'gets the stubbed responses when requests are made to http://example.com/foo, and does not record them' do
@@ -311,10 +257,6 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
311
257
  it 'rotates through multiple responses for the same request' do
312
258
  get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 1 with path=foo')
313
259
  get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 2 with path=foo')
314
-
315
- # subsequent requests keep getting the last one
316
- get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 2 with path=foo')
317
- get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 2 with path=foo')
318
260
  end unless other.include?(:does_not_support_rotating_responses)
319
261
 
320
262
  it "correctly handles stubbing multiple values for the same header" do
@@ -322,20 +264,6 @@ shared_examples_for "an http library" do |library, supported_request_match_attri
322
264
  header = header.split(', ') if header.respond_to?(:split)
323
265
  header.should =~ ['bar=bazz', 'foo=bar']
324
266
  end
325
-
326
- context 'when we restore our previous check point' do
327
- before(:each) { subject.restore_stubs_checkpoint(:my_checkpoint) }
328
-
329
- test_real_http_request(http_allowed, *other)
330
-
331
- if other.include?(:needs_net_http_extension)
332
- it 'returns false from #request_stubbed?' do
333
- test_request_stubbed(:get, 'http://example.com/foo', false)
334
- test_request_stubbed(:post, 'http://example.com', false)
335
- test_request_stubbed(:get, 'http://google.com', false)
336
- end
337
- end
338
- end
339
267
  end
340
268
  end
341
269
  end