File: rack_builder_spec.rb

package info (click to toggle)
ruby-faraday 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,008 kB
  • sloc: ruby: 6,509; sh: 10; makefile: 8
file content (317 lines) | stat: -rw-r--r-- 9,021 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# frozen_string_literal: true

RSpec.describe Faraday::RackBuilder do
  # mock handler classes
  (Handler = Struct.new(:app)).class_eval do
    def call(env)
      env[:request_headers]['X-Middleware'] ||= ''
      env[:request_headers]['X-Middleware'] += ":#{self.class.name.split('::').last}"
      app.call(env)
    end
  end

  class Apple < Handler
  end

  class Orange < Handler
  end

  class Banana < Handler
  end

  subject { conn.builder }
  before { Faraday.default_adapter = :test }
  after { Faraday.default_adapter = nil }

  context 'with default stack' do
    let(:conn) { Faraday::Connection.new }

    it { expect(subject[0]).to eq(Faraday::Request.lookup_middleware(:url_encoded)) }
    it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(Faraday.default_adapter)) }
  end

  context 'with custom empty block' do
    let(:conn) { Faraday::Connection.new {} }

    it { expect(subject[0]).to be_nil }
    it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(Faraday.default_adapter)) }
  end

  context 'with custom adapter only' do
    let(:conn) do
      Faraday::Connection.new do |builder|
        builder.adapter :test do |stub|
          stub.get('/') { |_| [200, {}, ''] }
        end
      end
    end

    it { expect(subject[0]).to be_nil }
    it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(:test)) }
  end

  context 'with custom handler and adapter' do
    let(:conn) do
      Faraday::Connection.new do |builder|
        builder.use Apple
        builder.adapter :test do |stub|
          stub.get('/') { |_| [200, {}, ''] }
        end
      end
    end

    it 'locks the stack after making a request' do
      expect(subject.locked?).to be_falsey
      conn.get('/')
      expect(subject.locked?).to be_truthy
      expect { subject.use(Orange) }.to raise_error(Faraday::RackBuilder::StackLocked)
    end

    it 'dup stack is unlocked' do
      expect(subject.locked?).to be_falsey
      subject.lock!
      expect(subject.locked?).to be_truthy
      dup = subject.dup
      expect(dup).to eq(subject)
      expect(dup.locked?).to be_falsey
    end

    it 'allows to compare handlers' do
      expect(subject.handlers.first).to eq(Faraday::RackBuilder::Handler.new(Apple))
    end
  end

  context 'when having a single handler' do
    let(:conn) { Faraday::Connection.new {} }

    before { subject.use(Apple) }

    it { expect(subject.handlers).to eq([Apple]) }

    it 'allows use' do
      subject.use(Orange)
      expect(subject.handlers).to eq([Apple, Orange])
    end

    it 'allows insert_before' do
      subject.insert_before(Apple, Orange)
      expect(subject.handlers).to eq([Orange, Apple])
    end

    it 'allows insert_after' do
      subject.insert_after(Apple, Orange)
      expect(subject.handlers).to eq([Apple, Orange])
    end

    it 'raises an error trying to use an unregistered symbol' do
      expect { subject.use(:apple) }.to raise_error(Faraday::Error) do |err|
        expect(err.message).to eq(':apple is not registered on Faraday::Middleware')
      end
    end
  end

  context 'when having two handlers' do
    let(:conn) { Faraday::Connection.new {} }

    before do
      subject.use(Apple)
      subject.use(Orange)
    end

    it 'allows insert_before' do
      subject.insert_before(Orange, Banana)
      expect(subject.handlers).to eq([Apple, Banana, Orange])
    end

    it 'allows insert_after' do
      subject.insert_after(Apple, Banana)
      expect(subject.handlers).to eq([Apple, Banana, Orange])
    end

    it 'allows to swap handlers' do
      subject.swap(Apple, Banana)
      expect(subject.handlers).to eq([Banana, Orange])
    end

    it 'allows to delete a handler' do
      subject.delete(Apple)
      expect(subject.handlers).to eq([Orange])
    end
  end

  context 'when adapter is added with named options' do
    after { Faraday.default_adapter_options = {} }
    let(:conn) { Faraday::Connection.new {} }

    let(:cat_adapter) do
      Class.new(Faraday::Adapter) do
        attr_accessor :name

        def initialize(app, name:)
          super(app)
          @name = name
        end
      end
    end

    let(:cat) { subject.adapter.build }

    it 'adds a handler to construct adapter with named options' do
      Faraday.default_adapter = cat_adapter
      Faraday.default_adapter_options = { name: 'Chloe' }
      expect { cat }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(cat.name).to eq 'Chloe'
    end
  end

  context 'when middleware is added with named arguments' do
    let(:conn) { Faraday::Connection.new {} }

    let(:dog_middleware) do
      Class.new(Faraday::Middleware) do
        attr_accessor :name

        def initialize(app, name:)
          super(app)
          @name = name
        end
      end
    end
    let(:dog) do
      subject.handlers.find { |handler| handler == dog_middleware }.build
    end

    it 'adds a handler to construct middleware with options passed to use' do
      subject.use dog_middleware, name: 'Rex'
      expect { dog }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(dog.name).to eq('Rex')
    end
  end

  context 'when a middleware is added with named arguments' do
    let(:conn) { Faraday::Connection.new {} }

    let(:cat_request) do
      Class.new(Faraday::Middleware) do
        attr_accessor :name

        def initialize(app, name:)
          super(app)
          @name = name
        end
      end
    end
    let(:cat) do
      subject.handlers.find { |handler| handler == cat_request }.build
    end

    it 'adds a handler to construct request adapter with options passed to request' do
      Faraday::Request.register_middleware cat_request: cat_request
      subject.request :cat_request, name: 'Felix'
      expect { cat }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(cat.name).to eq('Felix')
    end
  end

  context 'when a middleware is added with named arguments' do
    let(:conn) { Faraday::Connection.new {} }

    let(:fish_response) do
      Class.new(Faraday::Middleware) do
        attr_accessor :name

        def initialize(app, name:)
          super(app)
          @name = name
        end
      end
    end
    let(:fish) do
      subject.handlers.find { |handler| handler == fish_response }.build
    end

    it 'adds a handler to construct response adapter with options passed to response' do
      Faraday::Response.register_middleware fish_response: fish_response
      subject.response :fish_response, name: 'Bubbles'
      expect { fish }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(fish.name).to eq('Bubbles')
    end
  end

  context 'when a plain adapter is added with named arguments' do
    let(:conn) { Faraday::Connection.new {} }

    let(:rabbit_adapter) do
      Class.new(Faraday::Adapter) do
        attr_accessor :name

        def initialize(app, name:)
          super(app)
          @name = name
        end
      end
    end
    let(:rabbit) do
      subject.adapter.build
    end

    it 'adds a handler to construct adapter with options passed to adapter' do
      Faraday::Adapter.register_middleware rabbit_adapter: rabbit_adapter
      subject.adapter :rabbit_adapter, name: 'Thumper'
      expect { rabbit }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(rabbit.name).to eq('Thumper')
    end
  end

  context 'when handlers are directly added or updated' do
    let(:conn) { Faraday::Connection.new {} }

    let(:rock_handler) do
      Class.new do
        attr_accessor :name

        def initialize(_app, name:)
          @name = name
        end
      end
    end
    let(:rock) do
      subject.handlers.find { |handler| handler == rock_handler }.build
    end

    it 'adds a handler to construct adapter with options passed to insert' do
      subject.insert 0, rock_handler, name: 'Stony'
      expect { rock }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(rock.name).to eq('Stony')
    end

    it 'adds a handler with options passed to insert_after' do
      subject.insert_after 0, rock_handler, name: 'Rocky'
      expect { rock }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(rock.name).to eq('Rocky')
    end

    it 'adds a handler with options passed to swap' do
      subject.insert 0, rock_handler, name: 'Flint'
      subject.swap 0, rock_handler, name: 'Chert'
      expect { rock }.to_not output(
        /warning: Using the last argument as keyword parameters is deprecated/
      ).to_stderr
      expect(rock.name).to eq('Chert')
    end
  end
end