vcr 0.4.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +34 -0
- data/README.md +89 -21
- data/lib/vcr.rb +12 -1
- data/lib/vcr/cassette.rb +50 -27
- data/lib/vcr/config.rb +9 -0
- data/lib/vcr/deprecations.rb +15 -42
- data/lib/vcr/extensions/net_http.rb +13 -20
- data/lib/vcr/extensions/net_http_response.rb +6 -13
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +21 -3
- data/lib/vcr/http_stubbing_adapters/webmock.rb +34 -4
- data/lib/vcr/version.rb +16 -0
- metadata +90 -108
- data/.document +0 -5
- data/.gitignore +0 -22
- data/Rakefile +0 -67
- data/VERSION +0 -1
- data/benchmarks/http_stubbing_libraries.rb +0 -59
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette1.yml +0 -43
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette2.yml +0 -47
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette3.yml +0 -85
- data/features/fixtures/vcr_cassettes/1.8.6/nested_replay_cassette.yml +0 -24
- data/features/fixtures/vcr_cassettes/1.8.6/not_the_real_response.yml +0 -43
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette1.yml +0 -43
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette2.yml +0 -47
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette3.yml +0 -85
- data/features/fixtures/vcr_cassettes/1.8.7/nested_replay_cassette.yml +0 -24
- data/features/fixtures/vcr_cassettes/1.8.7/not_the_real_response.yml +0 -43
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette1.yml +0 -43
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette2.yml +0 -63
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette3.yml +0 -85
- data/features/fixtures/vcr_cassettes/1.9.1/nested_replay_cassette.yml +0 -32
- data/features/fixtures/vcr_cassettes/1.9.1/not_the_real_response.yml +0 -43
- data/features/record_response.feature +0 -77
- data/features/replay_recorded_response.feature +0 -69
- data/features/step_definitions/vcr_steps.rb +0 -144
- data/features/support/env.rb +0 -64
- data/lib/vcr/extensions/fake_web.rb +0 -20
- data/lib/vcr/extensions/net_read_adapter.rb +0 -22
- data/spec/cassette_spec.rb +0 -213
- data/spec/config_spec.rb +0 -49
- data/spec/cucumber_tags_spec.rb +0 -71
- data/spec/deprecations_spec.rb +0 -67
- data/spec/extensions/net_http_response_spec.rb +0 -86
- data/spec/extensions/net_http_spec.rb +0 -72
- data/spec/extensions/net_read_adapter_spec.rb +0 -10
- data/spec/fixtures/1.8.6/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/1.8.6/cassette_spec/example.yml +0 -110
- data/spec/fixtures/1.8.6/example_net_http.yml +0 -14
- data/spec/fixtures/1.8.6/example_net_http_request.yml +0 -12
- data/spec/fixtures/1.8.6/example_net_http_response.yml +0 -25
- data/spec/fixtures/1.8.6/fake_example.com_responses.yml +0 -90
- data/spec/fixtures/1.8.7/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/1.8.7/cassette_spec/example.yml +0 -110
- data/spec/fixtures/1.8.7/example_net_http.yml +0 -14
- data/spec/fixtures/1.8.7/example_net_http_request.yml +0 -12
- data/spec/fixtures/1.8.7/example_net_http_response.yml +0 -25
- data/spec/fixtures/1.8.7/fake_example.com_responses.yml +0 -90
- data/spec/fixtures/1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/1.9.1/cassette_spec/example.yml +0 -110
- data/spec/fixtures/1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/1.9.1/example_net_http_response.yml +0 -25
- data/spec/fixtures/1.9.1/fake_example.com_responses.yml +0 -90
- data/spec/http_stubbing_adapters/fakeweb_spec.rb +0 -24
- data/spec/http_stubbing_adapters/webmock_spec.rb +0 -23
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -35
- data/spec/structs_spec.rb +0 -95
- data/spec/support/deprecated.rb +0 -18
- data/spec/support/disable_warnings.rb +0 -12
- data/spec/support/http_stubbing_adapter.rb +0 -113
- data/spec/support/temp_cassette_library_dir.rb +0 -16
- data/spec/task_runner_spec.rb +0 -59
- data/spec/vcr_spec.rb +0 -119
- data/vcr.gemspec +0 -147
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,39 @@
|
|
1
1
|
#Changelog
|
2
2
|
|
3
|
+
## 1.0.0 (June 22, 2010)
|
4
|
+
|
5
|
+
* New Features
|
6
|
+
* Added support for [HTTPClient](http://github.com/nahi/httpclient), [Patron](http://github.com/toland/patron) and
|
7
|
+
[em-http-request](http://github.com/igrigorik/em-http-request) when WebMock is used. Any future http libraries
|
8
|
+
WebMock supports should (theoretically, at least) work without any VCR code changes. Thanks to
|
9
|
+
[Bartosz Blimke](http://github.com/bblimke) for adding the necessary code to WebMock to make this happen!
|
10
|
+
* Added support for dynamic responses using ERB. A cassette will be evaluated as ERB before the YAML
|
11
|
+
is deserialized if you pass it an `:erb => true` option. You can pass variables using
|
12
|
+
`:erb => { :var1 => 'some value', :var2 => 'another value' }`.
|
13
|
+
* Added `ignore_localhost` configuration setting, which defaults to false. Setting it true does the following:
|
14
|
+
* Localhost requests will proceed as normal. The "Real HTTP connections are disabled" error will not occur.
|
15
|
+
* Localhost requests will not be recorded.
|
16
|
+
* Previously recorded localhost requests will not be replayed.
|
17
|
+
* Exposed the version number:
|
18
|
+
* `VCR.version` => string (in the format "major.minor.patch")
|
19
|
+
* `VCR.version.parts` => array of integers
|
20
|
+
* `VCR.version.major` => integer
|
21
|
+
* `VCR.version.minor` => integer
|
22
|
+
* `VCR.version.patch` => integer
|
23
|
+
* Added test coverage and documentation of using a regex for non-deterministic URLs (i.e. URLs that include
|
24
|
+
a timestamp as a query parameter). It turns out this feature worked before, and I just didn't realize it :).
|
25
|
+
|
26
|
+
* Breaking Changes
|
27
|
+
* The `:allow_real_http => lambda { |uri| ... }` cassette option has been removed. There was no way to get
|
28
|
+
this to work with the newly supported http libraries without extensive monkeypatching, and it was mostly
|
29
|
+
useful for localhost requests, which is more easily handled by the new `ignore_localhost` config setting.
|
30
|
+
* Removed methods and options that had been previously deprecated. If you're upgrading from an old version,
|
31
|
+
I recommend upgrading to 0.4.1 first, deal with all the deprecation warnings, then upgrade to 1.0.0.
|
32
|
+
|
33
|
+
* Misc Changes:
|
34
|
+
* Removed dependency on [jeweler](http://github.com/technicalpickles/jeweler). Manage the gemspec by hand instead.
|
35
|
+
* Removed some extensions that are no longer necessary.
|
36
|
+
|
3
37
|
## 0.4.1 May 11, 2010
|
4
38
|
* Fixed a bug: when `Net::HTTPResponse#read_body` was called after VCR had read the body to record a new request,
|
5
39
|
it raised an error (`IOError: Net::HTTPResponse#read_body called twice`). My fix extends Net::HTTPResponse
|
data/README.md
CHANGED
@@ -38,10 +38,25 @@ will replay the response from example.com when the http request is made. This t
|
|
38
38
|
made anymore), deterministic (the test will continue to pass, even if you are offline, or example.com goes down for
|
39
39
|
maintenance) and accurate (the response from example.com will contain the same headers and body you get from a real request).
|
40
40
|
|
41
|
+
## Features
|
42
|
+
|
43
|
+
* Automatically records and replays your HTTP interactions with minimal setup/configuration code.
|
44
|
+
* Supports multiple HTTP stubbing libraries. Currently FakeWeb and WebMock are supported, and the adapter interface
|
45
|
+
is easy to implement for any future libraries.
|
46
|
+
* Supports multiple HTTP libraries: [Net::HTTP](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html)
|
47
|
+
(all HTTP stubbing libraries), [Patron](http://github.com/toland/patron) (WebMock only),
|
48
|
+
[HTTPClient](http://github.com/nahi/httpclient) (WebMock only) and
|
49
|
+
[em-http-request](http://github.com/igrigorik/em-http-request) (WebMock only).
|
50
|
+
* The same request can receive different responses in different tests--just use different cassettes.
|
51
|
+
* The recorded requests and responses are stored on disk as YAML and can easily be inspected and edited.
|
52
|
+
* Dynamic responses are supported using ERB.
|
53
|
+
* Disables all HTTP requests that you don't explicitly allow.
|
54
|
+
* Simple cucumber integration is provided using tags.
|
55
|
+
|
41
56
|
## Cassettes
|
42
57
|
|
43
|
-
Cassettes are the medium to which VCR records HTTP interactions, and the medium from which it replays them.
|
44
|
-
While a cassette is in use, new HTTP requests (or "new episodes", as VCR calls them) either get
|
58
|
+
Cassettes are the medium to which VCR records HTTP interactions, and the medium from which it replays them.
|
59
|
+
While a cassette is in use, new HTTP requests (or "new episodes", as VCR calls them) either get
|
45
60
|
recorded, or an error will be raised, depending on the cassette's `:record` mode (see below). When you use
|
46
61
|
a cassette that contains previously recorded HTTP interactions, it registers them with the http stubbing
|
47
62
|
library of your choice (fakeweb or webmock) so that HTTP requests get the recorded response. Between test
|
@@ -50,19 +65,7 @@ runs, cassettes are stored on disk as YAML files in your configured cassette lib
|
|
50
65
|
Each cassette can be configured with a couple options:
|
51
66
|
|
52
67
|
* `:record`: Specifies a record mode for this cassette.
|
53
|
-
* `:
|
54
|
-
|
55
|
-
For example, to prevent the VCR cassette from recording and replaying requests to google.com, you could use:
|
56
|
-
|
57
|
-
lambda { |uri| uri.host == 'google.com' }
|
58
|
-
|
59
|
-
All non-google requests would be recorded/replayed as normal. You can also set this to `:localhost`,
|
60
|
-
which is syntactic sugar for:
|
61
|
-
|
62
|
-
lambda { |uri| uri.host == 'localhost' }
|
63
|
-
|
64
|
-
This is particularly useful for using VCR with [capybara](http://github.com/jnicklas/capybara)
|
65
|
-
and any of its javascript drivers (see below for more info).
|
68
|
+
* `:erb`: Used for dynamic cassettes (see below for more details).
|
66
69
|
|
67
70
|
## Record modes
|
68
71
|
|
@@ -80,6 +83,7 @@ and a per-cassette record mode when inserting a cassette. The record modes are:
|
|
80
83
|
VCR.config do |c|
|
81
84
|
c.cassette_library_dir = File.join(Rails.root, 'features', 'fixtures', 'cassette_library')
|
82
85
|
c.http_stubbing_library = :fakeweb
|
86
|
+
c.ignore_localhost = true
|
83
87
|
c.default_cassette_options = { :record => :none }
|
84
88
|
end
|
85
89
|
|
@@ -92,7 +96,11 @@ configuration options:
|
|
92
96
|
This is currently optional--VCR will try to guess based on the presence or absence of the `FakeWeb` or `WebMock`
|
93
97
|
constants, but this is mostly done to assist users upgrading from VCR 0.3.1, which only worked with fakeweb and
|
94
98
|
didn't have this option. I recommend you explicitly configure this.
|
95
|
-
* `
|
99
|
+
* `ignore_localhost`: Defaults to false. Setting it true does the following:
|
100
|
+
* Localhost requests will proceed as normal. The "Real HTTP connections are disabled" error will not occur.
|
101
|
+
* Localhost requests will not be recorded.
|
102
|
+
* Previously recorded localhost requests will not be replayed.
|
103
|
+
* `default_cassette_options`: The default options for your cassettes. These will be overridden by any options you
|
96
104
|
set on each individual cassette.
|
97
105
|
|
98
106
|
## Usage with your favorite ruby test/spec framework
|
@@ -147,8 +155,69 @@ If the last argument to `#tags` is a hash, VCR will use it as the options for th
|
|
147
155
|
|
148
156
|
When you use any of the javascript-enabled drivers (selenium, celerity, culerity) with
|
149
157
|
[capybara](http://github.com/jnicklas/capybara), it'll need to ping the app running on localhost.
|
150
|
-
Set the
|
151
|
-
|
158
|
+
Set the `ignore_localhost` option to true to allow this.
|
159
|
+
|
160
|
+
## Cassette Customization
|
161
|
+
|
162
|
+
Cassettes are stored as simple plain text YAML files and can easily be edited to suit your needs. One common need
|
163
|
+
is for a particular request to be stubbed using a regex rather than the raw URL. This is handy for URLs that contain
|
164
|
+
non-deterministic portions (such as timestamps)--since the URL will be a bit different each time, the URL from the
|
165
|
+
recorded request will not match the URL for future requests. You can simply change the URL to the YAML of the
|
166
|
+
appropriate regex.
|
167
|
+
|
168
|
+
Figure out the yaml in irb:
|
169
|
+
|
170
|
+
>> require 'yaml'
|
171
|
+
=> true
|
172
|
+
>> puts /example\.com\/\d+/.to_yaml
|
173
|
+
--- !ruby/regexp /example\.com\/\d+/
|
174
|
+
|
175
|
+
Edit your cassette file:
|
176
|
+
|
177
|
+
request: !ruby/struct:VCR::Request
|
178
|
+
method: :get
|
179
|
+
uri: !ruby/regexp /example\.com\/\d+/
|
180
|
+
body:
|
181
|
+
headers:
|
182
|
+
|
183
|
+
## Dynamic Cassettes
|
184
|
+
|
185
|
+
VCR's default recording and replaying is static. The exact response that is initially recorded will
|
186
|
+
be replayed for all future requests. Usually this is fine, but in some cases you need something more
|
187
|
+
dynamic. You can use [ERB](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/) for this.
|
188
|
+
|
189
|
+
Enable ERB evaluation of a cassette using the `:erb` option:
|
190
|
+
|
191
|
+
VCR.use_cassette('user-subscription', :erb => :true) do
|
192
|
+
# do something that makes an HTTP request
|
193
|
+
end
|
194
|
+
|
195
|
+
You can use variables in your cassette's ERB by passing a hash:
|
196
|
+
|
197
|
+
VCR.use_cassette('user-subscription', :erb => { :user => User.last }) do
|
198
|
+
# do something that makes an HTTP request
|
199
|
+
end
|
200
|
+
|
201
|
+
In your cassette:
|
202
|
+
|
203
|
+
request: !ruby/struct:VCR::Request
|
204
|
+
method: :get
|
205
|
+
uri: http://some-domain.com:80/users/<%= user.id %>
|
206
|
+
body:
|
207
|
+
headers:
|
208
|
+
...
|
209
|
+
response: !ruby/struct:VCR::Response
|
210
|
+
...
|
211
|
+
body: Hello, <%= user.name %>!
|
212
|
+
|
213
|
+
## FakeWeb or WebMock?
|
214
|
+
|
215
|
+
VCR works fine with either FakeWeb or WebMock. Overall, WebMock has more features, and you'll need to use
|
216
|
+
WebMock if you want to use VCR with an HTTP library besides `Net::HTTP`. However, FakeWeb is currently
|
217
|
+
about three times faster than WebMock, so you may want to stick with FakeWeb if you don't need WebMock's
|
218
|
+
additional features. You can see the
|
219
|
+
[benchmarks](http://github.com/myronmarston/vcr/blob/master/benchmarks/http_stubbing_libraries.rb) for
|
220
|
+
more details.
|
152
221
|
|
153
222
|
## Suggested Workflow
|
154
223
|
|
@@ -183,8 +252,7 @@ that it is making a new HTTP request, you'll be notified by an error as shown ab
|
|
183
252
|
|
184
253
|
## Ruby Version Compatibility
|
185
254
|
|
186
|
-
VCR
|
187
|
-
[1.9.1](http://integrity191.heroku.com/vcr).
|
255
|
+
VCR has been tested on ruby 1.8.6, 1.8.7 and 1.9.1.
|
188
256
|
|
189
257
|
## Notes, etc.
|
190
258
|
|
@@ -214,7 +282,7 @@ VCR works on ruby [1.8.6](http://integrity186.heroku.com/vcr), [1.8.7](http://in
|
|
214
282
|
## Thanks
|
215
283
|
|
216
284
|
* [Aslak Hellesøy](http://github.com/aslakhellesoy) for [Cucumber](http://github.com/aslakhellesoy/cucumber).
|
217
|
-
* [Bartosz Blimke](http://github.com/bblimke) for [WebMock](http://github.com/bblimke/webmock)
|
285
|
+
* [Bartosz Blimke](http://github.com/bblimke) for [WebMock](http://github.com/bblimke/webmock).
|
218
286
|
* [Chris Kampmeier](http://github.com/chrisk) for [FakeWeb](http://github.com/chrisk/fakeweb).
|
219
287
|
* [Chris Young](http://github.com/chrisyoung) for [NetRecorder](http://github.com/chrisyoung/netrecorder),
|
220
288
|
the inspiration for VCR.
|
data/lib/vcr.rb
CHANGED
@@ -3,15 +3,17 @@ require 'vcr/config'
|
|
3
3
|
require 'vcr/cucumber_tags'
|
4
4
|
require 'vcr/deprecations'
|
5
5
|
require 'vcr/structs'
|
6
|
+
require 'vcr/version'
|
6
7
|
|
7
8
|
require 'vcr/extensions/net_http_response'
|
8
|
-
require 'vcr/extensions/net_read_adapter'
|
9
9
|
|
10
10
|
require 'vcr/http_stubbing_adapters/base'
|
11
11
|
|
12
12
|
module VCR
|
13
13
|
extend self
|
14
14
|
|
15
|
+
LOCALHOST_ALIASES = %w( localhost 127.0.0.1 )
|
16
|
+
|
15
17
|
def current_cassette
|
16
18
|
cassettes.last
|
17
19
|
end
|
@@ -39,6 +41,7 @@ module VCR
|
|
39
41
|
yield VCR::Config
|
40
42
|
http_stubbing_adapter.check_version!
|
41
43
|
http_stubbing_adapter.http_connections_allowed = false
|
44
|
+
http_stubbing_adapter.ignore_localhost = VCR::Config.ignore_localhost?
|
42
45
|
end
|
43
46
|
|
44
47
|
def cucumber_tags(&block)
|
@@ -57,6 +60,14 @@ module VCR
|
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
63
|
+
def record_http_interaction(interaction)
|
64
|
+
return unless cassette = current_cassette
|
65
|
+
return if http_stubbing_adapter.ignore_localhost? &&
|
66
|
+
LOCALHOST_ALIASES.include?(URI.parse(interaction.uri).host)
|
67
|
+
|
68
|
+
cassette.record_http_interaction(interaction)
|
69
|
+
end
|
70
|
+
|
60
71
|
private
|
61
72
|
|
62
73
|
def cassettes
|
data/lib/vcr/cassette.rb
CHANGED
@@ -1,18 +1,23 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
require 'yaml'
|
3
|
+
require 'erb'
|
3
4
|
|
4
5
|
module VCR
|
5
6
|
class Cassette
|
7
|
+
class MissingERBVariableError < NameError; end
|
8
|
+
|
6
9
|
VALID_RECORD_MODES = [:all, :none, :new_episodes].freeze
|
7
10
|
|
8
11
|
attr_reader :name, :record_mode
|
9
12
|
|
10
13
|
def initialize(name, options = {})
|
14
|
+
options = VCR::Config.default_cassette_options.merge(options)
|
15
|
+
|
11
16
|
@name = name
|
12
|
-
@record_mode = options[:record]
|
13
|
-
@
|
17
|
+
@record_mode = options[:record]
|
18
|
+
@erb = options[:erb]
|
14
19
|
|
15
|
-
|
20
|
+
deprecate_old_cassette_options(options)
|
16
21
|
raise_error_unless_valid_record_mode(record_mode)
|
17
22
|
|
18
23
|
set_http_connections_allowed
|
@@ -23,6 +28,7 @@ module VCR
|
|
23
28
|
write_recorded_interactions_to_disk
|
24
29
|
VCR.http_stubbing_adapter.restore_stubs_checkpoint(name)
|
25
30
|
restore_http_connections_allowed
|
31
|
+
restore_ignore_localhost_for_deprecation
|
26
32
|
end
|
27
33
|
|
28
34
|
def recorded_interactions
|
@@ -34,15 +40,15 @@ module VCR
|
|
34
40
|
end
|
35
41
|
|
36
42
|
def file
|
37
|
-
File.join(VCR::Config.cassette_library_dir, "#{
|
38
|
-
end
|
39
|
-
|
40
|
-
def allow_real_http_requests_to?(uri)
|
41
|
-
@allow_real_http_lambda ? @allow_real_http_lambda.call(uri) : false
|
43
|
+
File.join(VCR::Config.cassette_library_dir, "#{sanitized_name}.yml") if VCR::Config.cassette_library_dir
|
42
44
|
end
|
43
45
|
|
44
46
|
private
|
45
47
|
|
48
|
+
def sanitized_name
|
49
|
+
name.to_s.gsub(/[^\w\-\/]+/, '_')
|
50
|
+
end
|
51
|
+
|
46
52
|
def raise_error_unless_valid_record_mode(record_mode)
|
47
53
|
unless VALID_RECORD_MODES.include?(record_mode)
|
48
54
|
raise ArgumentError.new("#{record_mode} is not a valid cassette record mode. Valid options are: #{VALID_RECORD_MODES.inspect}")
|
@@ -73,14 +79,18 @@ module VCR
|
|
73
79
|
|
74
80
|
if file
|
75
81
|
@original_recorded_interactions = begin
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
raise
|
82
|
+
interactions = YAML.load(raw_yaml_content)
|
83
|
+
|
84
|
+
if VCR.http_stubbing_adapter.ignore_localhost?
|
85
|
+
interactions.reject! do |i|
|
86
|
+
i.uri.is_a?(String) && VCR::LOCALHOST_ALIASES.include?(URI.parse(i.uri).host)
|
87
|
+
end
|
83
88
|
end
|
89
|
+
|
90
|
+
interactions
|
91
|
+
rescue TypeError
|
92
|
+
raise unless raw_yaml_content =~ /VCR::RecordedResponse/
|
93
|
+
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."
|
84
94
|
end if File.exist?(file)
|
85
95
|
|
86
96
|
recorded_interactions.replace(@original_recorded_interactions)
|
@@ -89,6 +99,31 @@ module VCR
|
|
89
99
|
VCR.http_stubbing_adapter.stub_requests(recorded_interactions)
|
90
100
|
end
|
91
101
|
|
102
|
+
def raw_yaml_content
|
103
|
+
content = File.read(file)
|
104
|
+
return content unless @erb
|
105
|
+
|
106
|
+
template = ERB.new(content)
|
107
|
+
|
108
|
+
begin
|
109
|
+
return template.result unless @erb.is_a?(Hash)
|
110
|
+
|
111
|
+
# create an object with methods for each desired local variable...
|
112
|
+
local_variables = Struct.new(*@erb.keys).new(*@erb.values)
|
113
|
+
|
114
|
+
# instance_eval seems to be the only way to get the binding for ruby 1.9: http://redmine.ruby-lang.org/issues/show/2161
|
115
|
+
template.result(local_variables.instance_eval { binding })
|
116
|
+
rescue NameError => e
|
117
|
+
var_name = e.message[/undefined local variable or method `(.*)' for/, 1].to_sym
|
118
|
+
example_hash = (@erb.is_a?(Hash) ? @erb : {}).merge(var_name => 'some value')
|
119
|
+
|
120
|
+
raise MissingERBVariableError.new(
|
121
|
+
"The ERB in the #{sanitized_name}.yml cassette file references undefined variable #{var_name}. " +
|
122
|
+
"Pass it to the cassette using :erb => #{ example_hash.inspect }."
|
123
|
+
)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
92
127
|
def write_recorded_interactions_to_disk
|
93
128
|
if VCR::Config.cassette_library_dir && new_recorded_interactions.size > 0
|
94
129
|
directory = File.dirname(file)
|
@@ -96,17 +131,5 @@ module VCR
|
|
96
131
|
File.open(file, 'w') { |f| f.write recorded_interactions.to_yaml }
|
97
132
|
end
|
98
133
|
end
|
99
|
-
|
100
|
-
def unstub_requests
|
101
|
-
VCR.http_stubbing_adapter.unstub_requests(@original_recorded_interactions)
|
102
|
-
end
|
103
|
-
|
104
|
-
def allow_real_http_lambda_for(allow_option)
|
105
|
-
if allow_option == :localhost
|
106
|
-
lambda { |uri| uri.host == 'localhost' }
|
107
|
-
else
|
108
|
-
allow_option
|
109
|
-
end
|
110
|
-
end
|
111
134
|
end
|
112
135
|
end
|
data/lib/vcr/config.rb
CHANGED
@@ -21,6 +21,15 @@ module VCR
|
|
21
21
|
defined_constants[0].to_s.downcase.to_sym if defined_constants.size == 1
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
def ignore_localhost=(value)
|
26
|
+
VCR.http_stubbing_adapter && VCR.http_stubbing_adapter.ignore_localhost = value
|
27
|
+
@ignore_localhost = value
|
28
|
+
end
|
29
|
+
|
30
|
+
def ignore_localhost?
|
31
|
+
VCR.http_stubbing_adapter ? VCR.http_stubbing_adapter.ignore_localhost? : @ignore_localhost
|
32
|
+
end
|
24
33
|
end
|
25
34
|
end
|
26
35
|
end
|
data/lib/vcr/deprecations.rb
CHANGED
@@ -1,54 +1,27 @@
|
|
1
1
|
module VCR
|
2
|
-
def create_cassette!(*args)
|
3
|
-
warn "WARNING: VCR.create_cassette! is deprecated. Instead, use: VCR.insert_cassette."
|
4
|
-
insert_cassette(*args)
|
5
|
-
end
|
6
|
-
|
7
|
-
def destroy_cassette!(*args)
|
8
|
-
warn "WARNING: VCR.destroy_cassette! is deprecated. Instead, use: VCR.eject_cassette."
|
9
|
-
eject_cassette(*args)
|
10
|
-
end
|
11
|
-
|
12
|
-
def with_cassette(*args, &block)
|
13
|
-
warn "WARNING: VCR.with_cassette is deprecated. Instead, use: VCR.use_cassette."
|
14
|
-
use_cassette(*args, &block)
|
15
|
-
end
|
16
|
-
|
17
2
|
class Cassette
|
18
|
-
def
|
19
|
-
warn "WARNING: VCR::Cassette#
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def cache_file(*args)
|
24
|
-
warn "WARNING: VCR::Cassette#cache_file is deprecated. Instead, use: VCR::Cassette#file."
|
25
|
-
file(*args)
|
3
|
+
def allow_real_http_requests_to?(uri)
|
4
|
+
warn "WARNING: VCR::Cassette#allow_real_http_requests_to? is deprecated and should no longer be used."
|
5
|
+
VCR.http_stubbing_adapter.ignore_localhost? && VCR::LOCALHOST_ALIASES.include?(uri.host)
|
26
6
|
end
|
27
7
|
|
28
8
|
private
|
29
9
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
10
|
+
def deprecate_old_cassette_options(options)
|
11
|
+
message = "VCR's :allow_real_http cassette option is deprecated. Instead, use the ignore_localhost configuration option."
|
12
|
+
if options[:allow_real_http] == :localhost
|
13
|
+
@original_ignore_localhost = VCR.http_stubbing_adapter.ignore_localhost?
|
14
|
+
VCR.http_stubbing_adapter.ignore_localhost = true
|
15
|
+
Kernel.warn "WARNING: #{message}"
|
16
|
+
elsif options[:allow_real_http]
|
17
|
+
raise ArgumentError.new(message)
|
34
18
|
end
|
35
19
|
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class Config
|
39
|
-
def self.cache_dir(*args)
|
40
|
-
warn "WARNING: VCR::Config.cache_dir is deprecated. Instead, use: VCR::Config.cassette_library_dir."
|
41
|
-
cassette_library_dir(*args)
|
42
|
-
end
|
43
20
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def self.default_cassette_record_mode=(value)
|
50
|
-
warn %Q{WARNING: #default_cassette_record_mode is deprecated. Instead, use: "default_cassette_options = { :record => :#{value.to_s} }"}
|
51
|
-
default_cassette_options.merge!(:record => value)
|
21
|
+
def restore_ignore_localhost_for_deprecation
|
22
|
+
if defined?(@original_ignore_localhost)
|
23
|
+
VCR.http_stubbing_adapter.ignore_localhost = @original_ignore_localhost
|
24
|
+
end
|
52
25
|
end
|
53
26
|
end
|
54
27
|
end
|