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
@@ -3,47 +3,37 @@ Feature: Faraday middleware
3
3
  VCR provides middleware that can be used with Faraday. You can use this as
4
4
  an alternative to Faraday's built-in test adapter.
5
5
 
6
- To use VCR with Faraday, you should configure VCR to stub with faraday and
7
- use the provided middleware. The middleware should come before the Faraday
8
- HTTP adapter. You should provide the middleware with a block where you set
9
- the cassette name and options. If your block accepts two arguments, the
10
- env hash will be yielded, allowing you to dynamically set the cassette name
11
- and options based on the request environment.
12
-
13
- Background:
14
- Given a file named "env_setup.rb" with:
15
- """
16
- require 'vcr_cucumber_helpers'
6
+ To use VCR with Faraday, simply add the VCR middleware to the Faraday
7
+ connection stack. The middleware should come before the Faraday
8
+ HTTP adapter.
17
9
 
10
+ Scenario Outline: Use Faraday middleware
11
+ Given a file named "faraday_example.rb" with:
12
+ """ruby
18
13
  request_count = 0
19
14
  start_sinatra_app(:port => 7777) do
20
15
  get('/:path') { "Hello #{params[:path]} #{request_count += 1}" }
21
16
  end
22
17
 
18
+ require 'faraday'
23
19
  require 'vcr'
24
20
 
25
- VCR.config do |c|
21
+ VCR.configure do |c|
26
22
  c.cassette_library_dir = 'cassettes'
27
- c.stub_with :faraday
28
23
  end
29
- """
30
-
31
- Scenario Outline: Use Faraday middleware
32
- Given a file named "faraday_example.rb" with:
33
- """
34
- require 'env_setup'
35
24
 
36
25
  conn = Faraday::Connection.new(:url => 'http://localhost:7777') do |builder|
37
- builder.use VCR::Middleware::Faraday do |cassette|
38
- cassette.name 'faraday_example'
39
- cassette.options :record => :new_episodes
40
- end
41
-
26
+ builder.use VCR::Middleware::Faraday
42
27
  builder.adapter :<adapter>
43
28
  end
44
29
 
45
- puts "Response 1: #{conn.get('/foo').body}"
46
- puts "Response 2: #{conn.get('/foo').body}"
30
+ VCR.use_cassette('example') do
31
+ puts "Response 1: #{conn.get('/foo').body}"
32
+ end
33
+
34
+ VCR.use_cassette('example') do
35
+ puts "Response 2: #{conn.get('/foo').body}"
36
+ end
47
37
  """
48
38
  When I run `ruby faraday_example.rb`
49
39
  Then the output should contain:
@@ -51,39 +41,10 @@ Feature: Faraday middleware
51
41
  Response 1: Hello foo 1
52
42
  Response 2: Hello foo 1
53
43
  """
54
- And the file "cassettes/faraday_example.yml" should contain "body: Hello foo 1"
44
+ And the file "cassettes/example.yml" should contain "body: Hello foo 1"
55
45
 
56
46
  Examples:
57
47
  | adapter |
58
48
  | net_http |
59
49
  | typhoeus |
60
50
 
61
- Scenario: Set cassette name based on faraday env
62
- Given a file named "faraday_example.rb" with:
63
- """
64
- require 'env_setup'
65
-
66
- conn = Faraday::Connection.new(:url => 'http://localhost:7777') do |builder|
67
- builder.use VCR::Middleware::Faraday do |cassette, env|
68
- cassette.name env[:url].path.sub(/^\//, '')
69
- cassette.options :record => :new_episodes
70
- end
71
-
72
- builder.adapter :net_http
73
- end
74
-
75
- puts "Response 1: #{conn.get('/foo').body}"
76
- puts "Response 2: #{conn.get('/foo').body}"
77
- puts "Response 3: #{conn.get('/bar').body}"
78
- puts "Response 4: #{conn.get('/bar').body}"
79
- """
80
- When I run `ruby faraday_example.rb`
81
- Then the output should contain:
82
- """
83
- Response 1: Hello foo 1
84
- Response 2: Hello foo 1
85
- Response 3: Hello bar 2
86
- Response 4: Hello bar 2
87
- """
88
- And the file "cassettes/foo.yml" should contain "body: Hello foo 1"
89
- And the file "cassettes/bar.yml" should contain "body: Hello bar 2"
@@ -16,16 +16,14 @@ Feature: Rack
16
16
 
17
17
  Background:
