File: ignore_request.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 (266 lines) | stat: -rw-r--r-- 9,003 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
Feature: Ignore Request

  By default, VCR hooks into every request, either allowing it and recording
  it, or playing back a recorded response, or raising an error to force you
  to deal with the new request.  In some situations, you may prefer to have
  VCR ignore some requests.

  VCR provides 3 configuration options to accomplish this:

    * `ignore_request { |req| ... }` will ignore any request for which the
      given block returns true.
    * `ignore_hosts 'foo.com', 'bar.com'` allows you to specify particular
      hosts to ignore.
    * `ignore_localhost = true` is equivalent to `ignore_hosts 'localhost',
      '127.0.0.1', '0.0.0.0'`. It is particularly useful for when you use
      VCR with a javascript-enabled capybara driver, since capybara boots
      your rack app and makes localhost requests to it to check that it has
      booted.
    * `unignore_hosts 'foo.com', 'bar.com'` makes VCR stop ignoring particular
      hosts.

  Ignored requests are not recorded and are always allowed, regardless of
  the record mode, and even outside of a `VCR.use_cassette` block.

  Background:
    Given a file named "sinatra_app.rb" with:
      """ruby
      response_count = 0
      $server = start_sinatra_app do
        get('/') { "Port 7777 Response #{response_count += 1}" }
      end
      """

  @exclude-jruby
  Scenario Outline: ignore requests to a specific port
    Given a file named "ignore_request.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      response_count = 0
      $server_8888 = start_sinatra_app do
        get('/') { "Port 8888 Response #{response_count += 1}" }
      end

      require 'vcr'

      VCR.configure do |c|
        c.ignore_request do |request|
          URI(request.uri).port == $server.port
        end

        c.default_cassette_options = { :serialize_with => :syck }
        c.cassette_library_dir = 'cassettes'
        <configuration>
      end

      VCR.use_cassette('example') do
        puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
      end

      VCR.use_cassette('example') do
        puts response_body_for(:get, "http://localhost:#{$server.port}/")
      end

      puts response_body_for(:get, "http://localhost:#{$server.port}/")
      puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
      """
    When I run `ruby ignore_request.rb`
    Then it should fail with an error like:
      """
      An HTTP request has been made that VCR does not know how to handle:
      """
     And the output should contain:
      """
      Port 8888 Response 1
      Port 7777 Response 1
      Port 7777 Response 2
      """
    And the file "cassettes/example.yml" should contain "Port 8888"
    And the file "cassettes/example.yml" should not contain "Port 7777"

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :faraday  | faraday (w/ net_http) |

  Scenario Outline: ignored host requests are not recorded and are always allowed
    Given a file named "ignore_hosts.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      require 'vcr'

      VCR.configure do |c|
        c.ignore_hosts '127.0.0.1', 'localhost'
        c.cassette_library_dir = 'cassettes'
        <configuration>
      end

      VCR.use_cassette('example') do
        puts response_body_for(:get, "http://localhost:#{$server.port}/")
      end

      puts response_body_for(:get, "http://localhost:#{$server.port}/")
      """
    When I run `ruby ignore_hosts.rb`
    Then it should pass with:
      """
      Port 7777 Response 1
      Port 7777 Response 2
      """
    And the file "cassettes/example.yml" should not exist

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :excon    | excon                 |
      | c.hook_into :faraday  | faraday (w/ net_http) |

  Scenario Outline: unignored host requests are recorded again
    Given a file named "unignore_hosts.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      require 'vcr'

      VCR.configure do |c|
        c.ignore_hosts '127.0.0.1', 'localhost'
        c.cassette_library_dir = 'cassettes'
        <configuration>
      end

      VCR.use_cassette('example') do
        puts response_body_for(:get, "http://localhost:#{$server.port}/")
      end

      VCR.configure do |c|
        c.unignore_hosts '127.0.0.1', 'localhost'
      end

      VCR.use_cassette('example') do
        puts response_body_for(:get, "http://localhost:#{$server.port}/")
      end
      """
    When I run `ruby unignore_hosts.rb`
    Then it should pass with:
      """
      Port 7777 Response 1
      Port 7777 Response 2
      """
    And the file "cassettes/example.yml" should not contain "Response 1"
    And the file "cassettes/example.yml" should contain "Response 2"

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :excon    | excon                 |
      | c.hook_into :faraday  | faraday (w/ net_http) |

  Scenario Outline: unignored host requests are not allowed without a cassette
    Given a file named "unignore_hosts_without_cassette.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      require 'vcr'

      VCR.configure do |c|
        c.ignore_hosts '127.0.0.1', 'localhost'
        c.cassette_library_dir = 'cassettes'
        <configuration>
      end

      puts response_body_for(:get, "http://localhost:#{$server.port}/")

      VCR.configure do |c|
        c.unignore_hosts '127.0.0.1', 'localhost'
      end

      puts response_body_for(:get, "http://localhost:#{$server.port}/")
      """
    When I run `ruby unignore_hosts_without_cassette.rb`
    Then it should fail with "An HTTP request has been made that VCR does not know how to handle"
    And the output should contain "Response 1"
    And the output should not contain "Response 2"
    And the file "cassettes/example.yml" should not exist

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :excon    | excon                 |
      | c.hook_into :faraday  | faraday (w/ net_http) |

  @exclude-jruby
  Scenario Outline: localhost requests are not treated differently by default
    Given a file named "localhost_not_ignored.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      require 'vcr'

      VCR.configure do |c|
        c.cassette_library_dir = 'cassettes'
        c.default_cassette_options = { :serialize_with => :syck }
        <configuration>
      end

      VCR.use_cassette('localhost') do
        response_body_for(:get, "http://localhost:#{$server.port}/")
      end

      response_body_for(:get, "http://localhost:#{$server.port}/")
      """
    When I run `ruby localhost_not_ignored.rb`
    Then it should fail with "An HTTP request has been made that VCR does not know how to handle"
     And the file "cassettes/localhost.yml" should contain "Port 7777 Response 1"

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :excon    | excon                 |
      | c.hook_into :faraday  | faraday (w/ net_http) |

  Scenario Outline: localhost requests are allowed and not recorded when ignore_localhost = true
    Given a file named "ignore_localhost_true.rb" with:
      """ruby
      include_http_adapter_for("<http_lib>")
      require 'sinatra_app.rb'

      require 'vcr'

      VCR.configure do |c|
        c.ignore_localhost = true
        c.cassette_library_dir = 'cassettes'
        <configuration>
      end

      VCR.use_cassette('localhost') do
        puts response_body_for(:get, "http://localhost:#{$server.port}/")
      end

      puts response_body_for(:get, "http://localhost:#{$server.port}/")
      """
    When I run `ruby ignore_localhost_true.rb`
    Then it should pass with:
      """
      Port 7777 Response 1
      Port 7777 Response 2
      """
    And the file "cassettes/localhost.yml" should not exist

    Examples:
      | configuration         | http_lib              |
      | c.hook_into :webmock  | net/http              |
      | c.hook_into :typhoeus | typhoeus              |
      | c.hook_into :excon    | excon                 |
      | c.hook_into :faraday  | faraday (w/ net_http) |