mustwin-vcr 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/features/about_these_examples.md +18 -0
  3. data/features/cassettes/allow_unused_http_interactions.feature +100 -0
  4. data/features/cassettes/automatic_re_recording.feature +72 -0
  5. data/features/cassettes/decompress.feature +74 -0
  6. data/features/cassettes/dynamic_erb.feature +100 -0
  7. data/features/cassettes/exclusive.feature +126 -0
  8. data/features/cassettes/format.feature +323 -0
  9. data/features/cassettes/freezing_time.feature +68 -0
  10. data/features/cassettes/naming.feature +28 -0
  11. data/features/cassettes/no_cassette.feature +152 -0
  12. data/features/cassettes/update_content_length_header.feature +112 -0
  13. data/features/configuration/allow_http_connections_when_no_cassette.feature +55 -0
  14. data/features/configuration/cassette_library_dir.feature +31 -0
  15. data/features/configuration/debug_logging.feature +59 -0
  16. data/features/configuration/default_cassette_options.feature +100 -0
  17. data/features/configuration/filter_sensitive_data.feature +153 -0
  18. data/features/configuration/hook_into.feature +172 -0
  19. data/features/configuration/ignore_request.feature +192 -0
  20. data/features/configuration/preserve_exact_body_bytes.feature +108 -0
  21. data/features/configuration/query_parser.feature +84 -0
  22. data/features/configuration/uri_parser.feature +89 -0
  23. data/features/getting_started.md +82 -0
  24. data/features/hooks/after_http_request.feature +58 -0
  25. data/features/hooks/around_http_request.feature +57 -0
  26. data/features/hooks/before_http_request.feature +63 -0
  27. data/features/hooks/before_playback.feature +184 -0
  28. data/features/hooks/before_record.feature +172 -0
  29. data/features/http_libraries/em_http_request.feature +250 -0
  30. data/features/http_libraries/net_http.feature +179 -0
  31. data/features/middleware/faraday.feature +56 -0
  32. data/features/middleware/rack.feature +92 -0
  33. data/features/record_modes/all.feature +82 -0
  34. data/features/record_modes/new_episodes.feature +79 -0
  35. data/features/record_modes/none.feature +72 -0
  36. data/features/record_modes/once.feature +95 -0
  37. data/features/request_matching/README.md +30 -0
  38. data/features/request_matching/body.feature +91 -0
  39. data/features/request_matching/body_as_json.feature +90 -0
  40. data/features/request_matching/custom_matcher.feature +135 -0
  41. data/features/request_matching/headers.feature +85 -0
  42. data/features/request_matching/host.feature +95 -0
  43. data/features/request_matching/identical_request_sequence.feature +89 -0
  44. data/features/request_matching/method.feature +96 -0
  45. data/features/request_matching/path.feature +96 -0
  46. data/features/request_matching/playback_repeats.feature +98 -0
  47. data/features/request_matching/query.feature +97 -0
  48. data/features/request_matching/uri.feature +94 -0
  49. data/features/request_matching/uri_without_param.feature +101 -0
  50. data/features/step_definitions/cli_steps.rb +193 -0
  51. data/features/support/env.rb +44 -0
  52. data/features/support/http_lib_filters.rb +53 -0
  53. data/features/test_frameworks/cucumber.feature +211 -0
  54. data/features/test_frameworks/rspec_macro.feature +81 -0
  55. data/features/test_frameworks/rspec_metadata.feature +150 -0
  56. data/features/test_frameworks/test_unit.feature +49 -0
  57. data/lib/vcr.rb +347 -0
  58. data/lib/vcr/cassette.rb +291 -0
  59. data/lib/vcr/cassette/erb_renderer.rb +55 -0
  60. data/lib/vcr/cassette/http_interaction_list.rb +108 -0
  61. data/lib/vcr/cassette/migrator.rb +118 -0
  62. data/lib/vcr/cassette/persisters.rb +42 -0
  63. data/lib/vcr/cassette/persisters/file_system.rb +64 -0
  64. data/lib/vcr/cassette/serializers.rb +57 -0
  65. data/lib/vcr/cassette/serializers/json.rb +48 -0
  66. data/lib/vcr/cassette/serializers/psych.rb +48 -0
  67. data/lib/vcr/cassette/serializers/syck.rb +61 -0
  68. data/lib/vcr/cassette/serializers/yaml.rb +50 -0
  69. data/lib/vcr/configuration.rb +555 -0
  70. data/lib/vcr/deprecations.rb +109 -0
  71. data/lib/vcr/errors.rb +266 -0
  72. data/lib/vcr/extensions/net_http_response.rb +36 -0
  73. data/lib/vcr/library_hooks.rb +18 -0
  74. data/lib/vcr/library_hooks/excon.rb +27 -0
  75. data/lib/vcr/library_hooks/fakeweb.rb +196 -0
  76. data/lib/vcr/library_hooks/faraday.rb +51 -0
  77. data/lib/vcr/library_hooks/typhoeus.rb +120 -0
  78. data/lib/vcr/library_hooks/typhoeus_0.4.rb +103 -0
  79. data/lib/vcr/library_hooks/webmock.rb +164 -0
  80. data/lib/vcr/middleware/excon.rb +221 -0
  81. data/lib/vcr/middleware/excon/legacy_methods.rb +33 -0
  82. data/lib/vcr/middleware/faraday.rb +118 -0
  83. data/lib/vcr/middleware/rack.rb +79 -0
  84. data/lib/vcr/request_handler.rb +114 -0
  85. data/lib/vcr/request_ignorer.rb +43 -0
  86. data/lib/vcr/request_matcher_registry.rb +149 -0
  87. data/lib/vcr/structs.rb +578 -0
  88. data/lib/vcr/tasks/vcr.rake +9 -0
  89. data/lib/vcr/test_frameworks/cucumber.rb +64 -0
  90. data/lib/vcr/test_frameworks/rspec.rb +47 -0
  91. data/lib/vcr/util/hooks.rb +61 -0
  92. data/lib/vcr/util/internet_connection.rb +43 -0
  93. data/lib/vcr/util/logger.rb +59 -0
  94. data/lib/vcr/util/variable_args_block_caller.rb +13 -0
  95. data/lib/vcr/util/version_checker.rb +48 -0
  96. data/lib/vcr/version.rb +34 -0
  97. data/spec/acceptance/threading_spec.rb +34 -0
  98. data/spec/fixtures/cassette_spec/1_x_cassette.yml +110 -0
  99. data/spec/fixtures/cassette_spec/empty.yml +0 -0
  100. data/spec/fixtures/cassette_spec/example.yml +111 -0
  101. data/spec/fixtures/cassette_spec/with_localhost_requests.yml +111 -0
  102. data/spec/fixtures/fake_example_responses.yml +110 -0
  103. data/spec/fixtures/match_requests_on.yml +187 -0
  104. data/spec/lib/vcr/cassette/erb_renderer_spec.rb +53 -0
  105. data/spec/lib/vcr/cassette/http_interaction_list_spec.rb +295 -0
  106. data/spec/lib/vcr/cassette/migrator_spec.rb +195 -0
  107. data/spec/lib/vcr/cassette/persisters/file_system_spec.rb +69 -0
  108. data/spec/lib/vcr/cassette/persisters_spec.rb +39 -0
  109. data/spec/lib/vcr/cassette/serializers_spec.rb +176 -0
  110. data/spec/lib/vcr/cassette_spec.rb +618 -0
  111. data/spec/lib/vcr/configuration_spec.rb +326 -0
  112. data/spec/lib/vcr/deprecations_spec.rb +85 -0
  113. data/spec/lib/vcr/errors_spec.rb +162 -0
  114. data/spec/lib/vcr/extensions/net_http_response_spec.rb +86 -0
  115. data/spec/lib/vcr/library_hooks/excon_spec.rb +104 -0
  116. data/spec/lib/vcr/library_hooks/fakeweb_spec.rb +169 -0
  117. data/spec/lib/vcr/library_hooks/faraday_spec.rb +68 -0
  118. data/spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb +36 -0
  119. data/spec/lib/vcr/library_hooks/typhoeus_spec.rb +162 -0
  120. data/spec/lib/vcr/library_hooks/webmock_spec.rb +118 -0
  121. data/spec/lib/vcr/library_hooks_spec.rb +51 -0
  122. data/spec/lib/vcr/middleware/faraday_spec.rb +182 -0
  123. data/spec/lib/vcr/middleware/rack_spec.rb +115 -0
  124. data/spec/lib/vcr/request_ignorer_spec.rb +70 -0
  125. data/spec/lib/vcr/request_matcher_registry_spec.rb +345 -0
  126. data/spec/lib/vcr/structs_spec.rb +732 -0
  127. data/spec/lib/vcr/test_frameworks/cucumber_spec.rb +107 -0
  128. data/spec/lib/vcr/test_frameworks/rspec_spec.rb +83 -0
  129. data/spec/lib/vcr/util/hooks_spec.rb +158 -0
  130. data/spec/lib/vcr/util/internet_connection_spec.rb +37 -0
  131. data/spec/lib/vcr/util/version_checker_spec.rb +31 -0
  132. data/spec/lib/vcr/version_spec.rb +27 -0
  133. data/spec/lib/vcr_spec.rb +349 -0
  134. data/spec/monkey_patches.rb +182 -0
  135. data/spec/spec_helper.rb +62 -0
  136. data/spec/support/configuration_stubbing.rb +8 -0
  137. data/spec/support/cucumber_helpers.rb +35 -0
  138. data/spec/support/fixnum_extension.rb +10 -0
  139. data/spec/support/http_library_adapters.rb +289 -0
  140. data/spec/support/limited_uri.rb +21 -0
  141. data/spec/support/ruby_interpreter.rb +7 -0
  142. data/spec/support/shared_example_groups/excon.rb +63 -0
  143. data/spec/support/shared_example_groups/hook_into_http_library.rb +594 -0
  144. data/spec/support/shared_example_groups/request_hooks.rb +59 -0
  145. data/spec/support/sinatra_app.rb +86 -0
  146. data/spec/support/vcr_localhost_server.rb +76 -0
  147. data/spec/support/vcr_stub_helpers.rb +17 -0
  148. metadata +677 -0
