File: options_test.rb

package info (click to toggle)
ruby-faraday 0.13.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: ruby: 5,821; sh: 136; makefile: 5
file content (328 lines) | stat: -rw-r--r-- 8,119 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
318
319
320
321
322
323
324
325
326
327
328
require File.expand_path('../helper', __FILE__)

class OptionsTest < Faraday::TestCase
  SubOptions = Class.new(Faraday::Options.new(:sub_a, :sub_b))
  class ParentOptions < Faraday::Options.new(:a, :b, :c)
    options :c => SubOptions
  end

  def test_deep_merge
    sub_opts1 = SubOptions.from(sub_a: 3)
    sub_opts2 = SubOptions.from(sub_b: 4)
    opt1 = ParentOptions.from(a: 1, c: sub_opts1)
    opt2 = ParentOptions.from(b: 2, c: sub_opts2)

    merged = opt1.merge(opt2)

    expected_sub_opts = SubOptions.from(sub_a: 3, sub_b: 4)
    assert_equal merged, ParentOptions.from(a: 1, b: 2, c: expected_sub_opts)
  end

  def test_deep_merge_with_hash
    sub_opts1 = SubOptions.from(sub_a: 3)
    sub_opts2 = { sub_b: 4 }
    opt1 = ParentOptions.from(a: 1, c: sub_opts1)
    opt2 = { b: 2, c: sub_opts2 }

    merged = opt1.merge(opt2)

    expected_sub_opts = SubOptions.from(sub_a: 3, sub_b: 4)
    assert_equal merged, ParentOptions.from(a: 1, b: 2, c: expected_sub_opts)
  end

  def test_deep_merge_with_nil
    sub_opts = SubOptions.new(3, 4)
    options = ParentOptions.new(1, 2, sub_opts)
    assert_equal options.a, 1
    assert_equal options.b, 2
    assert_equal options.c.sub_a, 3
    assert_equal options.c.sub_b, 4

    options2 = ParentOptions.from(b: 5, c: nil)

    merged = options.merge(options2)

    assert_equal merged.b, 5
    assert_equal merged.c, sub_opts
  end

  def test_deep_merge_with_sub_nil
    options = ParentOptions.from(a: 1)

    sub_opts = SubOptions.new(3, 4)
    options2 = ParentOptions.from(b: 2, c: sub_opts)

    assert_equal options.a, 1
    assert_equal options2.b, 2
    assert_equal options2.c.sub_a, 3
    assert_equal options2.c.sub_b, 4

    merged = options.merge(options2)

    assert_equal merged.c, sub_opts
  end

  def test_dup_is_shallow
    sub_opts = SubOptions.from(sub_a: 3)
    opts = ParentOptions.from(b: 1, c: sub_opts)

    duped = opts.dup
    duped.b = 2
    duped.c.sub_a = 4

    assert_equal opts.b, 1
    assert_equal opts.c.sub_a, 4
  end

  def test_deep_dup
    sub_opts = SubOptions.from(sub_a: 3)
    opts = ParentOptions.from(b: 1, c: sub_opts)

    duped = opts.deep_dup
    duped.b = 2
    duped.c.sub_a = 4

    assert_equal opts.b, 1
    assert_equal opts.c.sub_a, 3
  end

  def test_clear
    options = SubOptions.new(1)
    assert !options.empty?
    assert options.clear
    assert options.empty?
  end

  def test_empty
    options = SubOptions.new
    assert options.empty?
    options.sub_a = 1
    assert !options.empty?
    options.delete(:sub_a)
    assert options.empty?
  end

  def test_each_key
    options = ParentOptions.new(1, 2, 3)
    enum = options.each_key
    assert_equal enum.next.to_sym, :a
    assert_equal enum.next.to_sym, :b
    assert_equal enum.next.to_sym, :c
  end

  def test_key?
    options = SubOptions.new
    assert !options.key?(:sub_a)
    options.sub_a = 1
    assert options.key?(:sub_a)
  end

  def test_each_value
    options = ParentOptions.new(1, 2, 3)
    enum = options.each_value
    assert_equal enum.next, 1
    assert_equal enum.next, 2
    assert_equal enum.next, 3
  end

  def test_value?
    options = SubOptions.new
    assert !options.value?(1)
    options.sub_a = 1
    assert options.value?(1)
  end

  def test_request_proxy_setter
    options = Faraday::RequestOptions.new
    assert_nil options.proxy

    assert_raises NoMethodError do
      options[:proxy] = {:booya => 1}
    end

    options[:proxy] = {:user => 'user'}
    assert_kind_of Faraday::ProxyOptions, options.proxy
    assert_equal 'user', options.proxy.user

    options.proxy = nil
    assert_nil options.proxy
  end

  def test_proxy_options_from_string
    options = Faraday::ProxyOptions.from 'http://user:pass@example.org'
    assert_equal 'user', options.user
    assert_equal 'pass', options.password
    assert_kind_of URI, options.uri
    assert_equal '', options.path
    assert_equal 80, options.port
    assert_equal 'example.org', options.host
    assert_equal 'http', options.scheme
  end

  def test_proxy_options_hash_access
    proxy = Faraday::ProxyOptions.from 'http://a%40b:pw%20d@example.org'
    assert_equal 'a@b', proxy[:user]
    assert_equal 'a@b', proxy.user
    assert_equal 'pw d', proxy[:password]
    assert_equal 'pw d', proxy.password
  end

  def test_proxy_options_no_auth
    proxy = Faraday::ProxyOptions.from 'http://example.org'
    assert_nil proxy.user
    assert_nil proxy.password
  end

  def test_from_options
    options = ParentOptions.new(1)

    value = ParentOptions.from(options)
    assert_equal 1, value.a
    assert_nil value.b
  end

  def test_from_options_with_sub_object
    sub = SubOptions.new(1)
    options = ParentOptions.from :a => 1, :c => sub
    assert_kind_of ParentOptions, options
    assert_equal 1, options.a
    assert_nil options.b
    assert_kind_of SubOptions, options.c
    assert_equal 1, options.c.sub_a
  end

  def test_from_hash
    options = ParentOptions.from :a => 1
    assert_kind_of ParentOptions, options
    assert_equal 1, options.a
    assert_nil options.b
  end

  def test_from_hash_with_sub_object
    options = ParentOptions.from :a => 1, :c => {:sub_a => 1}
    assert_kind_of ParentOptions, options
    assert_equal 1, options.a
    assert_nil options.b
    assert_kind_of SubOptions, options.c
    assert_equal 1, options.c.sub_a
  end

  def test_inheritance
    subclass = Class.new(ParentOptions)
    options = subclass.from(:c => {:sub_a => 'hello'})
    assert_kind_of SubOptions, options.c
    assert_equal 'hello', options.c.sub_a
  end

  def test_from_deep_hash
    hash = {:b => 1}
    options = ParentOptions.from :a => hash
    assert_equal 1, options.a[:b]

    hash[:b] = 2
    assert_equal 1, options.a[:b]

    options.a[:b] = 3
    assert_equal 2, hash[:b]
    assert_equal 3, options.a[:b]
  end

  def test_from_nil
    options = ParentOptions.from(nil)
    assert_kind_of ParentOptions, options
    assert_nil options.a
    assert_nil options.b
  end

  def test_invalid_key
    assert_raises NoMethodError do
      ParentOptions.from :invalid => 1
    end
  end

  def test_update
    options = ParentOptions.new(1)
    assert_equal 1, options.a
    assert_nil options.b

    updated = options.update :a => 2, :b => 3
    assert_equal 2, options.a
    assert_equal 3, options.b
    assert_equal options, updated
  end

  def test_delete
    options = ParentOptions.new(1)
    assert_equal 1, options.a
    assert_equal 1, options.delete(:a)
    assert_nil options.a
  end

  def test_merge
    options = ParentOptions.new(1)
    assert_equal 1, options.a
    assert_nil options.b

    dup = options.merge :a => 2, :b => 3
    assert_equal 2, dup.a
    assert_equal 3, dup.b
    assert_equal 1, options.a
    assert_nil options.b
  end

  def test_env_access_member
    e = Faraday::Env.new
    assert_nil e.method
    e.method = :get
    assert_equal :get, e.method
  end

  def test_env_access_symbol_non_member
    e = Faraday::Env.new
    assert_nil e[:custom]
    e[:custom] = :boom
    assert_equal :boom, e[:custom]
  end

  def test_env_access_string_non_member
    e = Faraday::Env.new
    assert_nil e["custom"]
    e["custom"] = :boom
    assert_equal :boom, e["custom"]
  end

  def test_env_fetch_ignores_false
    ssl = Faraday::SSLOptions.new
    ssl.verify = false
    assert !ssl.fetch(:verify, true)
  end

  def test_fetch_grabs_value
    opt = Faraday::SSLOptions.new
    opt.verify = 1
    assert_equal 1, opt.fetch(:verify, false) { |k| :blah }
  end

  def test_fetch_uses_falsey_default
    opt = Faraday::SSLOptions.new
    assert_equal false, opt.fetch(:verify, false) { |k| :blah }
  end

  def test_fetch_accepts_block
    opt = Faraday::SSLOptions.new
    assert_equal "yo :verify", opt.fetch(:verify) { |k| "yo #{k.inspect}"}
  end

  def test_fetch_needs_a_default_if_key_is_missing
    opt = Faraday::SSLOptions.new
    assert_raises Faraday::Options.fetch_error_class do
      opt.fetch :verify
    end
  end

  def test_fetch_works_with_key
    opt = Faraday::SSLOptions.new
    opt.verify = 1
    assert_equal 1, opt.fetch(:verify)
  end
end