File: errors_spec.rb

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 (194 lines) | stat: -rw-r--r-- 8,532 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
185
186
187
188
189
190
191
192
193
194
require 'spec_helper'

module VCR
  module Errors
    describe UnhandledHTTPRequestError do
      def message_for(request_values = {})
        described_class.new(request_with request_values).message
      end
      alias message message_for

      def request_with(options)
        VCR::Request.new(*options.values_at(*VCR::Request.members))
      end

      it 'identifies the request by method and URI' do
        expect(message_for(:method => :post, :uri => 'http://foo.com/')).to include(
          'POST http://foo.com/'
        )
      end

      context 'when there is no cassette' do
        it 'identifies the request by its body when the default_cassette_options include the body in the match_requests_on option' do
          VCR.configuration.default_cassette_options[:match_requests_on] = [:body]

          expect(message_for(:body => 'param=val1')).to include(
            "Body: param=val1"
          )
        end

        it 'identifies the request by its headers when the default_cassette_options include the headers in the match_requests_on option' do
          VCR.configuration.default_cassette_options[:match_requests_on] = [:headers]

          expect(message_for(:headers => { "Content-Type" => "application/json", "X-Custom" => ["123", "ab\"c"]})).to include(
            'Content-Type: "application/json"',
            'X-Custom: "123"',
            'X-Custom: "ab\"c"'
          )
        end

        it 'mentions that there is no cassette' do
          expect(message).to include('There is currently no cassette in use.')
        end

        it 'mentions that the request can be recorded by inserting a cassette' do
          expect(message).to match(/record this request and play it back.*VCR.use_cassette/m)
        end

        it 'mentions the allow_http_connections_when_no_cassette option' do
          expect(message).to include('allow_http_connections_when_no_cassette')
        end

        it 'mentions that the request can be ignored' do
          expect(message).to include('set an `ignore_request` callback')
        end

        it 'does not double-insert the asterisks for the bullet points' do
          expect(message).not_to match(/\s+\*\s+\*/)
        end

        it 'mentions the debug logging configuration option' do
          expect(message).to include('debug_logger')
        end
      end

      context 'when there are cassettes' do
        it 'identifies the request by its body when the match_requests_on option includes the body' do
          VCR.use_cassette('example', :match_requests_on => [:body]) do
            expect(message_for(:body => 'param=val1')).to include(
              "Body: param=val1"
            )
          end
        end

        it 'identifies the request by its headers when the match_requests_on option includes the headers' do
          VCR.use_cassette('example', :match_requests_on => [:headers]) do
            expect(message_for(:headers => { "Content-Type" => "application/json", "X-Custom" => ["123", "ab\"c"]})).to include(
              'Content-Type: "application/json"',
              'X-Custom: "123"',
              'X-Custom: "ab\"c"'
            )
          end
        end

        it 'does not identify the request by its body when the cassette match_requests_on option does not include the body but the default_cassette_options do' do
          VCR.configuration.default_cassette_options[:match_requests_on] = [:body]
          VCR.use_cassette('example', :match_requests_on => [:uri]) do
            expect(message_for(:body => 'param=val1')).to_not match(/body/i)
          end
        end

        it 'mentions the details about the single cassette when there is one cassette' do
          VCR.use_cassette('example') do
            expect(message).to match(/VCR is currently using the following cassette:.+example.yml/m)
          end
        end

        it 'mentions the details about all cassettes when there are a few cassettes' do
          VCR.use_cassette('example') do
            VCR.use_cassette('sample') do
              expect(message).to match(/VCR are currently using the following cassettes:.+sample.yml.+example.yml/m)
            end
          end
        end

        it 'mentions that :new_episodes can be used to record the request' do
          VCR.use_cassette('example') do
            expect(message).to include('use the :new_episodes record mode')
          end
        end

        it 'mentions that :once does not allow a cassette to be re-recorded' do
          VCR.use_cassette('example', :record => :once) do
            expect(message).to include('(:once) does not allow new requests to be recorded')
          end
        end

        it 'mentions that :none does not allow any recording' do
          VCR.use_cassette('example', :record => :none) do
            expect(message).to include('(:none) does not allow requests to be recorded')
          end
        end

        it 'does not mention the :once or :none record modes if using the :new_episodes record mode' do
          VCR.use_cassette('example', :record => :new_episodes) do
            expect(message).not_to include(':once', ':none')
          end
        end

        it 'does not mention the :once or :none record modes if using the :new_episodes record mode at least in one cassette' do
          VCR.use_cassette('example', :record => :new_episodes) do
            VCR.use_cassette('sample') do
              expect(message).not_to include('current record mode (:once)', 'current record mode (:none)')
            end
          end
        end

        it 'mentions :allow_playback_repeats if the cassette has a used matching interaction' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:has_used_interaction_matching?)
            allow(cassette.http_interactions).to receive(:has_used_interaction_matching?).and_return(true)
            expect(message).to include('allow_playback_repeats')
          end
        end

        it 'does not mention :allow_playback_repeats if the cassette does not have a used matching interaction' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:has_used_interaction_matching?)
            allow(cassette.http_interactions).to receive(:has_used_interaction_matching?).and_return(false)
            expect(message).not_to include('allow_playback_repeats')
          end
        end

        it 'does not mention using a different :match_requests_on option when there are no remaining unused interactions' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:remaining_unused_interaction_count)
            allow(cassette.http_interactions).to receive(:remaining_unused_interaction_count).and_return(0)
            expect(message).not_to include('match_requests_on cassette option')
          end
        end

        it 'mentions using a different :match_requests_on option when there are some remaining unused interactions' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:remaining_unused_interaction_count)
            allow(cassette.http_interactions).to receive(:remaining_unused_interaction_count).and_return(1)
            expect(message).to include('match_requests_on cassette option')
          end
        end

        it 'uses the singular (HTTP interaction) when there is only 1 left' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:remaining_unused_interaction_count)
            allow(cassette.http_interactions).to receive(:remaining_unused_interaction_count).and_return(1)
            expect(message).to include('1 HTTP interaction ')
          end
        end

        it 'uses the plural (HTTP interactions) when there is more than 1 left' do
          VCR.use_cassette('example') do |cassette|
            expect(cassette.http_interactions).to respond_to(:remaining_unused_interaction_count)
            allow(cassette.http_interactions).to receive(:remaining_unused_interaction_count).and_return(2)
            expect(message).to include('2 HTTP interactions ')
          end
        end

        it 'mentions the debug logging configuration option' do
          VCR.use_cassette('example', :record => :new_episodes) do
            expect(message).to include('debug_logger')
          end
        end
      end
    end
  end
end