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.
- data/.gitignore +2 -0
- data/.travis.yml +6 -2
- data/CHANGELOG.md +49 -1
- data/Gemfile +1 -5
- data/Guardfile +0 -5
- data/README.md +3 -2
- data/Rakefile +11 -16
- data/cucumber.yml +0 -4
- data/features/.nav +14 -2
- data/features/cassettes/automatic_re_recording.feature +4 -6
- data/features/cassettes/dynamic_erb.feature +6 -8
- data/features/cassettes/exclusive.feature +111 -0
- data/features/cassettes/format.feature +16 -14
- data/features/cassettes/naming.feature +4 -6
- data/features/cassettes/no_cassette.feature +25 -28
- data/features/cassettes/update_content_length_header.feature +9 -9
- data/features/configuration/allow_http_connections_when_no_cassette.feature +6 -8
- data/features/configuration/cassette_library_dir.feature +4 -6
- data/features/configuration/default_cassette_options.feature +12 -10
- data/features/configuration/filter_sensitive_data.feature +12 -17
- data/features/configuration/{stub_with.feature → hook_into.feature} +63 -62
- data/features/configuration/hooks.feature +23 -33
- data/features/configuration/ignore_hosts.feature +16 -17
- data/features/configuration/ignore_localhost.feature +33 -42
- data/features/http_libraries/em_http_request.feature +7 -8
- data/features/http_libraries/net_http.feature +26 -28
- data/features/middleware/faraday.feature +17 -56
- data/features/middleware/rack.feature +8 -11
- data/features/record_modes/all.feature +5 -7
- data/features/record_modes/new_episodes.feature +5 -7
- data/features/record_modes/none.feature +6 -6
- data/features/record_modes/once.feature +6 -8
- data/features/request_matching/README.md +28 -0
- data/features/request_matching/body.feature +81 -0
- data/features/request_matching/custom_matcher.feature +125 -0
- data/features/request_matching/headers.feature +85 -0
- data/features/request_matching/host.feature +85 -0
- data/features/request_matching/identical_request_sequence.feature +79 -0
- data/features/request_matching/method.feature +86 -0
- data/features/request_matching/path.feature +86 -0
- data/features/request_matching/playback_repeats.feature +87 -0
- data/features/request_matching/uri.feature +84 -0
- data/features/request_matching/uri_without_param.feature +85 -0
- data/features/step_definitions/cli_steps.rb +4 -28
- data/features/support/env.rb +11 -9
- data/features/support/http_lib_filters.rb +2 -11
- data/features/support/vcr_cucumber_helpers.rb +4 -5
- data/features/test_frameworks/cucumber.feature +17 -18
- data/features/test_frameworks/rspec.feature +8 -12
- data/features/test_frameworks/shoulda.feature +5 -8
- data/features/test_frameworks/test_unit.feature +5 -8
- data/lib/vcr.rb +38 -58
- data/lib/vcr/cassette.rb +41 -60
- data/lib/vcr/cassette/http_interaction_list.rb +56 -0
- data/lib/vcr/cassette/reader.rb +29 -31
- data/lib/vcr/configuration.rb +76 -0
- data/lib/vcr/deprecations.rb +39 -0
- data/lib/vcr/errors.rb +22 -0
- data/lib/vcr/library_hooks.rb +19 -0
- data/lib/vcr/library_hooks/excon.rb +136 -0
- data/lib/vcr/library_hooks/fakeweb.rb +110 -0
- data/lib/vcr/library_hooks/faraday.rb +3 -0
- data/lib/vcr/library_hooks/typhoeus.rb +98 -0
- data/lib/vcr/library_hooks/webmock.rb +100 -0
- data/lib/vcr/middleware/faraday.rb +43 -36
- data/lib/vcr/middleware/rack.rb +28 -4
- data/lib/vcr/request_handler.rb +43 -0
- data/lib/vcr/request_ignorer.rb +31 -0
- data/lib/vcr/request_matcher_registry.rb +86 -0
- data/lib/vcr/structs/http_interaction.rb +24 -18
- data/lib/vcr/structs/normalizers/body.rb +1 -1
- data/lib/vcr/structs/normalizers/header.rb +1 -1
- data/lib/vcr/structs/normalizers/status_message.rb +1 -1
- data/lib/vcr/structs/normalizers/uri.rb +1 -1
- data/lib/vcr/structs/request.rb +0 -13
- data/lib/vcr/structs/response.rb +2 -9
- data/lib/vcr/structs/response_status.rb +0 -4
- data/lib/vcr/test_frameworks/cucumber.rb +1 -1
- data/lib/vcr/test_frameworks/rspec.rb +1 -1
- data/lib/vcr/util/hooks.rb +28 -19
- data/lib/vcr/util/internet_connection.rb +29 -2
- data/lib/vcr/util/version_checker.rb +60 -0
- data/lib/vcr/version.rb +1 -1
- data/script/FullBuildRakeFile +0 -7
- data/script/full_build +1 -1
- data/spec/capture_warnings.rb +36 -31
- data/spec/fixtures/{1.9.1/cassette_spec → cassette_spec}/empty.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/example.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/with_localhost_requests.yml +0 -0
- data/spec/fixtures/{not_1.9.1/fake_example.com_responses.yml → fake_example.com_responses.yml} +0 -0
- data/spec/fixtures/{not_1.9.1/match_requests_on.yml → match_requests_on.yml} +0 -0
- data/spec/monkey_patches.rb +40 -11
- data/spec/spec_helper.rb +7 -43
- data/spec/support/http_library_adapters.rb +3 -13
- data/spec/support/shared_example_groups/{http_library.rb → hook_into_http_library.rb} +39 -111
- data/spec/support/shared_example_groups/version_checking.rb +9 -9
- data/spec/vcr/cassette/http_interaction_list_spec.rb +178 -0
- data/spec/vcr/cassette/reader_spec.rb +2 -2
- data/spec/vcr/cassette_spec.rb +121 -156
- data/spec/vcr/configuration_spec.rb +143 -0
- data/spec/vcr/deprecations_spec.rb +91 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/excon_spec.rb +6 -9
- data/spec/vcr/library_hooks/fakeweb_spec.rb +83 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/typhoeus_spec.rb +7 -11
- data/spec/vcr/library_hooks/webmock_spec.rb +17 -0
- data/spec/vcr/library_hooks_spec.rb +51 -0
- data/spec/vcr/middleware/faraday_spec.rb +17 -44
- data/spec/vcr/middleware/rack_spec.rb +94 -58
- data/spec/vcr/request_ignorer_spec.rb +54 -0
- data/spec/vcr/request_matcher_registry_spec.rb +223 -0
- data/spec/vcr/structs/request_spec.rb +0 -33
- data/spec/vcr/structs/response_spec.rb +0 -24
- data/spec/vcr/structs/response_status_spec.rb +0 -9
- data/spec/vcr/util/hooks_spec.rb +3 -5
- data/spec/vcr/version_spec.rb +1 -1
- data/spec/vcr_spec.rb +79 -91
- data/vcr.gemspec +1 -1
- metadata +83 -103
- data/features/cassettes/request_matching.feature +0 -383
- data/lib/vcr/config.rb +0 -84
- data/lib/vcr/deprecations/cassette.rb +0 -29
- data/lib/vcr/deprecations/config.rb +0 -18
- data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +0 -9
- data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +0 -11
- data/lib/vcr/extensions/net_http.rb +0 -32
- data/lib/vcr/http_stubbing_adapters/common.rb +0 -202
- data/lib/vcr/http_stubbing_adapters/excon.rb +0 -178
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +0 -107
- data/lib/vcr/http_stubbing_adapters/faraday.rb +0 -26
- data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +0 -43
- data/lib/vcr/http_stubbing_adapters/typhoeus.rb +0 -115
- data/lib/vcr/http_stubbing_adapters/webmock.rb +0 -120
- data/lib/vcr/middleware/cassette_arguments.rb +0 -19
- data/lib/vcr/middleware/common.rb +0 -20
- data/lib/vcr/request_matcher.rb +0 -94
- data/lib/vcr/rspec.rb +0 -2
- data/lib/vcr/util/basic_object.rb +0 -43
- data/lib/vcr/util/ping.rb +0 -30
- data/lib/vcr/util/regexes.rb +0 -37
- 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/cassette_spec/with_localhost_requests.yml +0 -109
- 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 -108
- data/spec/fixtures/1.9.1/match_requests_on.yml +0 -185
- data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/not_1.9.1/cassette_spec/empty.yml +0 -0
- data/spec/fixtures/not_1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/not_1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/not_1.9.1/example_net_http_response.yml +0 -25
- data/spec/support/shared_example_groups/http_stubbing_adapter.rb +0 -133
- data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +0 -28
- data/spec/vcr/config_spec.rb +0 -181
- data/spec/vcr/deprecations/cassette_spec.rb +0 -57
- data/spec/vcr/deprecations/config_spec.rb +0 -30
- data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +0 -7
- data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +0 -16
- data/spec/vcr/extensions/net_http_spec.rb +0 -80
- data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +0 -19
- data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +0 -76
- data/spec/vcr/http_stubbing_adapters/multi_object_proxy_spec.rb +0 -101
- data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +0 -17
- data/spec/vcr/middleware/cassette_arguments_spec.rb +0 -32
- data/spec/vcr/request_matcher_spec.rb +0 -230
@@ -1,52 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe VCR::Middleware::Faraday do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
4
|
+
%w[ typhoeus net_http patron ].each do |lib|
|
5
|
+
it_behaves_like 'a hook into an HTTP library', "faraday (w/ #{lib})",
|
6
|
+
:status_message_not_exposed,
|
7
|
+
:does_not_support_rotating_responses,
|
8
|
+
:not_disableable
|
10
9
|
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
VCR.current_cassette.should be_nil
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'sets the cassette name based on the provided block' do
|
28
|
-
app = lambda { |env| VCR.current_cassette.name.should eq('rack_cassette') }
|
29
|
-
instance = described_class.new(app) { |c| c.name 'rack_cassette' }
|
30
|
-
instance.call(env_hash)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'sets the cassette options based on the provided block' do
|
34
|
-
app = lambda { |env| VCR.current_cassette.erb.should eq({ :foo => :bar }) }
|
35
|
-
instance = described_class.new(app, &lambda do |c|
|
36
|
-
c.name 'c'
|
37
|
-
c.options :erb => { :foo => :bar }
|
38
|
-
end)
|
39
|
-
|
40
|
-
instance.call(env_hash)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'yields the env to the provided block when the block accepts 2 arguments' do
|
44
|
-
instance = described_class.new(lambda { |env| }, &lambda do |c, env|
|
45
|
-
env.should eq(env_hash)
|
46
|
-
c.name 'c'
|
47
|
-
end)
|
48
|
-
|
49
|
-
instance.call(env_hash)
|
11
|
+
it_performs('version checking', 'Faraday',
|
12
|
+
:valid => %w[ 0.7.0 0.7.10 ],
|
13
|
+
:too_low => %w[ 0.6.9 0.5.99 ],
|
14
|
+
:too_high => %w[ 0.8.0 1.0.0 ],
|
15
|
+
:file => 'vcr/middleware/faraday.rb'
|
16
|
+
) do
|
17
|
+
before(:each) { @orig_version = Faraday::VERSION }
|
18
|
+
after(:each) { Faraday::VERSION = @orig_version }
|
19
|
+
|
20
|
+
# Cannot be regular method def as that raises a "dynamic constant assignment" error
|
21
|
+
define_method :stub_version do |version|
|
22
|
+
::Faraday::VERSION = version
|
50
23
|
end
|
51
24
|
end
|
52
25
|
end
|
@@ -1,74 +1,110 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'vcr/middleware/rack'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
4
|
+
module VCR
|
5
|
+
module Middleware
|
6
|
+
describe CassetteArguments do
|
7
|
+
describe '#name' do
|
8
|
+
it 'initially returns nil' do
|
9
|
+
subject.name.should be_nil
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
rack_app = mock
|
16
|
-
rack_app.should_receive(:call).with(env_hash).and_return(:response)
|
17
|
-
instance = described_class.new(rack_app) { |c| c.name 'cassette_name' }
|
18
|
-
instance.call(env_hash).should eq(:response)
|
19
|
-
end
|
12
|
+
it 'stores the given value, returning it when no arg is given' do
|
13
|
+
subject.name :value1
|
14
|
+
subject.name.should eq(:value1)
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
instance.call({})
|
26
|
-
VCR.current_cassette.should be_nil
|
27
|
-
end
|
16
|
+
subject.name :value2
|
17
|
+
subject.name.should eq(:value2)
|
18
|
+
end
|
19
|
+
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
21
|
+
describe '#options' do
|
22
|
+
it 'initially returns an empty hash' do
|
23
|
+
subject.options.should eq({})
|
24
|
+
end
|
34
25
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
c.name 'c'
|
39
|
-
c.options :erb => { :foo => :bar }
|
40
|
-
end)
|
26
|
+
it 'merges the given hash options, returning them when no arg is given' do
|
27
|
+
subject.options :record => :new_episodes
|
28
|
+
subject.options.should eq({ :record => :new_episodes })
|
41
29
|
|
42
|
-
|
30
|
+
subject.options :erb => true
|
31
|
+
subject.options.should eq({ :record => :new_episodes, :erb => true })
|
32
|
+
end
|
33
|
+
end
|
43
34
|
end
|
44
35
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
36
|
+
describe Rack do
|
37
|
+
describe '.new' do
|
38
|
+
it 'raises an error if no cassette arguments block is provided' do
|
39
|
+
expect {
|
40
|
+
described_class.new(lambda { |env| })
|
41
|
+
}.to raise_error(ArgumentError)
|
42
|
+
end
|
43
|
+
end
|
50
44
|
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
describe '#call' do
|
46
|
+
let(:env_hash) { { :env => :hash } }
|
47
|
+
it 'calls the provided rack app and returns its response' do
|
48
|
+
rack_app = mock
|
49
|
+
rack_app.should_receive(:call).with(env_hash).and_return(:response)
|
50
|
+
instance = described_class.new(rack_app) { |c| c.name 'cassette_name' }
|
51
|
+
instance.call(env_hash).should eq(:response)
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
54
|
+
it 'uses a cassette when the rack app is called' do
|
55
|
+
VCR.current_cassette.should be_nil
|
56
|
+
rack_app = lambda { |env| VCR.current_cassette.should_not be_nil }
|
57
|
+
instance = described_class.new(rack_app) { |c| c.name 'cassette_name' }
|
58
|
+
instance.call({})
|
59
|
+
VCR.current_cassette.should be_nil
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
it 'sets the cassette name based on the provided block' do
|
63
|
+
rack_app = lambda { |env| VCR.current_cassette.name.should eq('rack_cassette') }
|
64
|
+
instance = described_class.new(rack_app) { |c| c.name 'rack_cassette' }
|
65
|
+
instance.call({})
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'sets the cassette options based on the provided block' do
|
69
|
+
rack_app = lambda { |env| VCR.current_cassette.erb.should eq({ :foo => :bar }) }
|
70
|
+
instance = described_class.new(rack_app, &lambda do |c|
|
71
|
+
c.name 'c'
|
72
|
+
c.options :erb => { :foo => :bar }
|
73
|
+
end)
|
74
|
+
|
75
|
+
instance.call({})
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'yields the rack env to the provided block when the block accepts 2 arguments' do
|
79
|
+
instance = described_class.new(lambda { |env| }, &lambda do |c, env|
|
80
|
+
env.should eq(env_hash)
|
81
|
+
c.name 'c'
|
82
|
+
end)
|
83
|
+
|
84
|
+
instance.call(env_hash)
|
85
|
+
end
|
86
|
+
end
|
67
87
|
|
68
|
-
|
69
|
-
|
70
|
-
|
88
|
+
let(:threaded_app) do
|
89
|
+
lambda do |env|
|
90
|
+
sleep 0.15
|
91
|
+
VCR.send(:cassettes).should have(1).cassette
|
92
|
+
[200, {}, ['OK']]
|
93
|
+
end
|
94
|
+
end
|
71
95
|
|
72
|
-
|
96
|
+
it 'is thread safe' do
|
97
|
+
stack = described_class.new(threaded_app) do |cassette|
|
98
|
+
cassette.name 'c'
|
99
|
+
end
|
100
|
+
|
101
|
+
thread = Thread.new { stack.call({}) }
|
102
|
+
stack.call({})
|
103
|
+
thread.join
|
104
|
+
|
105
|
+
VCR.current_cassette.should be_nil
|
106
|
+
end
|
107
|
+
end
|
73
108
|
end
|
74
109
|
end
|
110
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'vcr/structs/http_interaction'
|
2
|
+
require 'vcr/request_ignorer'
|
3
|
+
|
4
|
+
module VCR
|
5
|
+
describe RequestIgnorer do
|
6
|
+
def request(uri)
|
7
|
+
VCR::Request.new.tap { |r| r.uri = uri }
|
8
|
+
end
|
9
|
+
|
10
|
+
shared_examples_for "#ignore?" do |url, expected_value|
|
11
|
+
it "returns #{expected_value} if given a request with a url like #{url}" do
|
12
|
+
subject.ignore?(request(url)).should eq(expected_value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when example.com and example.net are ignored' do
|
17
|
+
before(:each) { subject.ignore_hosts 'example.com', 'example.net' }
|
18
|
+
|
19
|
+
it_behaves_like "#ignore?", "http://www.example.com/foo", false
|
20
|
+
it_behaves_like "#ignore?", "http://example.com/foo", true
|
21
|
+
it_behaves_like "#ignore?", "http://example.net:890/foo", true
|
22
|
+
it_behaves_like "#ignore?", "http://some-other-domain.com/", false
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when ignore_localhost is set to true' do
|
26
|
+
before(:each) { subject.ignore_localhost = true }
|
27
|
+
|
28
|
+
it_behaves_like "#ignore?", "http://some-host.com/foo", false
|
29
|
+
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
|
30
|
+
it_behaves_like "#ignore?", "http://#{host}/foo", true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when ignore_localhost is not set' do
|
35
|
+
it_behaves_like "#ignore?", "http://some-host.com/foo", false
|
36
|
+
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
|
37
|
+
it_behaves_like "#ignore?", "http://#{host}/foo", false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when ignore_localhost is set to false after being set to true' do
|
42
|
+
before(:each) do
|
43
|
+
subject.ignore_localhost = true
|
44
|
+
subject.ignore_localhost = false
|
45
|
+
end
|
46
|
+
|
47
|
+
it_behaves_like "#ignore?", "http://some-host.com/foo", false
|
48
|
+
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
|
49
|
+
it_behaves_like "#ignore?", "http://#{host}/foo", false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'vcr/request_matcher_registry'
|
2
|
+
require 'vcr/structs/http_interaction'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module VCR
|
6
|
+
describe RequestMatcherRegistry do
|
7
|
+
def request_with(values)
|
8
|
+
VCR::Request.new.tap do |request|
|
9
|
+
values.each do |name, value|
|
10
|
+
request.send("#{name}=", value)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#register" do
|
16
|
+
it 'registers a request matcher block that can be used later' do
|
17
|
+
matcher_called = false
|
18
|
+
subject.register(:my_matcher) { |*a| matcher_called = true }
|
19
|
+
subject[:my_matcher].matches?(stub, stub)
|
20
|
+
matcher_called.should be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when there is already a matcher for the given name' do
|
24
|
+
before(:each) do
|
25
|
+
subject.register(:foo) { |*a| false }
|
26
|
+
subject.stub :warn
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'overrides the existing matcher' do
|
30
|
+
subject.register(:foo) { |*a| true }
|
31
|
+
subject[:foo].matches?(stub, stub).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'warns that there is a name collision' do
|
35
|
+
subject.should_receive(:warn).with(
|
36
|
+
/WARNING: There is already a VCR request matcher registered for :foo\. Overriding it/
|
37
|
+
)
|
38
|
+
|
39
|
+
subject.register(:foo) { |*a| true }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#for" do
|
45
|
+
it 'returns a previously registered matcher' do
|
46
|
+
matcher = lambda { }
|
47
|
+
subject.register(:my_matcher, &matcher)
|
48
|
+
subject[:my_matcher].should eq(RequestMatcherRegistry::Matcher.new(matcher))
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'raises an ArgumentError when no matcher has been registered for the given name' do
|
52
|
+
expect {
|
53
|
+
subject[:some_unregistered_matcher]
|
54
|
+
}.to raise_error(UnregisteredMatcherError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns an object that calls the named block when #matches? is called on it' do
|
58
|
+
subject.register(:foo) { |r1, r2| r1 == 5 || r2 == 10 }
|
59
|
+
subject[:foo].matches?(5, 0).should be_true
|
60
|
+
subject[:foo].matches?(0, 10).should be_true
|
61
|
+
subject[:foo].matches?(7, 7).should be_false
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'returns an object that calls the given callable when #matches? is called on it' do
|
65
|
+
block_called = false
|
66
|
+
subject[lambda { |r1, r2| block_called = true }].matches?(5, 0)
|
67
|
+
block_called.should be_true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
[:uri_without_param, :uri_without_params].each do |meth|
|
72
|
+
describe "##{meth}" do
|
73
|
+
it 'matches two requests with URIs that are identical' do
|
74
|
+
subject[subject.send(meth, :foo)].matches?(
|
75
|
+
request_with(:uri => 'http://example.com/search?foo=123'),
|
76
|
+
request_with(:uri => 'http://example.com/search?foo=123')
|
77
|
+
).should be_true
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not matches two requests with different path parts' do
|
81
|
+
subject[subject.send(meth, :foo)].matches?(
|
82
|
+
request_with(:uri => 'http://example.com/search?foo=123'),
|
83
|
+
request_with(:uri => 'http://example.com/find?foo=123')
|
84
|
+
).should be_false
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'ignores the given query parameters when it is at the start' do
|
88
|
+
subject[subject.send(meth, :foo)].matches?(
|
89
|
+
request_with(:uri => 'http://example.com/search?foo=123&bar=r'),
|
90
|
+
request_with(:uri => 'http://example.com/search?foo=124&bar=r')
|
91
|
+
).should be_true
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'ignores the given query parameters when it is at the end' do
|
95
|
+
subject[subject.send(meth, :bar)].matches?(
|
96
|
+
request_with(:uri => 'http://example.com/search?foo=124&bar=r'),
|
97
|
+
request_with(:uri => 'http://example.com/search?foo=124&bar=q')
|
98
|
+
).should be_true
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'still takes into account other query params' do
|
102
|
+
subject[subject.send(meth, :bar)].matches?(
|
103
|
+
request_with(:uri => 'http://example.com/search?foo=123&bar=r'),
|
104
|
+
request_with(:uri => 'http://example.com/search?foo=124&bar=q')
|
105
|
+
).should be_false
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'handles multiple query params of the same name' do
|
109
|
+
subject[subject.send(meth, :tag)].matches?(
|
110
|
+
request_with(:uri => 'http://example.com/search?foo=124&tag[]=a&tag[]=b'),
|
111
|
+
request_with(:uri => 'http://example.com/search?foo=124&tag[]=d&tag[]=e')
|
112
|
+
).should be_true
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'can ignore multiple named parameters' do
|
116
|
+
subject[subject.send(meth, :foo, :bar)].matches?(
|
117
|
+
request_with(:uri => 'http://example.com/search?foo=123&bar=r&baz=9'),
|
118
|
+
request_with(:uri => 'http://example.com/search?foo=124&baz=9&bar=q')
|
119
|
+
).should be_true
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "built-ins" do
|
125
|
+
describe ":method" do
|
126
|
+
it 'matches when it is the same' do
|
127
|
+
subject[:method].matches?(
|
128
|
+
request_with(:method => :get),
|
129
|
+
request_with(:method => :get)
|
130
|
+
).should be_true
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'does not match when it is not the same' do
|
134
|
+
subject[:method].matches?(
|
135
|
+
request_with(:method => :get),
|
136
|
+
request_with(:method => :post)
|
137
|
+
).should be_false
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe ":uri" do
|
142
|
+
it 'matches when it is exactly the same' do
|
143
|
+
subject[:uri].matches?(
|
144
|
+
request_with(:uri => 'http://foo.com/bar?baz=7'),
|
145
|
+
request_with(:uri => 'http://foo.com/bar?baz=7')
|
146
|
+
).should be_true
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'does not match when it is different' do
|
150
|
+
subject[:uri].matches?(
|
151
|
+
request_with(:uri => 'http://foo1.com/bar?baz=7'),
|
152
|
+
request_with(:uri => 'http://foo2.com/bar?baz=7')
|
153
|
+
).should be_false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe ":host" do
|
158
|
+
it 'matches when it is the same' do
|
159
|
+
subject[:host].matches?(
|
160
|
+
request_with(:uri => 'http://foo.com/bar'),
|
161
|
+
request_with(:uri => 'http://foo.com/car')
|
162
|
+
).should be_true
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'does not match when it is not the same' do
|
166
|
+
subject[:host].matches?(
|
167
|
+
request_with(:uri => 'http://foo.com/bar'),
|
168
|
+
request_with(:uri => 'http://goo.com/bar')
|
169
|
+
).should be_false
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe ":path" do
|
174
|
+
it 'matches when it is the same' do
|
175
|
+
subject[:path].matches?(
|
176
|
+
request_with(:uri => 'http://foo.com/bar?a=8'),
|
177
|
+
request_with(:uri => 'http://goo.com/bar?a=9')
|
178
|
+
).should be_true
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'does not match when it is not the same' do
|
182
|
+
subject[:path].matches?(
|
183
|
+
request_with(:uri => 'http://foo.com/bar?a=8'),
|
184
|
+
request_with(:uri => 'http://foo.com/car?a=8')
|
185
|
+
).should be_false
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe ":body" do
|
190
|
+
it 'matches when it is the same' do
|
191
|
+
subject[:body].matches?(
|
192
|
+
request_with(:body => 'foo'),
|
193
|
+
request_with(:body => 'foo')
|
194
|
+
).should be_true
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'does not match when it is not the same' do
|
198
|
+
subject[:body].matches?(
|
199
|
+
request_with(:body => 'foo'),
|
200
|
+
request_with(:body => 'bar')
|
201
|
+
).should be_false
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
describe ":headers" do
|
206
|
+
it 'matches when it is the same' do
|
207
|
+
subject[:headers].matches?(
|
208
|
+
request_with(:headers => { 'a' => 1, 'b' => 2 }),
|
209
|
+
request_with(:headers => { 'b' => 2, 'a' => 1 })
|
210
|
+
).should be_true
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'does not match when it is not the same' do
|
214
|
+
subject[:headers].matches?(
|
215
|
+
request_with(:headers => { 'a' => 3, 'b' => 2 }),
|
216
|
+
request_with(:headers => { 'b' => 2, 'a' => 1 })
|
217
|
+
).should be_false
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|