@@ -0,0 +1,172 @@
1
+ Feature: hook_into
2
+
3
+ The `hook_into` configuration option determines how VCR hooks into the
4
+ HTTP requests to record and replay them. There are currently 4 valid
5
+ options which support many different HTTP libraries:
6
+
7
+ - :webmock can be used to hook into requests from:
8
+ - Net::HTTP
9
+ - HTTPClient
10
+ - Patron
11
+ - Curb (Curl::Easy, but not Curl::Multi)
12
+ - EM HTTP Request
13
+ - Typhoeus (Typhoeus::Hydra, but not Typhoeus::Easy or Typhoeus::Multi)
14
+ - Excon
15
+ - :typhoeus can be used to hook into itself (as long as you use Typhoeus::Hydra,
16
+ but not Typhoeus::Easy or Typhoeus::Multi).
17
+ - :excon can be used to hook into itself.
18
+ - :faraday can be used to hook into itself.
19
+ - :fakeweb (deprecated) can be used to hook into Net::HTTP requests.
20
+
21
+ There are some addiitonal trade offs to consider when deciding which
22
+ option to use:
23
+
24
+ - WebMock uses extensive monkey patching to hook into supported HTTP
25
+ libraries. No monkey patching is used for Typhoeus, Excon or Faraday.
26
+ - Typhoeus, Excon, Faraday can be used together, and with either FakeWeb or WebMock.
27
+ - FakeWeb and WebMock cannot both be used at the same time.
28
+
29
+ Regardless of which library you use, VCR takes care of all of the configuration
30
+ for you. You should not need to interact directly with FakeWeb, WebMock or the
31
+ stubbing facilities of Typhoeus, Excon or Faraday. If/when you decide to change stubbing
32
+ libraries (i.e. if you initially use FakeWeb because it's faster but later need the
33
+ additional features of WebMock) you can change the `hook_into` configuration
34
+ option and it'll work with no other changes required.
35
+
36
+ Scenario Outline: Record and replay a request using each supported hook_into/http library combination
37
+ Given a file named "hook_into_http_lib_combo.rb" with:
38
+ """ruby
39
+ include_http_adapter_for("<http_lib>")
40
+
41
+ require 'vcr'
42
+ VCR.configure { |c| c.ignore_localhost = true }
43
+
44
+ $server = start_sinatra_app do
45
+ get('/') { ARGV[0] }
46
+ end
47
+
48
+ puts "The response for request 1 was: #{response_body_for(:get, "http://localhost:#{$server.port}/")}"
49
+
50
+ VCR.configure do |c|
51
+ <configuration>
52
+ c.cassette_library_dir = 'vcr_cassettes'
53
+ c.ignore_localhost = false
54
+ c.default_cassette_options = { :serialize_with => :syck }
55
+ end
56
+
57
+ VCR.use_cassette('example') do
58
+ puts "The response for request 2 was: #{response_body_for(:get, "http://localhost:#{$server.port}/")}"
59
+ end
60
+ """
61
+ When I run `ruby hook_into_http_lib_combo.rb 'Hello World'`
62
+ Then the output should contain each of the following:
63
+ | The response for request 1 was: Hello World |
64
+ | The response for request 2 was: Hello World |
65
+ And the file "vcr_cassettes/example.yml" should contain "Hello World"
66
+
67
+ When I run `ruby hook_into_http_lib_combo.rb 'Goodbye World'`
68
+ Then the output should contain each of the following:
69
+ | The response for request 1 was: Goodbye World |
70
+ | The response for request 2 was: Hello World |
71
+ And the file "vcr_cassettes/example.yml" should contain "Hello World"
72
+
73
+ Examples:
74
+ | configuration | http_lib |
75
+ | c.hook_into :fakeweb | net/http |
76
+ | c.hook_into :webmock | net/http |
77
+ | c.hook_into :webmock | httpclient |
78
+ | c.hook_into :webmock | curb |
79
+ | c.hook_into :webmock | patron |
80
+ | c.hook_into :webmock | em-http-request |
81
+ | c.hook_into :webmock | typhoeus |
82
+ | c.hook_into :webmock | excon |
83
+ | c.hook_into :typhoeus | typhoeus |
84
+ | c.hook_into :excon | excon |
85
+ | c.hook_into :faraday | faraday (w/ net_http) |
86
+ | c.hook_into :faraday | faraday (w/ typhoeus) |
87
+
88
+ @exclude-jruby @exclude-18
89
+ Scenario Outline: Use Typhoeus, Excon and Faraday in combination with FakeWeb or WebMock
90
+ Given a file named "hook_into_multiple.rb" with:
91
+ """ruby
92
+ require 'typhoeus'
93
+ require 'excon'
94
+ require 'faraday'
95
+ require 'vcr'
96
+ <extra_require>
97
+
98
+ VCR.configure { |c| c.ignore_localhost = true }
99
+
100
+ $server = start_sinatra_app do
101
+ get('/:path') { "#{ARGV[0]} #{params[:path]}" }
102
+ end
103
+
104
+ def net_http_response
105
+ Net::HTTP.get_response('localhost', '/net_http', $server.port).body
106
+ end
107
+
108
+ def typhoeus_response
109
+ Typhoeus::Request.get("http://localhost:#{$server.port}/typhoeus").body
110
+ end
111
+
112
+ def excon_response
113
+ Excon.get("http://localhost:#{$server.port}/excon").body
114
+ end
115
+
116
+ def faraday_response
117
+ Faraday::Connection.new(:url => "http://localhost:#{$server.port}") do |builder|
118
+ builder.adapter :<faraday_adapter>
119
+ end.get('/faraday').body
120
+ end
121
+
122
+ puts "Net::HTTP 1: #{net_http_response}"
123
+ puts "Typhoeus 1: #{typhoeus_response}"
124
+ puts "Excon 1: #{excon_response}"
125
+ puts "Faraday 1: #{faraday_response}"
126
+
127
+ VCR.configure do |c|
128
+ c.hook_into <hook_into>, :typhoeus, :excon, :faraday
129
+ c.cassette_library_dir = 'vcr_cassettes'
130
+ c.ignore_localhost = false
131
+ end
132
+
133
+ VCR.use_cassette('example') do
134
+ puts "Net::HTTP 2: #{net_http_response}"
135
+ puts "Typhoeus 2: #{typhoeus_response}"
136
+ puts "Excon 2: #{excon_response}"
137
+ puts "Faraday 2: #{faraday_response}"
138
+ end
139
+ """
140
+ When I run `ruby hook_into_multiple.rb 'Hello'`
141
+ Then the output should contain each of the following:
142
+ | Net::HTTP 1: Hello net_http |
143
+ | Typhoeus 1: Hello typhoeus |
144
+ | Excon 1: Hello excon |
145
+ | Faraday 1: Hello faraday |
146
+ | Net::HTTP 2: Hello net_http |
147
+ | Typhoeus 2: Hello typhoeus |
148
+ | Excon 2: Hello excon |
149
+ | Faraday 2: Hello faraday |
150
+ And the cassette "vcr_cassettes/example.yml" should have the following response bodies:
151
+ | Hello net_http |
152
+ | Hello typhoeus |
153
+ | Hello excon |
154
+ | Hello faraday |
155
+
156
+ When I run `ruby hook_into_multiple.rb 'Goodbye'`
157
+ Then the output should contain each of the following:
158
+ | Net::HTTP 1: Goodbye net_http |
159
+ | Typhoeus 1: Goodbye typhoeus |
160
+ | Excon 1: Goodbye excon |
161
+ | Faraday 1: Goodbye faraday |
162
+ | Net::HTTP 2: Hello net_http |
163
+ | Typhoeus 2: Hello typhoeus |
164
+ | Excon 2: Hello excon |
165
+ | Faraday 2: Hello faraday |
166
+
167
+ Examples:
168
+ | hook_into | faraday_adapter | extra_require |
169
+ | :fakeweb | net_http | |
170
+ | :webmock | net_http | |
171
+ | :fakeweb | typhoeus | require 'typhoeus/adapters/faraday' |
172
+ | :webmock | typhoeus | require 'typhoeus/adapters/faraday' |
@@ -0,0 +1,192 @@
1
+ Feature: Ignore Request
2
+
3
+ By default, VCR hooks into every request, either allowing it and recording
4
+ it, or playing back a recorded response, or raising an error to force you
5
+ to deal with the new request. In some situations, you may prefer to have
6
+ VCR ignore some requests.
7
+
8
+ VCR provides 3 configuration options to accomplish this:
9
+
10
+ * `ignore_request { |req| ... }` will ignore any request for which the
11
+ given block returns true.
12
+ * `ignore_hosts 'foo.com', 'bar.com'` allows you to specify particular
13
+ hosts to ignore.
14
+ * `ignore_localhost = true` is equivalent to `ignore_hosts 'localhost',
15
+ '127.0.0.1', '0.0.0.0'`. It is particularly useful for when you use
16
+ VCR with a javascript-enabled capybara driver, since capybara boots
17
+ your rack app and makes localhost requests to it to check that it has
18
+ booted.
19
+
20
+ Ignored requests are not recorded and are always allowed, regardless of
21
+ the record mode, and even outside of a `VCR.use_cassette` block.
22
+
23
+ Background:
24
+ Given a file named "sinatra_app.rb" with:
25
+ """ruby
26
+ response_count = 0
27
+ $server = start_sinatra_app do
28
+ get('/') { "Port 7777 Response #{response_count += 1}" }
29
+ end
30
+ """
31
+
32
+ @exclude-jruby
33
+ Scenario Outline: ignore requests to a specific port
34
+ Given a file named "ignore_request.rb" with:
35
+ """ruby
36
+ include_http_adapter_for("<http_lib>")
37
+ require 'sinatra_app.rb'
38
+
39
+ response_count = 0
40
+ $server_8888 = start_sinatra_app do
41
+ get('/') { "Port 8888 Response #{response_count += 1}" }
42
+ end
43
+
44
+ require 'vcr'
45
+
46
+ VCR.configure do |c|
47
+ c.ignore_request do |request|
48
+ URI(request.uri).port == $server.port
49
+ end
50
+
51
+ c.default_cassette_options = { :serialize_with => :syck }
52
+ c.cassette_library_dir = 'cassettes'
53
+ <configuration>
54
+ end
55
+
56
+ VCR.use_cassette('example') do
57
+ puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
58
+ end
59
+
60
+ VCR.use_cassette('example') do
61
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
62
+ end
63
+
64
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
65
+ puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
66
+ """
67
+ When I run `ruby ignore_request.rb`
68
+ Then it should fail with an error like:
69
+ """
70
+ An HTTP request has been made that VCR does not know how to handle:
71
+ """
72
+ And the output should contain:
73
+ """
74
+ Port 8888 Response 1
75
+ Port 7777 Response 1
76
+ Port 7777 Response 2
77
+ """
78
+ And the file "cassettes/example.yml" should contain "Port 8888"
79
+ And the file "cassettes/example.yml" should not contain "Port 7777"
80
+
81
+ Examples:
82
+ | configuration | http_lib |
83
+ | c.hook_into :fakeweb | net/http |
84
+ | c.hook_into :webmock | net/http |
85
+ | c.hook_into :typhoeus | typhoeus |
86
+ | c.hook_into :faraday | faraday (w/ net_http) |
87
+
88
+ Scenario Outline: ignored host requests are not recorded and are always allowed
89
+ Given a file named "ignore_hosts.rb" with:
90
+ """ruby
91
+ include_http_adapter_for("<http_lib>")
92
+ require 'sinatra_app.rb'
93
+
94
+ require 'vcr'
95
+
96
+ VCR.configure do |c|
97
+ c.ignore_hosts '127.0.0.1', 'localhost'
98
+ c.cassette_library_dir = 'cassettes'
99
+ <configuration>
100
+ end
101
+
102
+ VCR.use_cassette('example') do
103
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
104
+ end
105
+
106
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
107
+ """
108
+ When I run `ruby ignore_hosts.rb`
109
+ Then it should pass with:
110
+ """
111
+ Port 7777 Response 1
112
+ Port 7777 Response 2
113
+ """
114
+ And the file "cassettes/example.yml" should not exist
115
+
116
+ Examples:
117
+ | configuration | http_lib |
118
+ | c.hook_into :fakeweb | net/http |
119
+ | c.hook_into :webmock | net/http |
120
+ | c.hook_into :typhoeus | typhoeus |
121
+ | c.hook_into :excon | excon |
122
+ | c.hook_into :faraday | faraday (w/ net_http) |
123
+
124
+ @exclude-jruby
125
+ Scenario Outline: localhost requests are not treated differently by default
126
+ Given a file named "localhost_not_ignored.rb" with:
127
+ """ruby
128
+ include_http_adapter_for("<http_lib>")
129
+ require 'sinatra_app.rb'
130
+
131
+ require 'vcr'
132
+
133
+ VCR.configure do |c|
134
+ c.cassette_library_dir = 'cassettes'
135
+ c.default_cassette_options = { :serialize_with => :syck }
136
+ <configuration>
137
+ end
138
+
139
+ VCR.use_cassette('localhost') do
140
+ response_body_for(:get, "http://localhost:#{$server.port}/")
141
+ end
142
+
143
+ response_body_for(:get, "http://localhost:#{$server.port}/")
144
+ """
145
+ When I run `ruby localhost_not_ignored.rb`
146
+ Then it should fail with "An HTTP request has been made that VCR does not know how to handle"
147
+ And the file "cassettes/localhost.yml" should contain "Port 7777 Response 1"
148
+
149
+ Examples:
150
+ | configuration | http_lib |
151
+ | c.hook_into :fakeweb | net/http |
152
+ | c.hook_into :webmock | net/http |
153
+ | c.hook_into :typhoeus | typhoeus |
154
+ | c.hook_into :excon | excon |
155
+ | c.hook_into :faraday | faraday (w/ net_http) |
156
+
157
+ Scenario Outline: localhost requests are allowed and not recorded when ignore_localhost = true
158
+ Given a file named "ignore_localhost_true.rb" with:
159
+ """ruby
160
+ include_http_adapter_for("<http_lib>")
161
+ require 'sinatra_app.rb'
162
+
163
+ require 'vcr'
164
+
165
+ VCR.configure do |c|
166
+ c.ignore_localhost = true
167
+ c.cassette_library_dir = 'cassettes'
168
+ <configuration>
169
+ end
170
+
171
+ VCR.use_cassette('localhost') do
172
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
173
+ end
174
+
175
+ puts response_body_for(:get, "http://localhost:#{$server.port}/")
176
+ """
177
+ When I run `ruby ignore_localhost_true.rb`
178
+ Then it should pass with:
179
+ """
180
+ Port 7777 Response 1
181
+ Port 7777 Response 2
182
+ """
183
+ And the file "cassettes/localhost.yml" should not exist
184
+
185
+ Examples:
186
+ | configuration | http_lib |
187
+ | c.hook_into :fakeweb | net/http |
188
+ | c.hook_into :webmock | net/http |
189
+ | c.hook_into :typhoeus | typhoeus |
190
+ | c.hook_into :excon | excon |
191
+ | c.hook_into :faraday | faraday (w/ net_http) |
192
+
@@ -0,0 +1,108 @@
1
+ @exclude-18
2
+ Feature: Preserve Exact Body Bytes
3
+
4
+ Some HTTP servers are not well-behaved and respond with invalid data: the response body may
5
+ not be encoded according to the encoding specified in the HTTP headers, or there may be bytes
6
+ that are invalid for the given encoding. The YAML and JSON serializers are not generally
7
+ designed to handle these cases gracefully, and you may get errors when the cassette is serialized
8
+ or deserialized. Also, the encoding may not be preserved when round-tripped through the
9
+ serializer.
10
+
11
+ VCR provides a configuration option to deal with cases like these. The `preserve_exact_body_bytes`
12
+ method accepts a block that VCR will use to determine if the body of the given request or response object
13
+ should be base64 encoded in order to preserve the bytes exactly as-is. VCR does not do this by
14
+ default, since base64-encoding the string removes the human readibility of the cassette.
15
+
16
+ Alternately, if you want to force an entire cassette to preserve the exact body bytes,
17
+ you can pass the `:preserve_exact_body_bytes => true` cassette option when inserting your
18
+ cassette.
19
+
20
+ Scenario: Preserve exact bytes for response body with invalid encoding
21
+ Given a file named "preserve.rb" with:
22
+ """ruby
23
+ # encoding: utf-8
24
+ string = "abc \xFA"
25
+ puts "Valid encoding: #{string.valid_encoding?}"
26
+
27
+ $server = start_sinatra_app do
28
+ get('/') { string }
29
+ end
30
+
31
+ require 'vcr'
32
+
33
+ VCR.configure do |c|
34
+ c.cassette_library_dir = 'cassettes'
35
+ c.hook_into :webmock
36
+ c.preserve_exact_body_bytes do |http_message|
37
+ http_message.body.encoding.name == 'ASCII-8BIT' ||
38
+ !http_message.body.valid_encoding?
39
+ end
40
+ end
41
+
42
+ def make_request(label)
43
+ puts
44
+ puts label
45
+ VCR.use_cassette('example', :serialize_with => :json) do
46
+ body = Net::HTTP.get_response('localhost', '/', $server.port).body
47
+ puts "Body: #{body.inspect}"
48
+ end
49
+ end
50
+
51
+ make_request("Recording:")
52
+ make_request("Playback:")
53
+ """
54
+ When I run `ruby preserve.rb`
55
+ Then the output should contain exactly:
56
+ """
57
+ Valid encoding: false
58
+
59
+ Recording:
60
+ Body: "abc \xFA"
61
+
62
+ Playback:
63
+ Body: "abc \xFA"
64
+
65
+ """
66
+ And the file "cassettes/example.json" should contain:
67
+ """
68
+ "body":{"encoding":"ASCII-8BIT","base64_string":"YWJjIPo=\n"}
69
+ """
70
+
71
+ Scenario: Preserve exact bytes for cassette with `:preserve_exact_body_bytes` option
72
+ Given a file named "preserve.rb" with:
73
+ """ruby
74
+ $server = start_sinatra_app do
75
+ get('/') { "Hello World" }
76
+ end
77
+
78
+ require 'vcr'
79
+
80
+ VCR.configure do |c|
81
+ c.cassette_library_dir = 'cassettes'
82
+ c.hook_into :webmock
83
+ c.default_cassette_options = { :serialize_with => :json }
84
+
85
+ c.before_record do |i|
86
+ # otherwise Ruby 2.0 will default to UTF-8:
87
+ i.response.body.force_encoding('US-ASCII')
88
+ end
89
+ end
90
+
91
+ VCR.use_cassette('preserve_bytes', :preserve_exact_body_bytes => true) do
92
+ Net::HTTP.get_response('localhost', '/', $server.port)
93
+ end
94
+
95
+ VCR.use_cassette('dont_preserve_bytes') do
96
+ Net::HTTP.get_response('localhost', '/', $server.port)
97
+ end
98
+ """
99
+ When I run `ruby preserve.rb`
100
+ Then the file "cassettes/preserve_bytes.json" should contain:
101
+ """
102
+ "body":{"encoding":"US-ASCII","base64_string":"SGVsbG8gV29ybGQ=\n"}
103
+ """
104
+ And the file "cassettes/dont_preserve_bytes.json" should contain:
105
+ """
106
+ "body":{"encoding":"US-ASCII","string":"Hello World"}
107
+ """
108
+