nano-sharp-sys 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/faraday-2.14.3/CHANGELOG.md +574 -0
  3. data/faraday-2.14.3/LICENSE.md +20 -0
  4. data/faraday-2.14.3/README.md +67 -0
  5. data/faraday-2.14.3/Rakefile +12 -0
  6. data/faraday-2.14.3/examples/client_spec.rb +119 -0
  7. data/faraday-2.14.3/examples/client_test.rb +144 -0
  8. data/faraday-2.14.3/lib/faraday/adapter/test.rb +319 -0
  9. data/faraday-2.14.3/lib/faraday/adapter.rb +102 -0
  10. data/faraday-2.14.3/lib/faraday/adapter_registry.rb +30 -0
  11. data/faraday-2.14.3/lib/faraday/connection.rb +567 -0
  12. data/faraday-2.14.3/lib/faraday/encoders/flat_params_encoder.rb +106 -0
  13. data/faraday-2.14.3/lib/faraday/encoders/nested_params_encoder.rb +193 -0
  14. data/faraday-2.14.3/lib/faraday/error.rb +202 -0
  15. data/faraday-2.14.3/lib/faraday/logging/formatter.rb +118 -0
  16. data/faraday-2.14.3/lib/faraday/methods.rb +6 -0
  17. data/faraday-2.14.3/lib/faraday/middleware.rb +72 -0
  18. data/faraday-2.14.3/lib/faraday/middleware_registry.rb +83 -0
  19. data/faraday-2.14.3/lib/faraday/options/connection_options.rb +23 -0
  20. data/faraday-2.14.3/lib/faraday/options/env.rb +204 -0
  21. data/faraday-2.14.3/lib/faraday/options/proxy_options.rb +39 -0
  22. data/faraday-2.14.3/lib/faraday/options/request_options.rb +23 -0
  23. data/faraday-2.14.3/lib/faraday/options/ssl_options.rb +76 -0
  24. data/faraday-2.14.3/lib/faraday/options.rb +219 -0
  25. data/faraday-2.14.3/lib/faraday/parameters.rb +5 -0
  26. data/faraday-2.14.3/lib/faraday/rack_builder.rb +248 -0
  27. data/faraday-2.14.3/lib/faraday/request/authorization.rb +54 -0
  28. data/faraday-2.14.3/lib/faraday/request/instrumentation.rb +58 -0
  29. data/faraday-2.14.3/lib/faraday/request/json.rb +70 -0
  30. data/faraday-2.14.3/lib/faraday/request/url_encoded.rb +60 -0
  31. data/faraday-2.14.3/lib/faraday/request.rb +139 -0
  32. data/faraday-2.14.3/lib/faraday/response/json.rb +74 -0
  33. data/faraday-2.14.3/lib/faraday/response/logger.rb +39 -0
  34. data/faraday-2.14.3/lib/faraday/response/raise_error.rb +83 -0
  35. data/faraday-2.14.3/lib/faraday/response.rb +95 -0
  36. data/faraday-2.14.3/lib/faraday/utils/headers.rb +150 -0
  37. data/faraday-2.14.3/lib/faraday/utils/params_hash.rb +61 -0
  38. data/faraday-2.14.3/lib/faraday/utils.rb +121 -0
  39. data/faraday-2.14.3/lib/faraday/version.rb +5 -0
  40. data/faraday-2.14.3/lib/faraday.rb +158 -0
  41. data/faraday-2.14.3/spec/external_adapters/faraday_specs_setup.rb +14 -0
  42. data/faraday-2.14.3/spec/faraday/adapter/test_spec.rb +460 -0
  43. data/faraday-2.14.3/spec/faraday/adapter_registry_spec.rb +28 -0
  44. data/faraday-2.14.3/spec/faraday/adapter_spec.rb +55 -0
  45. data/faraday-2.14.3/spec/faraday/connection_spec.rb +860 -0
  46. data/faraday-2.14.3/spec/faraday/error_spec.rb +175 -0
  47. data/faraday-2.14.3/spec/faraday/middleware_registry_spec.rb +31 -0
  48. data/faraday-2.14.3/spec/faraday/middleware_spec.rb +213 -0
  49. data/faraday-2.14.3/spec/faraday/options/env_spec.rb +76 -0
  50. data/faraday-2.14.3/spec/faraday/options/options_spec.rb +297 -0
  51. data/faraday-2.14.3/spec/faraday/options/proxy_options_spec.rb +79 -0
  52. data/faraday-2.14.3/spec/faraday/options/request_options_spec.rb +19 -0
  53. data/faraday-2.14.3/spec/faraday/params_encoders/flat_spec.rb +42 -0
  54. data/faraday-2.14.3/spec/faraday/params_encoders/nested_spec.rb +179 -0
  55. data/faraday-2.14.3/spec/faraday/rack_builder_spec.rb +317 -0
  56. data/faraday-2.14.3/spec/faraday/request/authorization_spec.rb +118 -0
  57. data/faraday-2.14.3/spec/faraday/request/instrumentation_spec.rb +74 -0
  58. data/faraday-2.14.3/spec/faraday/request/json_spec.rb +199 -0
  59. data/faraday-2.14.3/spec/faraday/request/url_encoded_spec.rb +93 -0
  60. data/faraday-2.14.3/spec/faraday/request_spec.rb +119 -0
  61. data/faraday-2.14.3/spec/faraday/response/json_spec.rb +206 -0
  62. data/faraday-2.14.3/spec/faraday/response/logger_spec.rb +299 -0
  63. data/faraday-2.14.3/spec/faraday/response/raise_error_spec.rb +286 -0
  64. data/faraday-2.14.3/spec/faraday/response_spec.rb +84 -0
  65. data/faraday-2.14.3/spec/faraday/utils/headers_spec.rb +109 -0
  66. data/faraday-2.14.3/spec/faraday/utils_spec.rb +120 -0
  67. data/faraday-2.14.3/spec/faraday_spec.rb +43 -0
  68. data/faraday-2.14.3/spec/spec_helper.rb +133 -0
  69. data/faraday-2.14.3/spec/support/disabling_stub.rb +14 -0
  70. data/faraday-2.14.3/spec/support/fake_safe_buffer.rb +15 -0
  71. data/faraday-2.14.3/spec/support/faraday_middleware_subclasses.rb +18 -0
  72. data/faraday-2.14.3/spec/support/helper_methods.rb +96 -0
  73. data/faraday-2.14.3/spec/support/shared_examples/adapter.rb +105 -0
  74. data/faraday-2.14.3/spec/support/shared_examples/params_encoder.rb +18 -0
  75. data/faraday-2.14.3/spec/support/shared_examples/request_method.rb +263 -0
  76. data/faraday-2.14.3/spec/support/streaming_response_checker.rb +35 -0
  77. data/nano-sharp-sys.gemspec +12 -0
  78. metadata +117 -0
