vcr 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +29 -2
  5. data/Gemfile +3 -3
  6. data/README.md +55 -11
  7. data/Rakefile +45 -4
  8. data/features/.nav +6 -0
  9. data/features/{README.md → about_the_cucumber_features.md} +1 -1
  10. data/features/cassettes/update_content_length_header.feature +106 -0
  11. data/features/configuration/default_cassette_options.feature +20 -2
  12. data/features/configuration/filter_sensitive_data.feature +154 -0
  13. data/features/getting_started.md +67 -0
  14. data/features/record_modes/all.feature +4 -2
  15. data/features/record_modes/new_episodes.feature +8 -2
  16. data/features/record_modes/none.feature +4 -2
  17. data/features/record_modes/once.feature +92 -0
  18. data/features/step_definitions/cli_steps.rb +18 -0
  19. data/lib/vcr.rb +13 -6
  20. data/lib/vcr/cassette.rb +36 -15
  21. data/lib/vcr/config.rb +14 -2
  22. data/lib/vcr/deprecations/cassette.rb +29 -0
  23. data/lib/vcr/deprecations/config.rb +18 -0
  24. data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +9 -0
  25. data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +11 -0
  26. data/lib/vcr/http_stubbing_adapters/common.rb +1 -1
  27. data/lib/vcr/http_stubbing_adapters/fakeweb.rb +2 -7
  28. data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +1 -1
  29. data/lib/vcr/middleware/common.rb +3 -5
  30. data/lib/vcr/rspec.rb +1 -38
  31. data/lib/vcr/structs/http_interaction.rb +29 -0
  32. data/lib/vcr/structs/request.rb +6 -0
  33. data/lib/vcr/structs/response.rb +4 -0
  34. data/lib/vcr/{cucumber_tags.rb → test_frameworks/cucumber.rb} +10 -2
  35. data/lib/vcr/test_frameworks/rspec.rb +37 -0
  36. data/lib/vcr/util/basic_object.rb +32 -28
  37. data/lib/vcr/{hooks.rb → util/hooks.rb} +3 -6
  38. data/lib/vcr/util/internet_connection.rb +1 -1
  39. data/lib/vcr/util/ping.rb +21 -17
  40. data/lib/vcr/util/variable_args_block_caller.rb +12 -0
  41. data/lib/vcr/util/yaml.rb +11 -0
  42. data/lib/vcr/version.rb +1 -1
  43. data/script/FullBuildRakeFile +7 -0
  44. data/spec/monkey_patches.rb +0 -7
  45. data/spec/spec_helper.rb +40 -8
  46. data/spec/support/http_library_adapters.rb +0 -262
  47. data/spec/support/shared_example_groups/http_library.rb +256 -0
  48. data/spec/support/{http_stubbing_adapter.rb → shared_example_groups/http_stubbing_adapter.rb} +15 -3
  49. data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +28 -0
  50. data/spec/support/{normalizers.rb → shared_example_groups/normalizers.rb} +3 -3
  51. data/spec/support/{version_checker.rb → shared_example_groups/version_checking.rb} +1 -1
  52. data/spec/vcr/cassette_spec.rb +80 -28
  53. data/spec/vcr/config_spec.rb +55 -8
  54. data/spec/vcr/deprecations/cassette_spec.rb +57 -0
  55. data/spec/vcr/deprecations/config_spec.rb +30 -0
  56. data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +7 -0
  57. data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +16 -0
  58. data/spec/vcr/extensions/net_http_response_spec.rb +1 -3
  59. data/spec/vcr/extensions/net_http_spec.rb +1 -3
  60. data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +1 -4
  61. data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +1 -4
  62. data/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb +1 -4
  63. data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +1 -3
  64. data/spec/vcr/middleware/faraday_spec.rb +4 -4
  65. data/spec/vcr/middleware/rack_spec.rb +4 -4
  66. data/spec/vcr/structs/http_interaction_spec.rb +61 -0
  67. data/spec/vcr/structs/request_spec.rb +20 -2
  68. data/spec/vcr/structs/response_spec.rb +23 -1
  69. data/spec/vcr/structs/response_status_spec.rb +1 -1
  70. data/spec/vcr/{cucumber_tags_spec.rb → test_frameworks/cucumber_spec.rb} +12 -8
  71. data/spec/vcr/{rspec_spec.rb → test_frameworks/rspec_spec.rb} +0 -0
  72. data/spec/vcr/{hooks_spec.rb → util/hooks_spec.rb} +3 -3
  73. data/spec/vcr/util/internet_connection_spec.rb +3 -3
  74. data/spec/vcr_spec.rb +3 -3
  75. data/vcr.gemspec +5 -5
  76. metadata +149 -131
  77. data/Gemfile.lock +0 -155
  78. data/lib/vcr/deprecations.rb +0 -54
  79. data/spec/support/disable_warnings.rb +0 -12
  80. data/spec/support/temp_cassette_library_dir.rb +0 -16
  81. data/spec/support/webmock_macros.rb +0 -14
  82. data/spec/vcr/deprecations_spec.rb +0 -139