18
18
  Given a file named "remote_server.rb" with:
19
- """
20
- require 'vcr_cucumber_helpers'
21
-
19
+ """ruby
22
20
  request_count = 0
23
21
  start_sinatra_app(:port => 7777) do
24
22
  get('/:path') { "Hello #{params[:path]} #{request_count += 1}" }
25
23
  end
26
24
  """
27
25
  And a file named "client.rb" with:
28
- """
26
+ """ruby
29
27
  require 'remote_server'
30
28
  require 'proxy_server'
31
29
  require 'cgi'
@@ -39,7 +37,7 @@ Feature: Rack
39
37
 
40
38
  Scenario: Use VCR rack middleware to record HTTP responses for a simple rack proxy app
41
39
  Given a file named "proxy_server.rb" with:
42
- """
40
+ """ruby
43
41
  require 'vcr'
44
42
 
45
43
  start_sinatra_app(:port => 8888) do
@@ -51,9 +49,9 @@ Feature: Rack
51
49
  get('/') { Net::HTTP.get_response(URI.parse(params[:url])).body }
52
50
  end
53
51
 
54
- VCR.config do |c|
52
+ VCR.configure do |c|
55
53
  c.cassette_library_dir = 'cassettes'
56
- c.stub_with :fakeweb
54
+ c.hook_into :fakeweb
57
55
  c.allow_http_connections_when_no_cassette = true
58
56
  end
59
57
  """
@@ -67,21 +65,20 @@ Feature: Rack
67
65
 
68
66
  Scenario: Set cassette name based on rack request env
69
67
  Given a file named "proxy_server.rb" with:
70
- """
68
+ """ruby
71
69
  require 'vcr'
72
70
 
73
71
  start_sinatra_app(:port => 8888) do
74
72
  use VCR::Middleware::Rack do |cassette, env|
75
73
  cassette.name env['SERVER_NAME']
76
- cassette.options :record => :new_episodes
77
74
  end
78
75
 
79
76
  get('/') { Net::HTTP.get_response(URI.parse(params[:url])).body }
80
77
  end
81
78
 
82
- VCR.config do |c|
79
+ VCR.configure do |c|
83
80
  c.cassette_library_dir = 'cassettes'
84
- c.stub_with :fakeweb
81
+ c.hook_into :fakeweb
85
82
  c.allow_http_connections_when_no_cassette = true
86
83
  end
87
84
  """
@@ -11,9 +11,7 @@ Feature: :all
11
11
 
12
12
  Background:
13
13
  Given a file named "setup.rb" with:
14
- """
15
- require 'vcr_cucumber_helpers'
16
-
14
+ """ruby
17
15
  start_sinatra_app(:port => 7777) do
18
16
  get('/') { 'Hello' }
19
17
  get('/foo') { 'Goodbye' }
@@ -21,8 +19,8 @@ Feature: :all
21
19
 
22
20
  require 'vcr'
23
21
 
24
- VCR.config do |c|
25
- c.stub_with :fakeweb
22
+ VCR.configure do |c|
23
+ c.hook_into :fakeweb
26
24
  c.cassette_library_dir = 'cassettes'
27
25
  end
28
26
  """
@@ -50,7 +48,7 @@ Feature: :all
50
48
 
51
49
  Scenario: Re-record previously recorded response
52
50
  Given a file named "re_record.rb" with:
53
- """
51
+ """ruby
54
52
  require 'setup'
55
53
 
56
54
  VCR.use_cassette('example', :record => :all) do
@@ -65,7 +63,7 @@ Feature: :all
65
63
 
66
64
  Scenario: Record new request
67
65
  Given a file named "record_new.rb" with:
68
- """
66
+ """ruby
69
67
  require 'setup'
70
68
 
71
69
  VCR.use_cassette('example', :record => :all) do
@@ -11,17 +11,15 @@ Feature: :new_episodes
11
11
 
12
12
  Background:
13
13
  Given a file named "setup.rb" with:
14
- """
15
- require 'vcr_cucumber_helpers'
16
-
14
+ """ruby
17
15
  start_sinatra_app(:port => 7777) do
18
16
  get('/') { 'Hello' }
19
17
  end
20
18
 
21
19
  require 'vcr'
22
20
 
23
- VCR.config do |c|
24
- c.stub_with :fakeweb
21
+ VCR.configure do |c|
22
+ c.hook_into :fakeweb
25
23
  c.cassette_library_dir = 'cassettes'
26
24
  end