@@ -0,0 +1,860 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CustomEncoder
4
+ def encode(params)
5
+ params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
6
+ end
7
+
8
+ def decode(params)
9
+ params.split(',').to_h { |pair| pair.split('-') }
10
+ end
11
+ end
12
+
13
+ shared_examples 'initializer with url' do
14
+ context 'with simple url' do
15
+ let(:address) { 'http://httpbingo.org' }
16
+
17
+ it { expect(subject.host).to eq('httpbingo.org') }
18
+ it { expect(subject.port).to eq(80) }
19
+ it { expect(subject.scheme).to eq('http') }
20
+ it { expect(subject.path_prefix).to eq('/') }
21
+ it { expect(subject.params).to eq({}) }
22
+ end
23
+
24
+ context 'with complex url' do
25
+ let(:address) { 'http://httpbingo.org:815/fish?a=1' }
26
+
27
+ it { expect(subject.port).to eq(815) }
28
+ it { expect(subject.path_prefix).to eq('/fish') }
29
+ it { expect(subject.params).to eq('a' => '1') }
30
+ end
31
+
32
+ context 'with IPv6 address' do
33
+ let(:address) { 'http://[::1]:85/' }
34
+
35
+ it { expect(subject.host).to eq('[::1]') }
36
+ it { expect(subject.port).to eq(85) }
37
+ end
38
+ end
39
+
40
+ shared_examples 'default connection options' do
41
+ after { Faraday.default_connection_options = nil }
42
+
43
+ it 'works with implicit url' do
44
+ conn = Faraday.new 'http://httpbingo.org/foo'
45
+ expect(conn.options.timeout).to eq(10)
46
+ end
47
+
48
+ it 'works with option url' do
49
+ conn = Faraday.new url: 'http://httpbingo.org/foo'
50
+ expect(conn.options.timeout).to eq(10)
51
+ end
52
+
53
+ it 'works with instance connection options' do
54
+ conn = Faraday.new 'http://httpbingo.org/foo', request: { open_timeout: 1 }
55
+ expect(conn.options.timeout).to eq(10)
56
+ expect(conn.options.open_timeout).to eq(1)
57
+ end
58
+
59
+ it 'default connection options persist with an instance overriding' do
60
+ conn = Faraday.new 'http://nigiri.com/bar'
61
+ conn.options.timeout = 1
62
+ expect(Faraday.default_connection_options.request.timeout).to eq(10)
63
+
64
+ other = Faraday.new url: 'https://httpbingo.org/foo'
65
+ other.options.timeout = 1
66
+
67
+ expect(Faraday.default_connection_options.request.timeout).to eq(10)
68
+ end
69
+
70
+ it 'default connection uses default connection options' do
71
+ expect(Faraday.default_connection.options.timeout).to eq(10)
72
+ end
73
+ end
74
+
75
+ RSpec.describe Faraday::Connection do
76
+ let(:conn) { Faraday::Connection.new(url, options) }
77
+ let(:url) { nil }
78
+ let(:options) { nil }
79
+
80
+ describe '.new' do
81
+ subject { conn }
82
+
83
+ context 'with implicit url param' do
84
+ # Faraday::Connection.new('http://httpbingo.org')
85
+ let(:url) { address }
86
+
87
+ it_behaves_like 'initializer with url'
88
+ end
89
+
90
+ context 'with explicit url param' do
91
+ # Faraday::Connection.new(url: 'http://httpbingo.org')
92
+ let(:url) { { url: address } }
93
+
94
+ it_behaves_like 'initializer with url'
95
+ end
96
+
97
+ context 'with custom builder' do
98
+ let(:custom_builder) { Faraday::RackBuilder.new }
99
+ let(:options) { { builder: custom_builder } }
100
+
101
+ it { expect(subject.builder).to eq(custom_builder) }
102
+ end
103
+
104
+ context 'with custom params' do
105
+ let(:options) { { params: { a: 1 } } }
106
+
107
+ it { expect(subject.params).to eq('a' => 1) }
108
+ end
109
+
110
+ context 'with custom params and params in url' do
111
+ let(:url) { 'http://httpbingo.org/fish?a=1&b=2' }
112
+ let(:options) { { params: { a: 3 } } }
113
+ it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
114
+ end
115
+
116
+ context 'with basic_auth in url' do
117
+ let(:url) { 'http://Aladdin:open%20sesame@httpbingo.org/fish' }
118
+
119
+ it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
120
+ end
121
+
122
+ context 'with custom headers' do
123
+ let(:options) { { headers: { user_agent: 'Faraday' } } }
124
+
125
+ it { expect(subject.headers['User-agent']).to eq('Faraday') }
126
+ end
127
+
128
+ context 'with ssl false' do
129
+ let(:options) { { ssl: { verify: false } } }
130
+
131
+ it { expect(subject.ssl.verify?).to be_falsey }
132
+ end
133
+
134
+ context 'with verify_hostname false' do
135
+ let(:options) { { ssl: { verify_hostname: false } } }
136
+
137
+ it { expect(subject.ssl.verify_hostname?).to be_falsey }
138
+ end
139
+
140
+ context 'with empty block' do
141
+ let(:conn) { Faraday::Connection.new {} }
142
+
143
+ it { expect(conn.builder.handlers.size).to eq(0) }
144
+ end
145
+
146
+ context 'with block' do
147
+ let(:conn) do
148
+ Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
149
+ faraday.adapter :test
150
+ faraday.url_prefix = 'http://httpbingo.org/omnom'
151
+ end
152
+ end
153
+
154
+ it { expect(conn.builder.handlers.size).to eq(0) }
155
+ it { expect(conn.path_prefix).to eq('/omnom') }
156
+ end
157
+ end
158
+
159
+ describe '#close' do
160
+ before { Faraday.default_adapter = :test }
161
+ after { Faraday.default_adapter = nil }
162
+
163
+ it 'can close underlying app' do
164
+ expect(conn.app).to receive(:close)
165
+ conn.close
166
+ end
167
+ end
168
+
169
+ describe '#build_exclusive_url' do
170
+ context 'with relative path' do
171
+ subject { conn.build_exclusive_url('sake.html') }
172
+
173
+ it 'uses connection host as default host' do
174
+ conn.host = 'httpbingo.org'
175
+ expect(subject.host).to eq('httpbingo.org')
176
+ expect(subject.scheme).to eq('http')
177
+ end
178
+
179
+ it do
180
+ conn.path_prefix = '/fish'
181
+ expect(subject.path).to eq('/fish/sake.html')
182
+ end
183
+
184
+ it do
185
+ conn.path_prefix = '/'
186
+ expect(subject.path).to eq('/sake.html')
187
+ end
188
+
189
+ it do
190
+ conn.path_prefix = 'fish'
191
+ expect(subject.path).to eq('/fish/sake.html')
192
+ end
193
+
194
+ it do
195
+ conn.path_prefix = '/fish/'
196
+ expect(subject.path).to eq('/fish/sake.html')
197
+ end
198
+ end
199
+
200
+ context 'with absolute path' do
201
+ subject { conn.build_exclusive_url('/sake.html') }
202
+
203
+ after { expect(subject.path).to eq('/sake.html') }
204
+
205
+ it { conn.path_prefix = '/fish' }
206
+ it { conn.path_prefix = '/' }
207
+ it { conn.path_prefix = 'fish' }
208
+ it { conn.path_prefix = '/fish/' }
209
+ end
210
+
211
+ context 'with complete url' do
212
+ subject { conn.build_exclusive_url('http://httpbingo.org/sake.html?a=1') }
213
+
214
+ it { expect(subject.scheme).to eq('http') }
215
+ it { expect(subject.host).to eq('httpbingo.org') }
216
+ it { expect(subject.port).to eq(80) }
217
+ it { expect(subject.path).to eq('/sake.html') }
218
+ it { expect(subject.query).to eq('a=1') }
219
+ end
220
+
221
+ it 'overrides connection port for absolute url' do
222
+ conn.port = 23
223
+ uri = conn.build_exclusive_url('http://httpbingo.org')
224
+ expect(uri.port).to eq(80)
225
+ end
226
+
227
+ it 'does not add ending slash given nil url' do
228
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
229
+ uri = conn.build_exclusive_url
230
+ expect(uri.path).to eq('/nigiri')
231
+ end
232
+
233
+ it 'does not add ending slash given empty url' do
234
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
235
+ uri = conn.build_exclusive_url('')
236
+ expect(uri.path).to eq('/nigiri')
237
+ end
238
+
239
+ it 'does not use connection params' do
240
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
241
+ conn.params = { a: 1 }
242
+ expect(conn.build_exclusive_url.to_s).to eq('http://httpbingo.org/nigiri')
243
+ end
244
+
245
+ it 'allows to provide params argument' do
246
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
247
+ conn.params = { a: 1 }
248
+ params = Faraday::Utils::ParamsHash.new
249
+ params[:a] = 2
250
+ uri = conn.build_exclusive_url(nil, params)
251
+ expect(uri.to_s).to eq('http://httpbingo.org/nigiri?a=2')
252
+ end
253
+
254
+ it 'handles uri instances' do
255
+ uri = conn.build_exclusive_url(URI('/sake.html'))
256
+ expect(uri.path).to eq('/sake.html')
257
+ end
258
+
259
+ it 'always returns new URI instance' do
260
+ conn.url_prefix = 'http://httpbingo.org'
261
+ uri1 = conn.build_exclusive_url(nil)
262
+ uri2 = conn.build_exclusive_url(nil)
263
+ expect(uri1).not_to equal(uri2)
264
+ end
265
+
266
+ context 'with url_prefixed connection' do
267
+ let(:url) { 'http://httpbingo.org/get/' }
268
+
269
+ it 'parses url and changes scheme' do
270
+ conn.scheme = 'https'
271
+ uri = conn.build_exclusive_url('sake.html')
272
+ expect(uri.to_s).to eq('https://httpbingo.org/get/sake.html')
273
+ end
274
+
275
+ it 'joins url to base with ending slash' do
276
+ uri = conn.build_exclusive_url('sake.html')
277
+ expect(uri.to_s).to eq('http://httpbingo.org/get/sake.html')
278
+ end
279
+
280
+ it 'used default base with ending slash' do
281
+ uri = conn.build_exclusive_url
282
+ expect(uri.to_s).to eq('http://httpbingo.org/get/')
283
+ end
284
+
285
+ it 'overrides base' do
286
+ uri = conn.build_exclusive_url('/sake/')
287
+ expect(uri.to_s).to eq('http://httpbingo.org/sake/')
288
+ end
289
+ end
290
+
291
+ context 'with colon in path' do
292
+ let(:url) { 'http://service.com' }
293
+
294
+ it 'joins url to base when used absolute path' do
295
+ conn = Faraday.new(url: url)
296
+ uri = conn.build_exclusive_url('/service:search?limit=400')
297
+ expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
298
+ end
299
+
300
+ it 'joins url to base when used relative path' do
301
+ conn = Faraday.new(url: url)
302
+ uri = conn.build_exclusive_url('service:search?limit=400')
303
+ expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
304
+ end
305
+
306
+ it 'joins url to base when used with path prefix' do
307
+ conn = Faraday.new(url: url)
308
+ conn.path_prefix = '/api'
309
+ uri = conn.build_exclusive_url('service:search?limit=400')
310
+ expect(uri.to_s).to eq('http://service.com/api/service:search?limit=400')
311
+ end
312
+ end
313
+
314
+ context 'with protocol-relative URL (GHSA-33mh-2634-fwr2)' do
315
+ it 'does not allow host override with //evil.com/path' do
316
+ conn.url_prefix = 'http://httpbingo.org/api'
317
+ uri = conn.build_exclusive_url('//evil.com/path')
318
+ expect(uri.host).to eq('httpbingo.org')
319
+ end
320
+
321
+ it 'does not allow host override with URI("//evil.com/path")' do
322
+ conn.url_prefix = 'http://httpbingo.org/api'
323
+ uri = conn.build_exclusive_url(URI('//evil.com/path?token=1'))
324
+ expect(uri.host).to eq('httpbingo.org')
325
+ expect(uri.query).to eq('token=1')
326
+ end
327
+
328
+ it 'does not allow host override with //evil.com:8080/path' do
329
+ conn.url_prefix = 'http://httpbingo.org/api'
330
+ uri = conn.build_exclusive_url('//evil.com:8080/path')
331
+ expect(uri.host).to eq('httpbingo.org')
332
+ end
333
+
334
+ it 'does not allow host override with //user:pass@evil.com/path' do
335
+ conn.url_prefix = 'http://httpbingo.org/api'
336
+ uri = conn.build_exclusive_url('//user:pass@evil.com/path')
337
+ expect(uri.host).to eq('httpbingo.org')
338
+ end
339
+
340
+ it 'does not allow host override with ///evil.com' do
341
+ conn.url_prefix = 'http://httpbingo.org/api'
342
+ uri = conn.build_exclusive_url('///evil.com')
343
+ expect(uri.host).to eq('httpbingo.org')
344
+ end
345
+
346
+ it 'still allows single-slash absolute paths' do
347
+ conn.url_prefix = 'http://httpbingo.org/api'
348
+ uri = conn.build_exclusive_url('/safe/path')
349
+ expect(uri.host).to eq('httpbingo.org')
350
+ expect(uri.path).to eq('/safe/path')
351
+ end
352
+ end
353
+
354
+ context 'with a custom `default_uri_parser`' do
355
+ let(:url) { 'http://httpbingo.org' }
356
+ let(:parser) { Addressable::URI }
357
+
358
+ around do |example|
359
+ with_default_uri_parser(parser) do
360
+ example.run
361
+ end
362
+ end
363
+
364
+ it 'does not raise error' do
365
+ expect { conn.build_exclusive_url('/nigiri') }.not_to raise_error
366
+ end
367
+ end
368
+ end
369
+
370
+ describe '#build_url' do
371
+ let(:url) { 'http://httpbingo.org/nigiri' }
372
+
373
+ it 'uses params' do
374
+ conn.params = { a: 1, b: 1 }
375
+ expect(conn.build_url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=1')
376
+ end
377
+
378
+ it 'merges params' do
379
+ conn.params = { a: 1, b: 1 }
380
+ url = conn.build_url(nil, b: 2, c: 3)
381
+ expect(url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=2&c=3')
382
+ end
383
+
384
+ it 'raises a controlled error when URL query params exceed the nested depth limit' do
385
+ original_param_depth_limit = Faraday::NestedParamsEncoder.param_depth_limit
386
+ Faraday::NestedParamsEncoder.param_depth_limit = 2
387
+
388
+ expect { conn.build_url('/nigiri?a[b][c]=1') }.to raise_error(
389
+ Faraday::Error,
390
+ 'exceeded nested parameter depth limit of 2'
391
+ )
392
+ ensure
393
+ Faraday::NestedParamsEncoder.param_depth_limit = original_param_depth_limit
394
+ end
395
+ end
396
+
397
+ describe '#build_request' do
398
+ let(:url) { 'https://ahttpbingo.org/sake.html' }
399
+ let(:request) { conn.build_request(:get) }
400
+
401
+ before do
402
+ conn.headers = { 'Authorization' => 'token abc123' }
403
+ request.headers.delete('Authorization')
404
+ end
405
+
406
+ it { expect(conn.headers.keys).to eq(['Authorization']) }
407
+ it { expect(conn.headers.include?('Authorization')).to be_truthy }
408
+ it { expect(request.headers.keys).to be_empty }
409
+ it { expect(request.headers.include?('Authorization')).to be_falsey }
410
+ end
411
+
412
+ describe '#to_env' do
413
+ subject { conn.build_request(:get).to_env(conn).url }
414
+
415
+ let(:url) { 'http://httpbingo.org/sake.html' }
416
+ let(:options) { { params: @params } }
417
+
418
+ it 'parses url params into query' do
419
+ @params = { 'a[b]' => '1 + 2' }
420
+ expect(subject.query).to eq('a%5Bb%5D=1+%2B+2')
421
+ end
422
+
423
+ it 'escapes per spec' do
424
+ @params = { 'a' => '1+2 foo~bar.-baz' }
425
+ expect(subject.query).to eq('a=1%2B2+foo~bar.-baz')
426
+ end
427
+
428
+ it 'bracketizes nested params in query' do
429
+ @params = { 'a' => { 'b' => 'c' } }
430
+ expect(subject.query).to eq('a%5Bb%5D=c')
431
+ end
432
+
433
+ it 'bracketizes repeated params in query' do
434
+ @params = { 'a' => [1, 2] }
435
+ expect(subject.query).to eq('a%5B%5D=1&a%5B%5D=2')
436
+ end
437
+
438
+ it 'without braketizing repeated params in query' do
439
+ @params = { 'a' => [1, 2] }
440
+ conn.options.params_encoder = Faraday::FlatParamsEncoder
441
+ expect(subject.query).to eq('a=1&a=2')
442
+ end
443
+ end
444
+
445
+ describe 'proxy support' do
446
+ it 'accepts string' do
447
+ with_env 'http_proxy' => 'http://env-proxy.com:80' do
448
+ conn.proxy = 'http://proxy.com'
449
+ expect(conn.proxy.host).to eq('proxy.com')
450
+ end
451
+ end
452
+
453
+ it 'accepts uri' do
454
+ with_env 'http_proxy' => 'http://env-proxy.com:80' do
455
+ conn.proxy = URI.parse('http://proxy.com')
456
+ expect(conn.proxy.host).to eq('proxy.com')
457
+ end
458
+ end
459
+
460
+ it 'accepts hash with string uri' do
461
+ with_env 'http_proxy' => 'http://env-proxy.com:80' do
462
+ conn.proxy = { uri: 'http://proxy.com', user: 'rick' }
463
+ expect(conn.proxy.host).to eq('proxy.com')
464
+ expect(conn.proxy.user).to eq('rick')
465
+ end
466
+ end
467
+
468
+ it 'accepts hash' do
469
+ with_env 'http_proxy' => 'http://env-proxy.com:80' do
470
+ conn.proxy = { uri: URI.parse('http://proxy.com'), user: 'rick' }
471
+ expect(conn.proxy.host).to eq('proxy.com')
472
+ expect(conn.proxy.user).to eq('rick')
473
+ end
474
+ end
475
+
476
+ it 'accepts http env' do
477
+ with_env 'http_proxy' => 'http://env-proxy.com:80' do
478
+ expect(conn.proxy.host).to eq('env-proxy.com')
479
+ end
480
+ end
481
+
482
+ it 'accepts http env with auth' do
483
+ with_env 'http_proxy' => 'http://a%40b:my%20pass@proxy.com:80' do
484
+ expect(conn.proxy.user).to eq('a@b')
485
+ expect(conn.proxy.password).to eq('my pass')
486
+ end
487
+ end
488
+
489
+ it 'accepts env without scheme' do
490
+ with_env 'http_proxy' => 'localhost:8888' do
491
+ uri = conn.proxy[:uri]
492
+ expect(uri.host).to eq('localhost')
493
+ expect(uri.port).to eq(8888)
494
+ end
495
+ end
496
+
497
+ it 'fetches no proxy from nil env' do
498
+ with_env 'http_proxy' => nil do
499
+ expect(conn.proxy).to be_nil
500
+ end
501
+ end
502
+
503
+ it 'fetches no proxy from blank env' do
504
+ with_env 'http_proxy' => '' do
505
+ expect(conn.proxy).to be_nil
506
+ end
507
+ end
508
+
509
+ it 'does not accept uppercase env' do
510
+ with_env 'HTTP_PROXY' => 'http://localhost:8888/' do
511
+ expect(conn.proxy).to be_nil
512
+ end
513
+ end
514
+
515
+ it 'allows when url in no proxy list' do
516
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
517
+ conn = Faraday::Connection.new('http://example.com')
518
+ expect(conn.proxy).to be_nil
519
+ end
520
+ end
521
+
522
+ it 'allows when url in no proxy list with url_prefix' do
523
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
524
+ conn = Faraday::Connection.new
525
+ conn.url_prefix = 'http://example.com'
526
+ expect(conn.proxy).to be_nil
527
+ end
528
+ end
529
+
530
+ it 'allows when prefixed url is not in no proxy list' do
531
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
532
+ conn = Faraday::Connection.new('http://prefixedexample.com')
533
+ expect(conn.proxy.host).to eq('proxy.com')
534
+ end
535
+ end
536
+
537
+ it 'allows when subdomain url is in no proxy list' do
538
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
539
+ conn = Faraday::Connection.new('http://subdomain.example.com')
540
+ expect(conn.proxy).to be_nil
541
+ end
542
+ end
543
+
544
+ it 'allows when url not in no proxy list' do
545
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example2.com' do
546
+ conn = Faraday::Connection.new('http://example.com')
547
+ expect(conn.proxy.host).to eq('proxy.com')
548
+ end
549
+ end
550
+
551
+ it 'allows when ip address is not in no proxy list but url is' do
552
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'localhost' do
553
+ conn = Faraday::Connection.new('http://127.0.0.1')
554
+ expect(conn.proxy).to be_nil
555
+ end
556
+ end
557
+
558
+ it 'allows when url is not in no proxy list but ip address is' do
559
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => '127.0.0.1' do
560
+ conn = Faraday::Connection.new('http://localhost')
561
+ expect(conn.proxy).to be_nil
562
+ end
563
+ end
564
+
565
+ it 'allows in multi element no proxy list' do
566
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example0.com,example.com,example1.com' do
567
+ expect(Faraday::Connection.new('http://example0.com').proxy).to be_nil
568
+ expect(Faraday::Connection.new('http://example.com').proxy).to be_nil
569
+ expect(Faraday::Connection.new('http://example1.com').proxy).to be_nil
570
+ expect(Faraday::Connection.new('http://example2.com').proxy.host).to eq('proxy.com')
571
+ end
572
+ end
573
+
574
+ it 'test proxy requires uri' do
575
+ expect { conn.proxy = { uri: :bad_uri, user: 'rick' } }.to raise_error(ArgumentError)
576
+ end
577
+
578
+ it 'uses env http_proxy' do
579
+ with_env 'http_proxy' => 'http://proxy.com' do
580
+ conn = Faraday.new
581
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
582
+ expect(conn.proxy_for_request('http://google.co.uk').host).to eq('proxy.com')
583
+ end
584
+ end
585
+
586
+ it 'uses processes no_proxy before http_proxy' do
587
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
588
+ conn = Faraday.new
589
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
590
+ expect(conn.proxy_for_request('http://google.co.uk')).to be_nil
591
+ end
592
+ end
593
+
594
+ it 'uses env https_proxy' do
595
+ with_env 'https_proxy' => 'https://proxy.com' do
596
+ conn = Faraday.new
597
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
598
+ expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy.com')
599
+ end
600
+ end
601
+
602
+ it 'uses processes no_proxy before https_proxy' do
603
+ with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
604
+ conn = Faraday.new
605
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
606
+ expect(conn.proxy_for_request('https://google.co.uk')).to be_nil
607
+ end
608
+ end
609
+
610
+ it 'gives priority to manually set proxy' do
611
+ with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
612
+ conn = Faraday.new
613
+ conn.proxy = 'http://proxy2.com'
614
+
615
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_truthy
616
+ expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy2.com')
617
+ end
618
+ end
619
+
620
+ it 'ignores env proxy if set that way' do
621
+ with_env_proxy_disabled do
622
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
623
+ expect(conn.proxy).to be_nil
624
+ end
625
+ end
626
+ end
627
+
628
+ context 'performing a request' do
629
+ let(:url) { 'http://example.com' }
630
+ let(:conn) do
631
+ Faraday.new do |f|
632
+ f.adapter :test do |stubs|
633
+ stubs.get(url) do
634
+ [200, {}, 'ok']
635
+ end
636
+ end
637
+ end
638
+ end
639
+
640
+ it 'dynamically checks proxy' do
641
+ with_env 'http_proxy' => 'http://proxy.com:80' do
642
+ expect(conn.proxy.uri.host).to eq('proxy.com')
643
+
644
+ conn.get(url) do |req|
645
+ expect(req.options.proxy.uri.host).to eq('proxy.com')
646
+ end
647
+ end
648
+
649
+ conn.get(url)
650
+ expect(conn.instance_variable_get(:@temp_proxy)).to be_nil
651
+ end
652
+
653
+ it 'dynamically check no proxy' do
654
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
655
+ expect(conn.proxy.uri.host).to eq('proxy.com')
656
+
657
+ conn.get('http://example.com') do |req|
658
+ expect(req.options.proxy).to be_nil
659
+ end
660
+ end
661
+ end
662
+ end
663
+ end
664
+
665
+ describe '#dup' do
666
+ subject { conn.dup }
667
+
668
+ let(:url) { 'http://httpbingo.org/foo' }
669
+ let(:options) do
670
+ {
671
+ ssl: { verify: :none },
672
+ headers: { 'content-type' => 'text/plain' },
673
+ params: { 'a' => '1' },
674
+ request: { timeout: 5 }
675
+ }
676
+ end
677
+
678
+ it { expect(subject.build_exclusive_url).to eq(conn.build_exclusive_url) }
679
+ it { expect(subject.headers['content-type']).to eq('text/plain') }
680
+ it { expect(subject.params['a']).to eq('1') }
681
+
682
+ context 'after manual changes' do
683
+ before do
684
+ subject.headers['content-length'] = 12
685
+ subject.params['b'] = '2'
686
+ subject.options[:open_timeout] = 10
687
+ end
688
+
689
+ it { expect(subject.builder.handlers.size).to eq(1) }
690
+ it { expect(conn.builder.handlers.size).to eq(1) }
691
+ it { expect(conn.headers.key?('content-length')).to be_falsey }
692
+ it { expect(conn.params.key?('b')).to be_falsey }
693
+ it { expect(subject.options[:timeout]).to eq(5) }
694
+ it { expect(conn.options[:open_timeout]).to be_nil }
695
+ end
696
+ end
697
+
698
+ describe '#respond_to?' do
699
+ it { expect(Faraday.respond_to?(:get)).to be_truthy }
700
+ it { expect(Faraday.respond_to?(:post)).to be_truthy }
701
+ end
702
+
703
+ describe 'default_connection_options' do
704
+ context 'assigning a default value' do
705
+ before do
706
+ Faraday.default_connection_options = nil
707
+ Faraday.default_connection_options.request.timeout = 10
708
+ end
709
+
710
+ it_behaves_like 'default connection options'
711
+ end
712
+
713
+ context 'assigning a hash' do
714
+ before { Faraday.default_connection_options = { request: { timeout: 10 } } }
715
+
716
+ it_behaves_like 'default connection options'
717
+ end
718
+
719
+ context 'preserving a user_agent assigned via default_conncetion_options' do
720
+ around do |example|
721
+ old = Faraday.default_connection_options
722
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
723
+ example.run
724
+ Faraday.default_connection_options = old
725
+ end
726
+
727
+ context 'when url is a Hash' do
728
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
729
+
730
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
731
+ end
732
+
733
+ context 'when url is a String' do
734
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
735
+
736
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
737
+ end
738
+ end
739
+ end
740
+
741
+ describe 'request params' do
742
+ context 'with simple url' do
743
+ let(:url) { 'http://example.com' }
744
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
745
+
746
+ before do
747
+ conn.adapter(:test, stubs)
748
+ stubs.get('http://example.com?a=a&p=3') do
749
+ [200, {}, 'ok']
750
+ end
751
+ end
752
+
753
+ after { stubs.verify_stubbed_calls }
754
+
755
+ it 'test_overrides_request_params' do
756
+ conn.get('?p=2&a=a', p: 3)
757
+ end
758
+
759
+ it 'test_overrides_request_params_block' do
760
+ conn.get('?p=1&a=a', p: 2) do |req|
761
+ req.params[:p] = 3
762
+ end
763
+ end
764
+
765
+ it 'test_overrides_request_params_block_url' do
766
+ conn.get(nil, p: 2) do |req|
767
+ req.url('?p=1&a=a', 'p' => 3)
768
+ end
769
+ end
770
+ end
771
+
772
+ context 'with url and extra params' do
773
+ let(:url) { 'http://example.com?a=1&b=2' }
774
+ let(:options) { { params: { c: 3 } } }
775
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
776
+
777
+ before do
778
+ conn.adapter(:test, stubs)
779
+ end
780
+
781
+ it 'merges connection and request params' do
782
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
783
+ stubs.get(expected) { [200, {}, 'ok'] }
784
+ conn.get('?page=1', limit: 5)
785
+ stubs.verify_stubbed_calls
786
+ end
787
+
788
+ it 'allows to override all params' do
789
+ expected = 'http://example.com?b=b'
790
+ stubs.get(expected) { [200, {}, 'ok'] }
791
+ conn.get('?p=1&a=a', p: 2) do |req|
792
+ expect(req.params[:a]).to eq('a')
793
+ expect(req.params['c']).to eq(3)
794
+ expect(req.params['p']).to eq(2)
795
+ req.params = { b: 'b' }
796
+ expect(req.params['b']).to eq('b')
797
+ end
798
+ stubs.verify_stubbed_calls
799
+ end
800
+
801
+ it 'allows to set params_encoder for single request' do
802
+ encoder = CustomEncoder.new
803
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
804
+ stubs.get(expected) { [200, {}, 'ok'] }
805
+
806
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
807
+ req.options.params_encoder = encoder
808
+ end
809
+ stubs.verify_stubbed_calls
810
+ end
811
+ end
812
+
813
+ context 'with default params encoder' do
814
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
815
+
816
+ before do
817
+ conn.adapter(:test, stubs)
818
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
819
+ [200, {}, 'ok']
820
+ end
821
+ end
822
+
823
+ after { stubs.verify_stubbed_calls }
824
+
825
+ it 'supports array params in url' do
826
+ conn.get('http://example.com?color[]=blue&color[]=red')
827
+ end
828
+
829
+ it 'supports array params in params' do
830
+ conn.get('http://example.com', color: %w[blue red])
831
+ end
832
+ end
833
+
834
+ context 'with flat params encoder' do
835
+ let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
836
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
837
+
838
+ before do
839
+ conn.adapter(:test, stubs)
840
+ stubs.get('http://example.com?color=blue&color=red') do
841
+ [200, {}, 'ok']
842
+ end
843
+ end
844
+
845
+ after { stubs.verify_stubbed_calls }
846
+
847
+ it 'supports array params in params' do
848
+ conn.get('http://example.com', color: %w[blue red])
849
+ end
850
+
851
+ context 'with array param in url' do
852
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
853
+
854
+ it do
855
+ conn.get('/')
856
+ end
857
+ end
858
+ end
859
+ end
860
+ end