vcr 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +62 -0
- data/README.md +224 -0
- data/Rakefile +12 -5
- data/VERSION +1 -1
- data/benchmarks/http_stubbing_libraries.rb +59 -0
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette1.yml +25 -25
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette2.yml +32 -30
- data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette3.yml +40 -40
- data/features/fixtures/vcr_cassettes/1.8.6/nested_replay_cassette.yml +16 -15
- data/features/fixtures/vcr_cassettes/1.8.6/not_the_real_response.yml +25 -25
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette1.yml +23 -23
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette2.yml +24 -22
- data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette3.yml +36 -36
- data/features/fixtures/vcr_cassettes/1.8.7/nested_replay_cassette.yml +12 -11
- data/features/fixtures/vcr_cassettes/1.8.7/not_the_real_response.yml +23 -23
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette1.yml +24 -24
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette2.yml +26 -24
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette3.yml +38 -38
- data/features/fixtures/vcr_cassettes/1.9.1/nested_replay_cassette.yml +13 -12
- data/features/fixtures/vcr_cassettes/1.9.1/not_the_real_response.yml +24 -24
- data/features/record_response.feature +2 -2
- data/features/replay_recorded_response.feature +1 -1
- data/features/step_definitions/vcr_steps.rb +8 -8
- data/features/support/env.rb +6 -0
- data/lib/vcr.rb +16 -3
- data/lib/vcr/cassette.rb +49 -46
- data/lib/vcr/config.rb +8 -0
- data/lib/vcr/extensions/fake_web.rb +12 -26
- data/lib/vcr/extensions/net_http.rb +9 -26
- data/lib/vcr/http_stubbing_adapters/base.rb +34 -0
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +75 -0
- data/lib/vcr/http_stubbing_adapters/webmock.rb +80 -0
- data/lib/vcr/structs.rb +44 -0
- data/lib/vcr/task_runner.rb +47 -0
- data/lib/vcr/tasks/vcr.rake +9 -0
- data/spec/cassette_spec.rb +70 -71
- data/spec/config_spec.rb +27 -0
- data/spec/extensions/net_http_spec.rb +21 -17
- data/spec/fixtures/1.8.6/0_3_1_cassette.yml +29 -0
- data/spec/fixtures/1.8.6/cassette_spec/example.yml +110 -108
- data/spec/fixtures/1.8.6/example_net_http.yml +14 -0
- data/spec/fixtures/1.8.6/example_net_http_request.yml +12 -0
- data/spec/fixtures/1.8.6/example_net_http_response.yml +25 -0
- data/spec/fixtures/1.8.6/fake_example.com_responses.yml +90 -0
- data/spec/fixtures/1.8.7/0_3_1_cassette.yml +29 -0
- data/spec/fixtures/1.8.7/cassette_spec/example.yml +110 -108
- data/spec/fixtures/1.8.7/example_net_http.yml +14 -0
- data/spec/fixtures/1.8.7/example_net_http_request.yml +12 -0
- data/spec/fixtures/1.8.7/example_net_http_response.yml +25 -0
- data/spec/fixtures/1.8.7/fake_example.com_responses.yml +90 -0
- data/spec/fixtures/1.9.1/0_3_1_cassette.yml +29 -0
- data/spec/fixtures/1.9.1/cassette_spec/example.yml +62 -59
- data/spec/fixtures/1.9.1/example_net_http.yml +14 -0
- data/spec/fixtures/1.9.1/example_net_http_request.yml +12 -0
- data/spec/fixtures/1.9.1/example_net_http_response.yml +25 -0
- data/spec/fixtures/1.9.1/fake_example.com_responses.yml +90 -0
- data/spec/http_stubbing_adapters/fakeweb_spec.rb +24 -0
- data/spec/http_stubbing_adapters/webmock_spec.rb +23 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/structs_spec.rb +95 -0
- data/spec/support/http_stubbing_adapter.rb +113 -0
- data/spec/task_runner_spec.rb +59 -0
- data/spec/vcr_spec.rb +37 -0
- data/vcr.gemspec +45 -15
- metadata +67 -26
- data/History.rdoc +0 -31
- data/README.rdoc +0 -222
- data/lib/vcr/recorded_response.rb +0 -4
- data/spec/extensions/fake_web_spec.rb +0 -63
- data/spec/recorded_response_spec.rb +0 -25
data/CHANGELOG.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#Changelog
|
2
|
+
|
3
|
+
## 0.4.0 April 28, 2010
|
4
|
+
* Added support for webmock. All the fakeweb-specific code is now in an adapter (as is the webmock code).
|
5
|
+
|
6
|
+
* Changed the format of the VCR cassettes. The old format was tied directly to Net::HTTP, but webmock supports
|
7
|
+
other HTTP libraries and I plan to allow VCR to use them in the future. Note that this is a breaking change--your
|
8
|
+
old VCR cassettes from prior releases will not work with VCR 0.4.0. However, VCR provides a rake task to assist
|
9
|
+
you in migrating your cassettes to the new format. Simply add `load 'vcr/tasks/vcr.rake'` to your project's Rakefile,
|
10
|
+
and run:
|
11
|
+
|
12
|
+
$ rake vcr:migrate_cassettes DIR=path/to/cassete/library/directory
|
13
|
+
|
14
|
+
* The new cassette format records more information about the request (i.e. the request headers and body), so that it
|
15
|
+
can potentially be used with webmock in the future.
|
16
|
+
|
17
|
+
* Made most of `VCR::Cassette`'s methods private. I had forgotten to make the methods private before, and most of them
|
18
|
+
don't need to be exposed.
|
19
|
+
|
20
|
+
* Automatically disallow http connections using the appropriate setting of the http stubbing library (fakeweb or webmock).
|
21
|
+
This relieves users from the need to set the option themselves, so they hopefully aren't using either fakeweb or webmock
|
22
|
+
directly, making it much easier to switch between these.
|
23
|
+
|
24
|
+
* Change documentation from rdoc to markdown format.
|
25
|
+
|
26
|
+
* Lots of other refactoring.
|
27
|
+
|
28
|
+
## 0.3.1 April 10, 2010
|
29
|
+
* Fixed a bug: when `Net::HTTP#request` was called with a block that had a return statement, the response was not being recorded.
|
30
|
+
|
31
|
+
## 0.3.0 March 24, 2010
|
32
|
+
* Renamed a bunch of methods, replacing them with method names that more clearly fit the VCR/cassette metaphor:
|
33
|
+
* `VCR.create_cassette!` => `VCR.insert_cassette`
|
34
|
+
* `VCR.destroy_cassette!` => `VCR.eject_cassette`
|
35
|
+
* `VCR.with_cassette` => `VCR.use_cassette`
|
36
|
+
* `VCR::Cassette#destroy!` => `VCR::Cassette#eject`
|
37
|
+
* `VCR::Cassette#cache_file` => `VCR::Cassette#file`
|
38
|
+
* `VCR::Config.cache_dir` => `VCR::Config.cassette_library_dir`
|
39
|
+
* `:unregistered` record mode => `:new_episodes` record mode
|
40
|
+
|
41
|
+
* All the old methods still work, but you'll get deprecation warnings.
|
42
|
+
|
43
|
+
## 0.2.0 March 9, 2010
|
44
|
+
* Added `:allow_real_http` cassette option, which allows VCR to work with capybara and a javascript driver.
|
45
|
+
Bug reported by [Ben Hutton](http://github.com/benhutton).
|
46
|
+
|
47
|
+
* Deprecated the `default_cassette_record_mode` option. Use `default_cassette_options[:record_mode]` instead.
|
48
|
+
|
49
|
+
## 0.1.2 March 4, 2010
|
50
|
+
* Added explanatory note about VCR to `FakeWeb::NetConnectNotAllowedError#message`.
|
51
|
+
|
52
|
+
* Got things to work for when a cassette records multiple requests made to the same URL with the same HTTP verb,
|
53
|
+
but different responses. We have to register an array of responses with fakeweb.
|
54
|
+
|
55
|
+
* Fixed our `Net::HTTP` monkey patch so that it only stores the recorded response once per request.
|
56
|
+
Internally, `Net::HTTP#request` recursively calls itself (passing slightly different arguments) in certain circumstances.
|
57
|
+
|
58
|
+
## 0.1.1 February 25, 2010
|
59
|
+
* Handle asynchronous HTTP requests (such as for mechanize). Bug reported by [Thibaud Guillaume-Gentil](http://github.com/thibaudgg).
|
60
|
+
|
61
|
+
## 0.1.0 February 25, 2010
|
62
|
+
* Initial release. Basic recording and replaying of responses works.
|
data/README.md
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
# VCR
|
2
|
+
|
3
|
+
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install vcr
|
8
|
+
|
9
|
+
You'll also need either [FakeWeb](http://github.com/chrisk/fakeweb) or [WebMock](http://github.com/bblimke/webmock):
|
10
|
+
|
11
|
+
gem install fakeweb
|
12
|
+
|
13
|
+
or
|
14
|
+
|
15
|
+
gem install webmock
|
16
|
+
|
17
|
+
## Synopsis
|
18
|
+
|
19
|
+
require 'test/unit'
|
20
|
+
require 'vcr'
|
21
|
+
|
22
|
+
VCR.config do |c|
|
23
|
+
c.cassette_library_dir = 'fixtures/vcr_cassettes'
|
24
|
+
c.http_stubbing_library = :fakeweb # or :webmock
|
25
|
+
end
|
26
|
+
|
27
|
+
class VCRTest < Test::Unit::TestCase
|
28
|
+
def test_example_dot_com
|
29
|
+
response = VCR.use_cassette('synopsis', :record => :new_episodes) do
|
30
|
+
Net::HTTP.get_response(URI.parse('http://example.com/'))
|
31
|
+
end
|
32
|
+
assert_match /You have reached this web page by typing.*example\.com/, response.body
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Run this test once, and VCR will record the http request to `fixtures/vcr_cassettes/synopsis.yml`. Run it again, and VCR
|
37
|
+
will replay the response from example.com when the http request is made. This test is now fast (no real HTTP requests are
|
38
|
+
made anymore), deterministic (the test will continue to pass, even if you are offline, or example.com goes down for
|
39
|
+
maintenance) and accurate (the response from example.com will contain the same headers and body you get from a real request).
|
40
|
+
|
41
|
+
## Cassettes
|
42
|
+
|
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
|
45
|
+
recorded, or an error will be raised, depending on the cassette's `:record` mode (see below). When you use
|
46
|
+
a cassette that contains previously recorded HTTP interactions, it registers them with the http stubbing
|
47
|
+
library of your choice (fakeweb or webmock) so that HTTP requests get the recorded response. Between test
|
48
|
+
runs, cassettes are stored on disk as YAML files in your configured cassette library directory.
|
49
|
+
|
50
|
+
Each cassette can be configured with a couple options:
|
51
|
+
|
52
|
+
* `:record`: Specifies a record mode for this cassette.
|
53
|
+
* `:allow_real_http`: You can use this to force VCR to always allow a real HTTP request for particular URIs.
|
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).
|
66
|
+
|
67
|
+
## Record modes
|
68
|
+
|
69
|
+
VCR supports 3 record modes. You can set a default record mode in your configuration (see below)
|
70
|
+
and a per-cassette record mode when inserting a cassette. The record modes are:
|
71
|
+
|
72
|
+
* `:new_episodes`: Previously recorded HTTP interactions will be replayed. New HTTP interactions will be recorded.
|
73
|
+
* `:all`: Previously recorded HTTP interactions will be ignored. All HTTP interactions will be recorded.
|
74
|
+
* `:none`: Previously recorded HTTP interactions will be replayed. New HTTP interactions will result in an error.
|
75
|
+
|
76
|
+
## Configuration
|
77
|
+
|
78
|
+
require 'vcr'
|
79
|
+
|
80
|
+
VCR.config do |c|
|
81
|
+
c.cassette_library_dir = File.join(Rails.root, 'features', 'fixtures', 'cassette_library')
|
82
|
+
c.http_stubbing_library = :fakeweb
|
83
|
+
c.default_cassette_options = { :record => :none }
|
84
|
+
end
|
85
|
+
|
86
|
+
This can go pretty much wherever, as long as this code is run before your tests, specs or scenarios. I tend
|
87
|
+
to put it in `spec/support/vcr.rb`, `test/support/vcr.rb` or `features/support/vcr.rb`. You can set the following
|
88
|
+
configuration options:
|
89
|
+
|
90
|
+
* `cassette_library_dir`: VCR will save the cassette YAML files to this directory.
|
91
|
+
* `http_stubbing_library`: Which http stubbing library to use. Currently `:fakeweb` and `:webmock` are supported.
|
92
|
+
This is currently optional--VCR will try to guess based on the presence or absence of the `FakeWeb` or `WebMock`
|
93
|
+
constants, but this is mostly done to assist users upgrading from VCR 0.3.1, which only worked with fakeweb and
|
94
|
+
didn't have this option. I recommend you explicitly configure this.
|
95
|
+
* `default_cassette_options`: The default options for your cassettes. These will be overriden by any options you
|
96
|
+
set on each individual cassette.
|
97
|
+
|
98
|
+
## Usage with your favorite ruby test/spec framework
|
99
|
+
|
100
|
+
VCR can easily be used with any ruby test or spec framework. Usually, you'll want to use `VCR.use_cassette`:
|
101
|
+
|
102
|
+
VCR.use_cassette('geocoding/Seattle, WA', :record => :new_episodes) do
|
103
|
+
# do something that makes an HTTP request
|
104
|
+
end
|
105
|
+
|
106
|
+
Alternately, you can insert and eject a cassette with individual method calls from setup/before and teardown/after:
|
107
|
+
|
108
|
+
describe "Some object that makes an HTTP request" do
|
109
|
+
before(:each) do
|
110
|
+
VCR.insert_cassette('geocoding/Seattle, WA', :record => :new_episodes)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "does something that makes an HTTP request"
|
114
|
+
|
115
|
+
after(:each) do
|
116
|
+
VCR.eject_cassette
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
## Usage with Cucumber
|
121
|
+
|
122
|
+
VCR provides additional support for cucumber. You can of course use `VCR.use_cassette` within a step definition,
|
123
|
+
and that's the recommended way for any of your custom step definitions. But many times I find myself using generic
|
124
|
+
step definitions provided by another library (such as the webrat/capybara web steps generated by cucumber-rails),
|
125
|
+
and you don't want to modify these. VCR provides cucumber tagging support to help in these cases.
|
126
|
+
|
127
|
+
First, tag your scenario with something descriptive:
|
128
|
+
|
129
|
+
@facebook_http_request
|
130
|
+
Scenario: Sign up with facebook connect
|
131
|
+
|
132
|
+
Then let VCR know about this tag, in `features/support/vcr.rb` (or some similar support file):
|
133
|
+
|
134
|
+
VCR.cucumber_tags do |t|
|
135
|
+
t.tags '@facebook_http_request', '@twitter_status_update', :record => :none
|
136
|
+
t.tags '@another_scenario_tag'
|
137
|
+
end
|
138
|
+
|
139
|
+
For each of the tags you specify in your `cucumber_tags` block, VCR will set up the appropriate
|
140
|
+
[Before and After hooks](http://wiki.github.com/aslakhellesoy/cucumber/hooks) to use a cassette
|
141
|
+
for the entire scenario. The tag (minus the '@') will be used as the cassette name, and it'll
|
142
|
+
go in the `cucumber_tags` subdirectory of the configured cassette library directory.
|
143
|
+
|
144
|
+
If the last argument to `#tags` is a hash, VCR will use it as the options for the named cassettes.
|
145
|
+
|
146
|
+
## Usage with Capybara
|
147
|
+
|
148
|
+
When you use any of the javascript-enabled drivers (selenium, celerity, culerity) with
|
149
|
+
[capybara](http://github.com/jnicklas/capybara), it'll need to ping the app running on localhost.
|
150
|
+
Set the `:allow_real_http => :localhost` option on your cassettes to allow this (or set it as a
|
151
|
+
default cassette option in your configuration).
|
152
|
+
|
153
|
+
## Suggested Workflow
|
154
|
+
|
155
|
+
First, configure VCR as I have above. I like setting the default record mode to `:none`
|
156
|
+
so that no new HTTP requests are made without me explicitly allowing it.
|
157
|
+
|
158
|
+
When an HTTP request is made, you'll get an error such as:
|
159
|
+
|
160
|
+
Real HTTP connections are disabled. Unregistered request: get http://example.com
|
161
|
+
|
162
|
+
Find the place that is making the HTTP request (the backtrace should help here). If you've already recorded this HTTP
|
163
|
+
request to a cassette from a different test, you can simply re-use the cassette. Use `VCR.use_cassette`, as
|
164
|
+
shown above. You may also want to refactor this into a helper method that sets up the VCR cassette and does whatever
|
165
|
+
makes the HTTP request:
|
166
|
+
|
167
|
+
def set_user_address(user, address, city, state)
|
168
|
+
VCR.use_cassette("geocoding/#{address}, #{city}, #{state}", :record => :new_episodes) do
|
169
|
+
user.address.update_attributes!(:address => address, :city => city, :state => state)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
In this case, I've used a dynamic cassette name based on the address being geocoded. That way, each separate address
|
174
|
+
gets a different cassette, and tests that set the same user address will reuse the same cassette. I've also set
|
175
|
+
the record mode to `:new_episodes` so that VCR will automatically record geocoding requests for a new address
|
176
|
+
to a new cassette, without me having to change any code.
|
177
|
+
|
178
|
+
If the HTTP request that triggered the error is new, you'll have to record it for the first time. Simply use
|
179
|
+
`VCR.use_cassette` with the record mode set to `:new_episodes` or `:all`. Run the test again, and VCR will
|
180
|
+
record the HTTP interaction. I usually remove the record mode at this point so that it uses the default
|
181
|
+
of `:none` in the future. Future test runs will get the recorded response, and if your code changes so
|
182
|
+
that it is making a new HTTP request, you'll be notified by an error as shown above.
|
183
|
+
|
184
|
+
## Ruby Version Compatibility
|
185
|
+
|
186
|
+
VCR works on ruby [1.8.6](http://integrity186.heroku.com/vcr), [1.8.7](http://integrity187.heroku.com/vcr) and
|
187
|
+
[1.9.1](http://integrity191.heroku.com/vcr).
|
188
|
+
|
189
|
+
## Notes, etc.
|
190
|
+
|
191
|
+
* The objects serialized to the cassette YAML files changed with the 0.4 release. Cassettes recorded with
|
192
|
+
older versions of VCR will not work with VCR 0.4.0 and later. However, VCR provides a rake task to migrate
|
193
|
+
your old cassettes to the new format--see the [changelog](http://github.com/myronmarston/vcr/blob/master/CHANGELOG.md)
|
194
|
+
for more info.
|
195
|
+
* The cassette name determines the name of the library file for the given cassette. Strings or symbols are fine,
|
196
|
+
and you can include any characters, but spaces and invalid file name characters will be removed
|
197
|
+
before the cassette reads or writes to its library file.
|
198
|
+
* You can use a directory separator (i.e. '/') in your cassette names to cause it to use a subdirectory
|
199
|
+
of the cassette library directory. The cucumber tagging support uses this.
|
200
|
+
* VCR maintains a simple stack of cassettes. This allows you to nest them as deeply as you want.
|
201
|
+
This is particularly useful when you have a cucumber step definition that uses a cassette, and
|
202
|
+
you also want to use a cassette for the entire scenario using the tagging support.
|
203
|
+
|
204
|
+
## Note on Patches/Pull Requests
|
205
|
+
|
206
|
+
* Fork the project.
|
207
|
+
* Make your feature addition or bug fix.
|
208
|
+
* Add tests for it. This is important so I don't break it in a
|
209
|
+
future version unintentionally.
|
210
|
+
* Commit, do not mess with rakefile, version, or history.
|
211
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
212
|
+
* Send me a pull request. Bonus points for topic branches.
|
213
|
+
|
214
|
+
## Thanks
|
215
|
+
|
216
|
+
* [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)
|
218
|
+
* [Chris Kampmeier](http://github.com/chrisk) for [FakeWeb](http://github.com/chrisk/fakeweb).
|
219
|
+
* [Chris Young](http://github.com/chrisyoung) for [NetRecorder](http://github.com/chrisyoung/netrecorder),
|
220
|
+
the inspiration for VCR.
|
221
|
+
|
222
|
+
## Copyright
|
223
|
+
|
224
|
+
Copyright (c) 2010 Myron Marston. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -5,16 +5,17 @@ begin
|
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "vcr"
|
8
|
-
gem.summary = %Q{
|
9
|
-
gem.description = %Q{VCR provides helpers to record HTTP
|
8
|
+
gem.summary = %Q{Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.}
|
9
|
+
gem.description = %Q{VCR provides helpers to record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests. It works with any ruby testing framework, and provides built-in support for cucumber.}
|
10
10
|
gem.email = "myron.marston@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/myronmarston/vcr"
|
12
12
|
gem.authors = ["Myron Marston"]
|
13
13
|
|
14
|
-
gem.add_dependency 'fakeweb', '>= 1.2.8'
|
15
|
-
|
16
14
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
17
15
|
gem.add_development_dependency "cucumber", ">= 0.6.1"
|
16
|
+
gem.add_development_dependency "fakeweb", ">= 1.2.8"
|
17
|
+
gem.add_development_dependency "webmock", ">= 1.1.0"
|
18
|
+
|
18
19
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
20
|
end
|
20
21
|
Jeweler::GemcutterTasks.new
|
@@ -47,7 +48,13 @@ rescue LoadError
|
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
50
|
-
task :default =>
|
51
|
+
task :default => :spec do
|
52
|
+
%w( webmock fakeweb ).each do |http_stubbing_adapter|
|
53
|
+
puts "\n\n-------------- Running features using #{http_stubbing_adapter} http_stubbing_adapter -----------------\n"
|
54
|
+
ENV['HTTP_STUBBING_ADAPTER'] = http_stubbing_adapter
|
55
|
+
Rake::Task[:features].execute
|
56
|
+
end
|
57
|
+
end
|
51
58
|
|
52
59
|
require 'rake/rdoctask'
|
53
60
|
Rake::RDocTask.new do |rdoc|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'benchmark'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
def http_request
|
6
|
+
res = Net::HTTP.get_response(URI.parse('http://example.com'))
|
7
|
+
raise "Body should be 'Hello'" unless res.body == 'Hello'
|
8
|
+
end
|
9
|
+
|
10
|
+
def fakeweb
|
11
|
+
FakeWeb.register_uri(:get, 'http://example.com', :body => 'Hello')
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
FakeWeb.clean_registry
|
15
|
+
end
|
16
|
+
|
17
|
+
def webmock
|
18
|
+
WebMock.stub_request(:get, 'http://example.com').to_return(:body => 'Hello')
|
19
|
+
yield
|
20
|
+
ensure
|
21
|
+
WebMock.reset_webmock
|
22
|
+
end
|
23
|
+
|
24
|
+
def perform_benchmark(name)
|
25
|
+
puts "\n\nBenchmarking #{name}:"
|
26
|
+
Benchmark.benchmark do |b|
|
27
|
+
%w(webmock fakeweb).each do |type|
|
28
|
+
b.report(type) do
|
29
|
+
# this is a bit convoluted, but we want to ensure that each benchmark runs without the other library loaded,
|
30
|
+
# so we fork off a sub-process before requiring the libraries.
|
31
|
+
Process.fork do
|
32
|
+
require type
|
33
|
+
yield type
|
34
|
+
end
|
35
|
+
Process.wait
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
n = 5000
|
42
|
+
perform_benchmark("Single setup/teardown") do |type|
|
43
|
+
send(type) { n.times { http_request } }
|
44
|
+
end
|
45
|
+
|
46
|
+
perform_benchmark("Setup/teardown for each http request") do |type|
|
47
|
+
n.times { send(type) { http_request } }
|
48
|
+
end
|
49
|
+
|
50
|
+
# Output on my machine:
|
51
|
+
#
|
52
|
+
# Benchmarking Single setup/teardown:
|
53
|
+
# webmock 0.000000 0.000000 18.830000 ( 19.350281)
|
54
|
+
# fakeweb 0.000000 0.000000 1.930000 ( 2.049569)
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# Benchmarking Setup/teardown for each http request:
|
58
|
+
# webmock 0.000000 0.000000 21.350000 ( 21.795557)
|
59
|
+
# fakeweb 0.000000 0.000000 2.490000 ( 2.707574)
|
@@ -1,30 +1,21 @@
|
|
1
1
|
---
|
2
|
-
- !ruby/struct:VCR::
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
body:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
This is not the real response from example.com
|
14
|
-
</p>
|
15
|
-
</BODY>
|
16
|
-
</HTML>
|
17
|
-
|
18
|
-
|
19
|
-
body_exist: true
|
20
|
-
code: "200"
|
21
|
-
header:
|
22
|
-
etag:
|
23
|
-
- "\"24ec5-1b6-4059a80bfd280\""
|
2
|
+
- !ruby/struct:VCR::HTTPInteraction
|
3
|
+
request: !ruby/struct:VCR::Request
|
4
|
+
method: :get
|
5
|
+
uri: http://example.com:80/
|
6
|
+
body:
|
7
|
+
headers:
|
8
|
+
response: !ruby/struct:VCR::Response
|
9
|
+
status: !ruby/struct:VCR::ResponseStatus
|
10
|
+
code: 200
|
11
|
+
message: OK
|
12
|
+
headers:
|
24
13
|
last-modified:
|
25
14
|
- Tue, 15 Nov 2005 13:24:10 GMT
|
26
15
|
connection:
|
27
16
|
- Keep-Alive
|
17
|
+
etag:
|
18
|
+
- "\"24ec5-1b6-4059a80bfd280\""
|
28
19
|
content-type:
|
29
20
|
- text/html; charset=UTF-8
|
30
21
|
date:
|
@@ -37,7 +28,16 @@
|
|
37
28
|
- "2643"
|
38
29
|
accept-ranges:
|
39
30
|
- bytes
|
31
|
+
body: |
|
32
|
+
<HTML>
|
33
|
+
<HEAD>
|
34
|
+
<TITLE>Example Web Page</TITLE>
|
35
|
+
</HEAD>
|
36
|
+
<body>
|
37
|
+
<p>
|
38
|
+
This is not the real response from example.com
|
39
|
+
</p>
|
40
|
+
</BODY>
|
41
|
+
</HTML>
|
42
|
+
|
40
43
|
http_version: "1.1"
|
41
|
-
message: OK
|
42
|
-
read: true
|
43
|
-
socket:
|