27
25
  """
@@ -49,7 +47,7 @@ Feature: :new_episodes
49
47
 
50
48
  Scenario: Previously recorded responses are replayed
51
49
  Given a file named "replay_recorded_response.rb" with:
52
- """
50
+ """ruby
53
51
  require 'setup'
54
52
 
55
53
  VCR.use_cassette('example', :record => :new_episodes) do
@@ -62,7 +60,7 @@ Feature: :new_episodes
62
60
 
63
61
  Scenario: New requests get recorded
64
62
  Given a file named "record_new_requests.rb" with:
65
- """
63
+ """ruby
66
64
  require 'setup'
67
65
 
68
66
  VCR.use_cassette('example', :record => :new_episodes) do
@@ -11,11 +11,11 @@ Feature: :none
11
11
 
12
12
  Background:
13
13
  Given a file named "vcr_config.rb" with:
14
- """
14
+ """ruby
15
15
  require 'vcr'
16
16
 
17
- VCR.config do |c|
18
- c.stub_with :fakeweb
17
+ VCR.configure do |c|
18
+ c.hook_into :fakeweb
19
19
  c.cassette_library_dir = 'cassettes'
20
20
  end
21
21
  """
@@ -43,7 +43,7 @@ Feature: :none
43
43
 
44
44
  Scenario: Previously recorded responses are replayed
45
45
  Given a file named "replay_recorded_response.rb" with:
46
- """
46
+ """ruby
47
47
  require 'vcr_config'
48
48
 
49
49
  VCR.use_cassette('example', :record => :none) do
@@ -56,7 +56,7 @@ Feature: :none
56
56
 
57
57
  Scenario: New requests are prevented
58
58
  Given a file named "prevent_new_request.rb" with:
59
- """
59
+ """ruby
60
60
  require 'vcr_config'
61
61
 
62
62
  VCR.use_cassette('example', :record => :none) do
@@ -64,4 +64,4 @@ Feature: :none
64
64
  end
65
65
  """
66
66
  When I run `ruby prevent_new_request.rb`
67
- Then it should fail with "Real HTTP connections are disabled. Unregistered request: GET http://example.com/bar"
67
+ Then it should fail with "Real HTTP connections are disabled. Request: GET http://example.com:80/bar"
@@ -14,17 +14,15 @@ Feature: :once
14
14
 
15
15
  Background:
16
16
  Given a file named "setup.rb" with:
17
- """
18
- require 'vcr_cucumber_helpers'
19
-
17
+ """ruby
20
18
  start_sinatra_app(:port => 7777) do
21
19
  get('/') { 'Hello' }
22
20
  end
23
21
 
24
22
  require 'vcr'
25
23
 
26
- VCR.config do |c|
27
- c.stub_with :fakeweb
24
+ VCR.configure do |c|
25
+ c.hook_into :fakeweb
28
26
  c.cassette_library_dir = 'cassettes'
29
27
  end
30
28
  """
@@ -52,7 +50,7 @@ Feature: :once
52
50
 
53
51
  Scenario: Previously recorded responses are replayed
54
52
  Given a file named "replay_recorded_response.rb" with:
55
- """
53
+ """ruby
56
54
  require 'setup'
57
55
 
58
56
  VCR.use_cassette('example', :record => :once) do
@@ -65,7 +63,7 @@ Feature: :once
65
63
 
66
64
  Scenario: New requests result in an error when the cassette file exists
67
65
  Given a file named "error_for_new_requests_when_cassette_exists.rb" with:
68
- """
66
+ """ruby
69
67
  require 'setup'
70
68
 
71
69
  VCR.use_cassette('example', :record => :once) do
@@ -78,7 +76,7 @@ Feature: :once
78
76
 
79
77
  Scenario: New requests get recorded when there is no cassette file
80
78
  Given a file named "record_new_requests.rb" with:
81
- """
79
+ """ruby
82
80
  require 'setup'
83
81
 
84
82
  VCR.use_cassette('example', :record => :once) do
