File: http_interaction_list_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 (295 lines) | stat: -rw-r--r-- 11,101 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'vcr/util/logger'
require 'vcr/cassette/http_interaction_list'
require 'vcr/request_matcher_registry'
require 'vcr/structs'
require 'support/configuration_stubbing'

module VCR
  class Cassette
    describe HTTPInteractionList do
      include_context "configuration stubbing"

      ::RSpec::Matchers.define :respond_with do |expected|
        match { |a| expected.nil? ? a.nil? : a.body == expected }
      end

      before(:each) do
        allow(VCR).to receive(:request_matchers).and_return(VCR::RequestMatcherRegistry.new)
        allow(config).to receive(:logger).and_return(double.as_null_object)
      end

      def request_with(values)
        VCR::Request.new.tap do |request|
          values.each do |name, value|
            request.send("#{name}=", value)
          end
        end
      end

      def response(body)
        VCR::Response.new.tap do |r|
          r.body = body
          r.status = VCR::ResponseStatus.new(200)
        end
      end

      def interaction(body, request_values)
        VCR::HTTPInteraction.new \
          request_with(request_values),
          response(body)
      end

      let(:original_list_array) do [
        interaction('put response', :method => :put),
        interaction('post response 1', :method => :post),
        interaction('post response 2', :method => :post)
      ] end

      let(:allow_playback_repeats) { false } # the default
      let(:list) { HTTPInteractionList.new(original_list_array, [:method], allow_playback_repeats) }

      describe "#has_used_interaction_matching?" do
        it 'returns false when no interactions have been used' do
          expect(list).not_to have_used_interaction_matching(request_with(:method => :put))
        end

        it 'returns true when there is a matching used interaction (even if there is also an unused one that matches)' do
          list.response_for(request_with(:method => :post))
          expect(list).to have_used_interaction_matching(request_with(:method => :post))
        end

        it 'returns false when none of the used interactions match' do
          list.response_for(request_with(:method => :put))
          expect(list).not_to have_used_interaction_matching(request_with(:method => :post))
        end
      end

      describe "#remaining_unused_interaction_count" do
        it 'returns the number of unused interactions' do
          expect(list.remaining_unused_interaction_count).to eq(3)

          list.response_for(request_with(:method => :get))
          expect(list.remaining_unused_interaction_count).to eq(3)

          list.response_for(request_with(:method => :put))
          expect(list.remaining_unused_interaction_count).to eq(2)

          list.response_for(request_with(:method => :put))
          expect(list.remaining_unused_interaction_count).to eq(2)

          list.response_for(request_with(:method => :post))
          expect(list.remaining_unused_interaction_count).to eq(1)

          list.response_for(request_with(:method => :post))
          expect(list.remaining_unused_interaction_count).to eq(0)

          list.response_for(request_with(:method => :post))
          expect(list.remaining_unused_interaction_count).to eq(0)
        end
      end

      describe "#assert_no_unused_interactions?" do
        it 'should raise a SkippedHTTPRequestError when there are unused interactions left' do
          expect {
            list.assert_no_unused_interactions!
          }.to raise_error(Errors::UnusedHTTPInteractionError)

          list.response_for(request_with(:method => :put))
          expect {
            list.assert_no_unused_interactions!
          }.to raise_error(Errors::UnusedHTTPInteractionError)
        end

        it 'should raise nothing when there are no unused interactions left' do
          [:put, :post, :post].each do |method|
            list.response_for(request_with(:method => method))
          end

          expect {
            list.assert_no_unused_interactions!
          }.not_to raise_error
        end

        context 'when the null logger is in use' do
          before { allow(config).to receive(:logger).and_return(Logger::Null) }

          it 'includes formatted request details in the error message' do
            expect {
              list.assert_no_unused_interactions!
            }.to raise_error(/\[put/)
          end

          it 'includes formatted response details in the error message' do
            expect {
              list.assert_no_unused_interactions!
            }.to raise_error(/\[200 "put response"\]/)
          end
        end
      end

      describe "has_interaction_matching?" do
        it 'returns false when the list is empty' do
          expect(HTTPInteractionList.new([], [:method])).not_to have_interaction_matching(double)
        end

        it 'returns false when there is no matching interaction' do
          expect(list).not_to have_interaction_matching(request_with(:method => :get))
        end

        it 'returns true when there is a matching interaction' do
          expect(list).to have_interaction_matching(request_with(:method => :post))
        end

        it 'does not consume the interactions when they match' do
          expect(list).to have_interaction_matching(request_with(:method => :post))
          expect(list.remaining_unused_interaction_count).to eq(3)
          expect(list).to have_interaction_matching(request_with(:method => :post))
          expect(list.remaining_unused_interaction_count).to eq(3)
        end

        it 'invokes each matcher block to find the matching interaction' do
          VCR.request_matchers.register(:foo) { |r1, r2| true }
          VCR.request_matchers.register(:bar) { |r1, r2| true }

          calls = 0
          VCR.request_matchers.register(:baz) { |r1, r2| calls += 1; calls == 2 }

          list = HTTPInteractionList.new([
            interaction('response', :method => :put)
          ], [:foo, :bar, :baz])

          expect(list).not_to have_interaction_matching(request_with(:method => :post))
          expect(list).to     have_interaction_matching(request_with(:method => :post))
        end

        it "delegates to the parent list when it can't find a matching interaction" do
          parent_list = double(:has_interaction_matching? => true)
          expect(HTTPInteractionList.new( [], [:method], false, parent_list)).to have_interaction_matching(double)
          parent_list = double(:has_interaction_matching? => false)
          expect(HTTPInteractionList.new( [], [:method], false, parent_list)).not_to have_interaction_matching(double)
        end

        context 'when allow_playback_repeats is set to true' do
          let(:allow_playback_repeats) { true }

          it 'considers used interactions' do
            list.response_for(request_with(:method => :put))

            results = 10.times.map do
              list.has_interaction_matching?(request_with(:method => :put))
            end

            expect(results).to eq([true] * 10)
          end
        end

        context 'when allow_playback_repeats is set to false' do
          let(:allow_playback_repeats) { false }

          it 'does not consider used interactions' do
            list.response_for(request_with(:method => :put))

            result = 10.times.map do
              list.has_interaction_matching?(request_with(:method => :put))
            end

            expect(result).to eq([false] * 10)
          end
        end
      end

      describe "#response_for" do
        it 'returns nil when the list is empty' do
          expect(HTTPInteractionList.new([], [:method]).response_for(double)).to respond_with(nil)
        end

        it 'returns nil when there is no matching interaction' do
          response = HTTPInteractionList.new([
            interaction('foo', :method => :post),
            interaction('foo', :method => :put)
          ], [:method]).response_for(
            request_with(:method => :get)
          )

          expect(response).to respond_with(nil)
        end

        it 'returns the first matching interaction' do
          list = HTTPInteractionList.new([
            interaction('put response', :method => :put),
            interaction('post response 1', :method => :post),
            interaction('post response 2', :method => :post)
          ], [:method])

          expect(list.response_for(request_with(:method => :post))).to respond_with("post response 1")
        end

        it 'invokes each matcher block to find the matching interaction' do
          VCR.request_matchers.register(:foo) { |r1, r2| true }
          VCR.request_matchers.register(:bar) { |r1, r2| true }

          calls = 0
          VCR.request_matchers.register(:baz) { |r1, r2| calls += 1; calls == 2 }

          list = HTTPInteractionList.new([
            interaction('response', :method => :put)
          ], [:foo, :bar, :baz])

          expect(list.response_for(request_with(:method => :post))).to respond_with(nil)
          expect(list.response_for(request_with(:method => :post))).to respond_with('response')
        end

        it "delegates to the parent list when it can't find a matching interaction" do
          parent_list = double(:response_for => response('parent'))
          result = HTTPInteractionList.new(
            [], [:method], false, parent_list
          ).response_for(double)

          expect(result).to respond_with('parent')
        end

        it 'consumes the first matching interaction so that it will not be used again' do
          expect(list.response_for(request_with(:method => :post)).body).to eq("post response 1")
          expect(list.response_for(request_with(:method => :post)).body).to eq("post response 2")
        end

        context 'when allow_playback_repeats is set to true' do
          let(:allow_playback_repeats) { true }

          it 'continues to return the response from the last matching interaction when there are no more' do
            list.response_for(request_with(:method => :post))

            results = 10.times.map do
              response = list.response_for(request_with(:method => :post))
              response ? response.body : nil
            end

            expect(results).to eq(["post response 2"] * 10)
          end
        end

        context 'when allow_playback_repeats is set to false' do
          let(:allow_playback_repeats) { false }

          it 'returns nil when there are no more unused interactions' do
            list.response_for(request_with(:method => :post))
            list.response_for(request_with(:method => :post))

            results = 10.times.map do
              list.response_for(request_with(:method => :post))
            end

            expect(results).to eq([nil] * 10)
          end
        end

        it 'does not modify the original interaction array the list was initialized with' do
          original_dup = original_list_array.dup
          list.response_for(request_with(:method => :post))
          expect(original_list_array).to eq original_dup
        end
      end
    end
  end
end