File without changes
data/.gitignore CHANGED
@@ -26,3 +26,8 @@ features/fixtures/vcr_cassettes/**/temp/
26
26
  .bundle
27
27
  tmp
28
28
  rerun.txt
29
+ Gemfile.lock
30
+
31
+ features/README.md
32
+ features/CHANGELOG.md
33
+ features/LICENSE
@@ -0,0 +1 @@
1
+ script: "rake ci:build"
@@ -2,7 +2,34 @@
2
2
 
3
3
  ## In git
4
4
 
5
- [Full Changelog](http://github.com/myronmarston/vcr/compare/v1.6.0...master)
5
+ [Full Changelog](http://github.com/myronmarston/vcr/compare/v1.7.0...master)
6
+
7
+ ## 1.7.0 (March 1, 2011)
8
+
9
+ [Full Changelog](http://github.com/myronmarston/vcr/compare/v1.6.0...v1.7.0)
10
+
11
+ * Use Psych for YAML serialization/deserialization when it is available.
12
+ Syck, Ruby's old YAML engine, will remove whitespace from some
13
+ strings. Bug reported by [Robert Poor](https://github.com/rdpoor).
14
+ * Add new `:update_content_length_header` cassette option. The option
15
+ will ensure the `content-length` header value matches the actual
16
+ response body length.
17
+ * Add new `:once` record mode. It operates like `:new_episodes` except
18
+ when the cassette file already exists, in which case it causes
19
+ new requests to raise an error. Feature suggested by
20
+ [Jamie Cobbett](https://github.com/jamiecobbett).
21
+ * Made `:once` the default record mode.
22
+ * Add new `filter_sensitive_data` configuration option. Feature
23
+ suggested by [Nathaniel Bibler](https://github.com/nbibler).
24
+ * Commit to [Semantic Versioning](http://semver.org/). The cucumber
25
+ features document the public API for the purposes of semver.
26
+ * Add support for CI builds using [travis-ci](http://travis-ci.org/myronmarston/vcr).
27
+ * Add support for running tests through `gem test vcr`. Visit
28
+ [test.rubygems.org](http://test.rubygems.org/gems/vcr) to see
29
+ the results.
30
+ * Fix cucumber support to use separate `Before` & `After` hooks rather than
31
+ a single `Around` hook because of a bug in cucumber that prevents
32
+ background steps from running within the `Around` hook.
6
33
 
7
34
  ## 1.6.0 (February 3, 2011)
8
35
 
@@ -20,7 +47,7 @@
20
47
 
21
48
  * Fix response and request serialization so that the headers are raw
22
49
  strings. This fixes intermittent YAML seg faults for paperclip
23
- uploads to S3. But reported by [Rob Slifka](https://github.com/rslifka).
50
+ uploads to S3. Bug reported by [Rob Slifka](https://github.com/rslifka).
24
51
 
25
52
  ## 1.5.0 (January 12, 2011)
26
53
 
data/Gemfile CHANGED
@@ -4,9 +4,9 @@ gemspec
4
4
  group :development do
5
5
  # patron and em-http-request can't install on JRuby, so we have to limit their platform here.
6
6
  platforms :ruby do
7
- gem 'patron', '~> 0.4.6'
7
+ gem 'patron', '0.4.9'
8
8
  gem 'em-http-request', '~> 0.2.7'
9
- gem 'curb', '~> 0.7.8'
9
+ gem 'curb', '0.7.8'
10
10
  gem 'typhoeus', '~> 0.2.1'
11
11
  end
12
12
 
@@ -34,7 +34,7 @@ group :extras do
34
34
 
35
35
  platforms :mri_19 do
36
36
  gem 'ruby-debug19'
37
- gem 'ruby-debug-base19', '0.11.23'
37
+ gem 'ruby-debug-base19', RUBY_VERSION == '1.9.1' ? '0.11.23' : '~> 0.11.24'
38
38
  end
39
39
  end
40
40
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # VCR
1
+ # VCR [![Build Status](http://travis-ci.org/myronmarston/vcr.png)](http://travis-ci.org/myronmarston/vcr)
2
2
 
3
3
  Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
4
4
 
@@ -30,15 +30,20 @@ maintenance) and accurate (the response from example.com will contain the same h
30
30
  ## Features
31
31
 
32
32
  * Automatically records and replays your HTTP interactions with minimal setup/configuration code.
33
- * Supports multiple HTTP stubbing libraries. Currently FakeWeb, WebMock, Typhoeus and Faraday
34
- are supported.
33
+ * Supports and works with the HTTP stubbing facilities of multiple libraries. Currently, the
34
+ following are supported:
35
+ * FakeWeb
36
+ * WebMock
37
+ * Typhoeus
38
+ * Faraday
35
39
  * Supports multiple HTTP libraries:
36
- * [Patron](http://github.com/toland/patron) (WebMock)
37
- * [Curb](http://github.com/taf2/curb) (WebMock -- only supports Curb::Easy at the moment)
38
- * [HTTPClient](http://github.com/nahi/httpclient) (WebMock)
39
- * [em-http-request](http://github.com/igrigorik/em-http-request) (WebMock)
40
- * [Net::HTTP](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html) (FakeWeb and WebMock)
40
+ * [Patron](http://github.com/toland/patron) (when using WebMock)
41
+ * [Curb](http://github.com/taf2/curb) (when using WebMock -- only supports Curb::Easy at the moment)
42
+ * [HTTPClient](http://github.com/nahi/httpclient) (when using WebMock)
43
+ * [em-http-request](http://github.com/igrigorik/em-http-request) (when using WebMock)
44
+ * [Net::HTTP](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html) (when using FakeWeb and WebMock)
41
45
  * [Typhoeus](https://github.com/pauldix/typhoeus) (Typhoeus::Hydra, but not Typhoeus::Easy or Typhoeus::Multi)
46
+ * [Faraday](https://github.com/technoweenie/faraday)
42
47
  * And of course any library built on Net::HTTP, such as [Mechanize](http://github.com/tenderlove/mechanize),
43
48
  [HTTParty](http://github.com/jnunemaker/httparty) or [Rest Client](http://github.com/archiloque/rest-client).
44
49
  * Request matching is configurable based on HTTP method, URI, host, path, body and headers.
@@ -48,6 +53,7 @@ maintenance) and accurate (the response from example.com will contain the same h
48
53
  * Automatically re-records cassettes on a configurable regular interval to keep them fresh and current.
49
54
  * Disables all HTTP requests that you don't explicitly allow.
50
55
  * Simple cucumber integration is provided using tags.
56
+ * Includes convenient RSpec macro.
51
57
  * Known to work well with many popular ruby libraries including RSpec 1 & 2, Cucumber, Test::Unit,
52
58
  Capybara, Mechanize, Rest-Client and HTTParty.
53
59
  * Extensively tested on 7 different ruby interpretters.
@@ -55,13 +61,51 @@ maintenance) and accurate (the response from example.com will contain the same h
55
61
 
56
62
  ## Usage
57
63
 
58
- Browse the [cucumber features](http://relishapp.com/myronmarston/vcr) or visit
59
- the [wiki](http://github.com/myronmarston/vcr/wiki) for usage info and
60
- documentation.
64
+ Browse the [documentation](http://relishapp.com/myronmarston/vcr) for usage info.
61
65
 
62
66
  The [VCR talk given at Philly.rb](http://philly-rb-vcr-talk.heroku.com/) also
63
67
  contains good usage info.
64
68
 
69
+ ## Release Policy
70
+
71
+ VCR follows the principles of [semantic versioning](http://semver.org/).
72
+ The [cucumber features](http://relishapp.com/myronmarston/vcr) define
73
+ VCR's public API. Patch level releases contain only bug fixes. Minor
74
+ releases contain backward-compatible new features. Major new releases
75
+ contain backwards-incompatible changes to the public API.
76
+
77
+ ## Ruby Interpreter Compatibility
78
+
79
+ VCR has been tested on the following ruby interpreters:
80
+
81
+ * MRI 1.8.6
82
+ * MRI 1.8.7
83
+ * MRI 1.9.1
84
+ * MRI 1.9.2
85
+ * REE 1.8.7
86
+ * JRuby 1.5.6
87
+ * Rubinius 1.2.1
88
+
89
+ ## Known Issues
90
+
91
+ * VCR uses YAML to serialize the HTTP interactions to disk in a
92
+ human-readable, human-editable format. Unfortunately there are bugs
93
+ in Syck, Ruby's default YAML engine, that cause it to modify strings
94
+ when serializing them. It appears the the bug is limited to entire
95
+ lines of whitespace. A string such as `"1\n \n2"` will get changed
96
+ to `"1\n\n2"` (see [this gist](https://gist.github.com/815754) for
97
+ example code). In practice, this usually isn't so bad, but it can
98
+ occassionally cause problems, especially when the recorded
99
+ response includes a `content_length` header and you are using an
100
+ HTTP client that relies on this. Mechanize will raise an `EOFError`
101
+ when the `content_length` header does not match the response body
102
+ length. One solution is to use Psych, the new YAML engine included
103
+ in Ruby 1.9. VCR attempts to use Psych if possible, but you may have
104
+ to [re-compile ruby 1.9](http://rhnh.net/2011/01/31/psych-yaml-in-ruby-1-9-2-with-rvm-and-snow-leopard-osx)
105
+ to use it. See [this issue](https://github.com/myronmarston/vcr/issues#issue/43)
106
+ for more info. You can also use the `:update_content_length_header`
107
+ cassette option to ensure the header has the correct value.
108
+
65
109
  ## Development
66
110
 
67
111
  * Source hosted on [GitHub](http://github.com/myronmarston/vcr).
data/Rakefile CHANGED
@@ -1,13 +1,18 @@
1
- require 'bundler'
2
- require 'bundler/setup'
3
- Bundler::GemHelper.install_tasks
1
+ using_git = File.exist?(File.expand_path('../.git/', __FILE__))
2
+
3
+ if using_git
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ Bundler::GemHelper.install_tasks
7
+ end
4
8
 
5
9
  require 'rake'
6
10
  require "rspec/core/rake_task"
7
11
 
8
12
  RSpec::Core::RakeTask.new(:spec) do |t|
9
13
  t.verbose = false
10
- t.rspec_opts = %w[--format progress] if ENV['FULL_BUILD']
14
+ t.skip_bundler = true unless using_git
15
+ t.rspec_opts = %w[--format progress] if (ENV['FULL_BUILD'] || !using_git)
11
16
  end
12
17
 
13
18
  desc "Run all examples using rcov"
@@ -25,8 +30,42 @@ Cucumber::Rake::Task.new
25
30
 
26
31
  task :default => [:spec, :cucumber]
27
32
 
33
+ namespace :ci do
34
+ desc "Sets things up for a ci build on travis-ci.org"
35
+ task :setup do
36
+ sh "git submodule init"
37
+ sh "git submodule update"
38
+ end
39
+
40
+ RSpec::Core::RakeTask.new(:spec) do |t|
41
+ t.verbose = true
42
+ t.rspec_opts = %w[--format progress --backtrace]
43
+ end
44
+
45
+ desc "Run a ci build"
46
+ task :build => [:setup, :spec, :cucumber]
47
+ end
48
+
49
+ def ensure_relish_doc_symlinked(filename)
50
+ from = File.expand_path("../features/#{filename}", __FILE__)
51
+ to = File.expand_path("../#{filename}", __FILE__)
52
+
53
+ if File.symlink?(from)
54
+ return if File.readlink(from) == to
55
+
56
+ # delete the old symlink
57
+ File.unlink(from)
58
+ end
59
+
60
+ FileUtils.ln_s to, from
61
+ end
62
+
28
63
  desc "Push cukes to relishapp using the relish-client-gem"
29
64
  task :relish do
65
+ %w[ README.md CHANGELOG.md LICENSE ].each do |file|
66
+ ensure_relish_doc_symlinked(file)
67
+ end
68
+
30
69
  require 'vcr/version'
31
70
  sh "relish versions:add myronmarston/vcr:#{VCR.version}"
32
71
  sh "relish push vcr:#{VCR.version}"
@@ -34,3 +73,5 @@ end
34
73
 
35
74
  task :release => :relish
36
75
 
76
+ # For gem-test: http://gem-testers.org/
77
+ task :test => :spec
@@ -1,10 +1,16 @@
1
+ getting_started.md (Getting Started)
2
+ CHANGELOG.md (Changelog)
3
+ about_the_cucumber_features.md (About the Cucumber Features)
4
+ LICENSE (License)
1
5
  - cassettes:
2
6
  - format.feature
3
7
  - no_cassette.feature
4
8
  - request_matching.feature
5
9
  - dynamic_erb.feature
6
10
  - automatic_re_recording.feature
11
+ - update_content_length_header.feature
7
12
  - record_modes:
13
+ - once.feature
8
14
  - new_episodes.feature
9
15
  - none.feature
10
16
  - all.feature
@@ -1,4 +1,4 @@
1
- ## VCR
1
+ ## About the Cucumber Features
2
2
 
3
3
  The cucumber features provided here demonstrate all of the major features of
4
4
  VCR. These features are executable documentation for VCR.
@@ -0,0 +1,106 @@
1
+ Feature: Update content_length header
2
+
3
+ When the `:update_content_length_header` option is set to a truthy value,
4
+ VCR will ensure that the `content-length` header will have the correct
5
+ value. This is useful in several situations:
6
+
7
+ - When you manually edit the cassette file and change the resonse body
8
+ length. You can use this option so you don't have to manually calculate
9
+ and update the body length.
10
+ - When you use ERB, the response body length may vary. This will ensure
11
+ it is always correct.
12
+ - Syck, the default YAML engine for ruby 1.8 (and 1.9, unless you compile
13
+ it to use Psych), has a bug where it sometimes will remove some
14
+ whitespace strings when you serialize them. This may cause the
15
+ `content-length` header to have the wrong value.
16
+
17
+ This is especially important when you use a client that checks the
18
+ `content-length` header. Mechanize, for example, will raise an `EOFError`
19
+ when the header value does not match the actual body length.
20
+
21
+ Background:
22
+ Given a previously recorded cassette file "cassettes/example.yml" with:
23
+ """
24
+ ---
25
+ - !ruby/struct:VCR::HTTPInteraction
26
+ request: !ruby/struct:VCR::Request
27
+ method: :get
28
+ uri: http://example.com:80/
29
+ body:
30
+ headers:
31
+ response: !ruby/struct:VCR::Response
32
+ status: !ruby/struct:VCR::ResponseStatus
33
+ code: 200
34
+ message: OK
35
+ headers:
36
+ content-type:
37
+ - text/html;charset=utf-8
38
+ content-length:
39
+ - "11"
40
+ body: Hello <modified>
41
+ http_version: "1.1"
42
+ """
43
+ Given a file named "common_stuff.rb" with:
44
+ """
45
+ require 'vcr'
46
+
47
+ VCR.config do |c|
48
+ c.cassette_library_dir = 'cassettes'
49
+ c.stub_with :fakeweb
50
+ end
51
+
52
+ def make_request_and_print_results
53
+ response = Net::HTTP.get_response('example.com', '/')
54
+ puts "Body length: #{response.body.length}"
55
+ puts "Header value: #{response['content-length']}"
56
+ end
57
+ """
58
+
59
+ Scenario: Default :update_content_length_header setting
60
+ Given a file named "default.rb" with:
61
+ """
62
+ require 'common_stuff'
63
+
64
+ VCR.use_cassette('example', :record => :none) do
65
+ make_request_and_print_results
66
+ end
67
+ """
68
+ When I run "ruby default.rb"
69
+ Then the output should contain:
70
+ """
71
+ Body length: 16
72
+ Header value: 11
73
+ """
74
+
75
+ Scenario: :update_content_length_header => false
76
+ Given a file named "false.rb" with:
77
+ """
78
+ require 'common_stuff'
79
+
80
+ VCR.use_cassette('example', :record => :none, :update_content_length_header => false) do
81
+ make_request_and_print_results
82
+ end
83
+ """
84
+ When I run "ruby false.rb"
85
+ Then the output should contain:
86
+ """
87
+ Body length: 16
88
+ Header value: 11
89
+ """
90
+
91
+ Scenario: :update_content_length_header => true
92
+ Given a file named "true.rb" with:
93
+ """
94
+ require 'common_stuff'
95
+
96
+ VCR.use_cassette('example', :record => :none, :update_content_length_header => true) do
97
+ make_request_and_print_results
98
+ end
99
+ """
100
+ When I run "ruby true.rb"
101
+ Then the output should contain:
102
+ """
103
+ Body length: 16
104
+ Header value: 16
105
+ """
106
+
@@ -21,7 +21,7 @@ Feature: default_cassette_options
21
21
  end
22
22
  """
23
23
 
24
- Scenario: cassettes get default values from configured default_cassette_options
24
+ Scenario: cassettes get default values from configured `default_cassette_options`
25
25
  Given a file named "default_cassette_options.rb" with:
26
26
  """
27
27
  require 'vcr_setup.rb'
@@ -38,7 +38,7 @@ Feature: default_cassette_options
38
38
  ERB: true
39
39
  """
40
40
 
41
- Scenario: :match_requests_on defaults to [:method, :uri] when it has not been set
41
+ Scenario: `:match_requests_on` defaults to `[:method, :uri]` when it has not been set
42
42
  Given a file named "default_cassette_options.rb" with:
43
43
  """
44
44
  require 'vcr_setup.rb'
@@ -50,6 +50,24 @@ Feature: default_cassette_options
50
50
  When I run "ruby default_cassette_options.rb"
51
51
  Then the output should contain "Match Requests On: [:method, :uri]"
52
52
 
53
+ Scenario: `:record` defaults to `:once` when it has not been set
54
+ Given a file named "default_record_mode.rb" with:
55
+ """
56
+ require 'vcr'
57
+
58
+ VCR.config do |c|
59
+ # not important for this example, but must be set to something
60
+ c.stub_with :webmock
61
+ c.cassette_library_dir = 'cassettes'
62
+ end
63
+
64
+ VCR.use_cassette('example') do
65
+ puts "Record mode: #{VCR.current_cassette.record_mode.inspect}"
66
+ end
67
+ """
68
+ When I run "ruby default_record_mode.rb"
69
+ Then the output should contain "Record mode: :once"
70
+
53
71
  Scenario: cassettes can set their own options
54
72
  Given a file named "default_cassette_options.rb" with:
55
73
  """
@@ -0,0 +1,154 @@
1
+ Feature: Filter sensitive data
2
+
3
+ The `filter_sensitive_data` configuration option can be used to prevent
4
+ sensitive data from being written to your cassette files. This may be
5
+ important if you commit your cassettes files to source controla and do
6
+ not want your sensitive data exposed. Pass the following arguments to
7
+ `filter_sensitive_data`:
8
+
9
+ - A substitution string. This is the string that will be written to
10
+ the cassettte file as a placeholder. It should be unique and you
11
+ may want to wrap it in special characters like `{ }` or `< >`.
12
+ - A symbol specifying a tag (optional). If a tag is given, the
13
+ filtering will only be applied to cassettes with the given tag.
14
+ - A block. The block should return the sensitive text that you want
15
+ replaced with the substitution string. If your block accepts an
16
+ argument, the HTTP interaction will be yielded so that you can
17
+ dynamically specify the sensitive text based on the interaction
18
+ (see the last scenario for an example of this).
19
+
20
+ When the interactions are replayed, the sensitive text will replace the
21
+ substitution string so that the interaction will be identical to what was
22
+ originally recorded.
23
+
24
+ You can specify as many filterings as you want.
25
+
26
+ Scenario: Multiple filterings
27
+ Given a file named "filtering.rb" with:
28
+ """
29
+ require 'vcr_cucumber_helpers'
30
+
31
+ if ARGV.include?('--with-server')
32
+ start_sinatra_app(:port => 7777) do
33
+ get('/') { "Hello World" }
34
+ end
35
+ end
36
+
37
+ require 'vcr'
38
+
39
+ VCR.config do |c|
40
+ c.stub_with :fakeweb
41
+ c.cassette_library_dir = 'cassettes'
42
+ c.filter_sensitive_data('<GREETING>') { 'Hello' }
43
+ c.filter_sensitive_data('<LOCATION>') { 'World' }
44
+ end
45
+
46
+ VCR.use_cassette('filtering', :record => :new_episodes) do
47
+ response = Net::HTTP.get_response('localhost', '/', 7777)
48
+ puts "Response: #{response.body}"
49
+ end
50
+ """
51
+ When I run "ruby filtering.rb --with-server"
52
+ Then the output should contain "Response: Hello World"
53
+ And the file "cassettes/filtering.yml" should contain "body: <GREETING> <LOCATION>"
54
+ And the file "cassettes/filtering.yml" should not contain "Hello"
55
+ And the file "cassettes/filtering.yml" should not contain "World"
56
+
57
+ When I run "ruby filtering.rb"
58
+ Then the output should contain "Hello World"
59
+
60
+ Scenario: Filter tagged cassettes
61
+ Given a file named "tagged_filtering.rb" with:
62
+ """
63
+ require 'vcr_cucumber_helpers'
64
+
65
+ if ARGV.include?('--with-server')
66
+ response_count = 0
67
+ start_sinatra_app(:port => 7777) do
68
+ get('/') { "Hello World #{response_count += 1 }" }
69
+ end
70
+ end
71
+
72
+ require 'vcr'
73
+
74
+ VCR.config do |c|
75
+ c.stub_with :fakeweb
76
+ c.cassette_library_dir = 'cassettes'
77
+ c.filter_sensitive_data('<LOCATION>', :my_tag) { 'World' }
78
+ end
79
+
80
+ VCR.use_cassette('tagged', :tag => :my_tag, :record => :new_episodes) do
81
+ response = Net::HTTP.get_response('localhost', '/', 7777)
82
+ puts "Tagged Response: #{response.body}"
83
+ end
84
+
85
+ VCR.use_cassette('untagged', :record => :new_episodes) do
86
+ response = Net::HTTP.get_response('localhost', '/', 7777)
87
+ puts "Untagged Response: #{response.body}"
88
+ end
89
+ """
90
+ When I run "ruby tagged_filtering.rb --with-server"
91
+ Then the output should contain each of the following:
92
+ | Tagged Response: Hello World 1 |
93
+ | Untagged Response: Hello World 2 |
94
+ And the file "cassettes/tagged.yml" should contain "body: Hello <LOCATION> 1"
95
+ And the file "cassettes/untagged.yml" should contain "body: Hello World 2"
96
+
97
+ When I run "ruby tagged_filtering.rb"
98
+ Then the output should contain each of the following:
99
+ | Tagged Response: Hello World 1 |
100
+ | Untagged Response: Hello World 2 |
101
+
102
+ Scenario: Filter dynamic data based on yielded HTTP interaction
103
+ Given a file named "dynamic_filtering.rb" with:
104
+ """
105
+ require 'vcr_cucumber_helpers'
106
+ include_http_adapter_for('net/http')
107
+
108
+ if ARGV.include?('--with-server')
109
+ start_sinatra_app(:port => 7777) do
110
+ helpers do
111
+ def request_header_for(header_key_fragment)
112
+ key = env.keys.find { |k| k =~ /#{header_key_fragment}/i }
113
+ env[key]
114
+ end
115
+ end
116
+
117
+ get('/') { "#{request_header_for('username')}/#{request_header_for('password')}" }
118
+ end
119
+ end
120
+
121
+ require 'vcr'
122
+
123
+ USER_PASSWORDS = {
124
+ 'john.doe' => 'monkey',
125
+ 'jane.doe' => 'cheetah'
126
+ }
127
+
128
+ VCR.config do |c|
129
+ c.stub_with :webmock
130
+ c.cassette_library_dir = 'cassettes'
131
+ c.filter_sensitive_data('<PASSWORD>') do |interaction|
132
+ USER_PASSWORDS[interaction.request.headers['x-http-username'].first]
133
+ end
134
+ end
135
+
136
+ VCR.use_cassette('example', :record => :new_episodes, :match_requests_on => [:method, :uri, :headers]) do
137
+ puts "Response: " + response_body_for(
138
+ :get, 'http://localhost:7777/', nil,
139
+ 'X-HTTP-USERNAME' => 'john.doe',
140
+ 'X-HTTP-PASSWORD' => USER_PASSWORDS['john.doe']
141
+ )
142
+ end
143
+ """
144
+ When I run "ruby dynamic_filtering.rb --with-server"
145
+ Then the output should contain "john.doe/monkey"
146
+ And the file "cassettes/example.yml" should contain "body: john.doe/<PASSWORD>"
147
+ And the file "cassettes/example.yml" should contain a YAML fragment like:
148
+ """
149
+ x-http-password:
150
+ - <PASSWORD>
151
+ """
152
+
153
+ When I run "ruby dynamic_filtering.rb"
154
+ Then the output should contain "john.doe/monkey"