File: before_playback.feature

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (184 lines) | stat: -rw-r--r-- 5,469 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Feature: before_playback hook

  The `before_playback` hook is called before a cassette sets up its
  stubs for playback.

  Your block should accept up to 2 arguments.  The first argument will be
  the HTTP interaction that is about to be used for play back.  The second
  argument will be the current cassette.

  You can also call `#ignore!` on the HTTP interaction to prevent VCR
  from playing it back.

  You can use tags to specify a cassette, otherwise your hook will apply to all cassettes.  Consider this code:

      VCR.configure do |c|
        c.before_playback(:twitter) { ... } # modify the interactions somehow
      end

      VCR.use_cassette('cassette_1', :tag => :twitter) { ... }
      VCR.use_cassette('cassette_2') { ... }

  In this example, the hook would apply to the first cassette but not the
  second cassette.

  Background:
    Given a previously recorded cassette file "cassettes/example.yml" with:
      """
      ---
      http_interactions:
      - request:
          method: get
          uri: http://localhost:7777/
          body:
            encoding: UTF-8
            string: ""
          headers: {}
        response:
          status:
            code: 200
            message: OK
          headers:
            Content-Type:
            - text/html;charset=utf-8
            Content-Length:
            - "20"
          body:
            encoding: UTF-8
            string: previously recorded response
          http_version: "1.1"
        recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
      recorded_with: VCR 2.0.0
      """

  Scenario: Modify played back response
    Given a file named "before_playback_example.rb" with:
      """ruby
      require 'vcr'

      VCR.configure do |c|
        c.hook_into :webmock
        c.cassette_library_dir = 'cassettes'

        c.before_playback do |interaction|
          interaction.response.body = 'response from before_playback'
        end
      end

      VCR.use_cassette('example') do
        response = Net::HTTP.get_response('localhost', '/', 7777)
        puts "Response: #{response.body}"
      end
      """
    When I run `ruby before_playback_example.rb`
    Then it should pass with "Response: response from before_playback"

  Scenario: Modify played back response based on the cassette
    Given a file named "before_playback_example.rb" with:
      """ruby
      require 'vcr'

      VCR.configure do |c|
        c.hook_into :webmock
        c.cassette_library_dir = 'cassettes'

        c.before_playback do |interaction, cassette|
          interaction.response.body = "response for #{cassette.name} cassette"
        end
      end

      VCR.use_cassette('example') do
        response = Net::HTTP.get_response('localhost', '/', 7777)
        puts "Response: #{response.body}"
      end
      """
    When I run `ruby before_playback_example.rb`
    Then it should pass with "Response: response for example cassette"

  Scenario: Prevent playback by ignoring interaction in before_playback hook
    Given a file named "before_playback_ignore.rb" with:
      """ruby
      $server = start_sinatra_app do
        get('/') { "sinatra response" }
      end

      require 'vcr'

      VCR.configure do |c|
        c.hook_into :webmock
        c.cassette_library_dir = 'cassettes'
        c.before_playback { |i| i.ignore! }
      end

      VCR.use_cassette('localhost', :record => :new_episodes, :match_requests_on => [:method, :host, :path]) do
        response = Net::HTTP.get_response('localhost', '/', $server.port)
        puts "Response: #{response.body}"
      end
      """
    When I run `ruby before_playback_ignore.rb`
    Then it should pass with "Response: sinatra response"

  Scenario: Multiple hooks are run in order
    Given a file named "multiple_hooks.rb" with:
      """ruby
      require 'vcr'

      VCR.configure do |c|
        c.hook_into :webmock
        c.cassette_library_dir = 'cassettes'

        c.before_playback { puts "In before_playback hook 1" }
        c.before_playback { puts "In before_playback hook 2" }
      end

      VCR.use_cassette('example', :record => :new_episodes) do
        response = Net::HTTP.get_response('localhost', '/', 7777)
        puts "Response: #{response.body}"
      end
      """
    When I run `ruby multiple_hooks.rb`
    Then it should pass with:
      """
      In before_playback hook 1
      In before_playback hook 2
      Response: previously recorded response
      """

  Scenario: Use tagging to apply hooks to only certain cassettes
    Given a file named "tagged_hooks.rb" with:
      """ruby
      require 'vcr'

      VCR.configure do |c|
        c.hook_into :webmock
        c.cassette_library_dir = 'cassettes'

        c.before_playback(:tag_2) do |i|
          puts "In before_playback hook for tag_2"
        end
      end

      [:tag_1, :tag_2, nil].each do |tag|
        puts
        puts "Using tag: #{tag.inspect}"

        VCR.use_cassette('example', :record => :new_episodes, :tag => tag) do
          response = Net::HTTP.get_response('localhost', '/', 7777)
          puts "Response: #{response.body}"
        end
      end
      """
    When I run `ruby tagged_hooks.rb`
    Then it should pass with:
      """
      Using tag: :tag_1
      Response: previously recorded response

      Using tag: :tag_2
      In before_playback hook for tag_2
      Response: previously recorded response

      Using tag: nil
      Response: previously recorded response
      """