@@ -0,0 +1,28 @@
1
+ In order to properly replay previously recorded requests, VCR must match new
2
+ HTTP requests to a previously recorded one. By default, it matches on HTTP
3
+ method and URI, since that is usually deterministic and fully identifies the
4
+ resource and action for typical RESTful APIs.
5
+
6
+ You can customize how VCR matches requests using the `:match_requests_on` cassette option.
7
+ Specify an array of attributes to match on. Supported attributes are:
8
+
9
+ - `:method` - The HTTP method (i.e. GET, POST, PUT or DELETE) of the request.
10
+ - `:uri` - The full URI of the request.
11
+ - `:host` - The host of the URI. You can use this (alone, or in combination
12
+ with `:path`) as an alternative to `:uri` to cause VCR to match using a regex
13
+ that matches the host.
14
+ - `:path` - The path of the URI. You can use this (alone, or in combination
15
+ with `:host`) as an alternative to `:uri` to cause VCR to match using a regex
16
+ that matches the path.
17
+ - `:body` - The body of the request.
18
+ - `:headers` - The request headers.
19
+
20
+ You can also register a custom request matcher. This particularly comes
21
+ in handy for dealing with APIs that use non-deterministic URIs (i.e. by
22
+ including a timestamp as a query parameter or whatever).
23
+
24
+ When a cassette contains multiple HTTP interactions that match a request
25
+ based on the configured `:match_requests_on` setting, the responses are
26
+ sequenced: the first matching request will get the first response,
27
+ the second matching request will get the second response, etc.
28
+
@@ -0,0 +1,81 @@
1
+ Feature: Matching on Body
2
+
3
+ Use the `:body` request matcher to match requests on the request body.
4
+
5
+ Background:
6
+ Given a previously recorded cassette file "cassettes/example.yml" with:
7
+ """
8
+ ---
9
+ - !ruby/struct:VCR::HTTPInteraction
10
+ request: !ruby/struct:VCR::Request
11
+ method: :post
12
+ uri: http://example.net:80/some/long/path
13
+ body: body1
14
+ headers:
15
+ response: !ruby/struct:VCR::Response
16
+ status: !ruby/struct:VCR::ResponseStatus
17
+ code: 200
18
+ message: OK
19
+ headers:
20
+ content-length:
21
+ - "14"
22
+ body: body1 response
23
+ http_version: "1.1"
24
+ - !ruby/struct:VCR::HTTPInteraction
25
+ request: !ruby/struct:VCR::Request
26
+ method: :post
27
+ uri: http://example.net:80/some/long/path
28
+ body: body2
29
+ headers:
30
+ response: !ruby/struct:VCR::Response
31
+ status: !ruby/struct:VCR::ResponseStatus
32
+ code: 200
33
+ message: OK
34
+ headers:
35
+ content-length:
36
+ - "14"
37
+ body: body2 response
38
+ http_version: "1.1"
39
+ """
40
+
41
+ Scenario Outline: Replay interaction that matches the body
42
+ And a file named "body_matching.rb" with:
43
+ """ruby
44
+ include_http_adapter_for("<http_lib>")
45
+
46
+ require 'vcr'
47
+
48
+ VCR.configure do |c|
49
+ <configuration>
50
+ c.cassette_library_dir = 'cassettes'
51
+ end
52
+
53
+ VCR.use_cassette('example', :match_requests_on => [:body]) do
54
+ puts "Response for body2: " + response_body_for(:put, "http://example.com/", "body2")
55
+ end
56
+
57
+ VCR.use_cassette('example', :match_requests_on => [:body]) do
58
+ puts "Response for body1: " + response_body_for(:put, "http://example.com/", "body1")
59
+ end
60
+ """
61
+ When I run `ruby body_matching.rb`
62
+ Then it should pass with:
63
+ """
64
+ Response for body2: body2 response
65
+ Response for body1: body1 response
66
+ """
67
+
68
+ Examples:
69
+ | configuration | http_lib |
70
+ | c.hook_into :fakeweb | net/http |
71
+ | c.hook_into :webmock | net/http |
72
+ | c.hook_into :webmock | httpclient |
73
+ | c.hook_into :webmock | curb |
74
+ | c.hook_into :webmock | patron |
75
+ | c.hook_into :webmock | em-http-request |
76
+ | c.hook_into :webmock | typhoeus |
77
+ | c.hook_into :typhoeus | typhoeus |
78
+ | c.hook_into :excon | excon |
79
+ | | faraday (w/ net_http) |
80
+ | | faraday (w/ typhoeus) |
81
+
@@ -0,0 +1,125 @@
1
+ Feature: Register and use a custom matcher
2
+
3
+ If the built-in matchers do not meet your needs, you can use a custom matcher.
4
+
5
+ Any 2-argument callable (that is, an object that responds to #call and accepts
6
+ 2 arguments) can be a matcher. Simply put the callable in your
7
+ `:match_requests_on` array.
8
+
9
+ In addition, you can register a named custom matcher with VCR, and use
10
+ the name in your `:match_requests_on` array.
11
+
12
+ Either way, your custom matcher should return a truthy value if the
13
+ given requests should be considered equivalent.
14
+
15
+ Background:
16
+ Given a previously recorded cassette file "cassettes/example.yml" with:
17
+ """
18
+ ---
19
+ - !ruby/struct:VCR::HTTPInteraction
20
+ request: !ruby/struct:VCR::Request
21
+ method: :get
22
+ uri: http://foo.com:9000/foo
23
+ body:
24
+ headers:
25
+ response: !ruby/struct:VCR::Response
26
+ status: !ruby/struct:VCR::ResponseStatus
27
+ code: 200
28
+ message: OK
29
+ headers:
30
+ content-length:
31
+ - "18"
32
+ body: port 9000 response
33
+ http_version: "1.1"
34
+ - !ruby/struct:VCR::HTTPInteraction
35
+ request: !ruby/struct:VCR::Request
36
+ method: :get
37
+ uri: http://foo.com:8000/foo
38
+ body:
39
+ headers:
40
+ response: !ruby/struct:VCR::Response
41
+ status: !ruby/struct:VCR::ResponseStatus
42
+ code: 200
43
+ message: OK
44
+ headers:
45
+ content-length:
46
+ - "18"
47
+ body: port 8000 response
48
+ http_version: "1.1"
49
+ """
50
+
51
+ Scenario Outline: Use a callable as a custom request matcher
52
+ And a file named "callable_matcher.rb" with:
53
+ """ruby
54
+ include_http_adapter_for("<http_lib>")
55
+
56
+ require 'vcr'
57
+
58
+ VCR.configure do |c|
59
+ <configuration>
60
+ c.cassette_library_dir = 'cassettes'
61
+ end
62
+
63
+ port_matcher = lambda do |request_1, request_2|
64
+ URI(request_1.uri).port == URI(request_2.uri).port
65
+ end
66
+
67
+ VCR.use_cassette('example', :match_requests_on => [:method, port_matcher]) do
68
+ puts "Response for port 8000: " + response_body_for(:get, "http://example.com:8000/")
69
+ end
70
+
71
+ VCR.use_cassette('example', :match_requests_on => [:method, port_matcher]) do
72
+ puts "Response for port 9000: " + response_body_for(:get, "http://example.com:9000/")
73
+ end
74
+ """
75
+ When I run `ruby callable_matcher.rb`
76
+ Then it should pass with:
77
+ """
78
+ Response for port 8000: port 8000 response
79
+ Response for port 9000: port 9000 response
80
+ """
81
+
82
+ Examples:
83
+ | configuration | http_lib |
84
+ | c.hook_into :fakeweb | net/http |
85
+ | c.hook_into :webmock | net/http |
86
+ | c.hook_into :webmock | httpclient |
87
+ | c.hook_into :webmock | curb |
88
+ | c.hook_into :webmock | patron |
89
+ | c.hook_into :webmock | em-http-request |
90
+ | c.hook_into :webmock | typhoeus |
91
+ | c.hook_into :typhoeus | typhoeus |
92
+ | c.hook_into :excon | excon |
93
+ | | faraday (w/ net_http) |
94
+ | | faraday (w/ typhoeus) |
95
+
96
+ Scenario: Register a named custom matcher
97
+ And a file named "register_custom_matcher.rb" with:
98
+ """ruby
99
+ include_http_adapter_for("net/http")
100
+
101
+ require 'vcr'
102
+
103
+ VCR.configure do |c|
104
+ c.hook_into :fakeweb
105
+ c.cassette_library_dir = 'cassettes'
106
+ c.register_request_matcher :port do |request_1, request_2|
107
+ URI(request_1.uri).port == URI(request_2.uri).port
108
+ end
109
+ end
110
+
111
+ VCR.use_cassette('example', :match_requests_on => [:method, :port]) do
112
+ puts "Response for port 8000: " + response_body_for(:get, "http://example.com:8000/")
113
+ end
114
+
115
+ VCR.use_cassette('example', :match_requests_on => [:method, :port]) do
116
+ puts "Response for port 9000: " + response_body_for(:get, "http://example.com:9000/")
117
+ end
118
+ """
119
+ When I run `ruby register_custom_matcher.rb`
120
+ Then it should pass with:
121
+ """
122
+ Response for port 8000: port 8000 response
123
+ Response for port 9000: port 9000 response
124
+ """
125
+