avdi-faraday 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/Gemfile +27 -0
  2. data/LICENSE.md +20 -0
  3. data/README.md +250 -0
  4. data/Rakefile +87 -0
  5. data/config.ru +6 -0
  6. data/faraday.gemspec +86 -0
  7. data/lib/faraday.rb +276 -0
  8. data/lib/faraday/adapter.rb +71 -0
  9. data/lib/faraday/adapter/em_http.rb +217 -0
  10. data/lib/faraday/adapter/em_synchrony.rb +89 -0
  11. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
  12. data/lib/faraday/adapter/excon.rb +59 -0
  13. data/lib/faraday/adapter/httpclient.rb +92 -0
  14. data/lib/faraday/adapter/net_http.rb +116 -0
  15. data/lib/faraday/adapter/net_http_persistent.rb +37 -0
  16. data/lib/faraday/adapter/patron.rb +65 -0
  17. data/lib/faraday/adapter/rack.rb +57 -0
  18. data/lib/faraday/adapter/test.rb +162 -0
  19. data/lib/faraday/adapter/typhoeus.rb +107 -0
  20. data/lib/faraday/builder.rb +184 -0
  21. data/lib/faraday/connection.rb +468 -0
  22. data/lib/faraday/error.rb +40 -0
  23. data/lib/faraday/middleware.rb +41 -0
  24. data/lib/faraday/request.rb +101 -0
  25. data/lib/faraday/request/authorization.rb +40 -0
  26. data/lib/faraday/request/basic_authentication.rb +13 -0
  27. data/lib/faraday/request/multipart.rb +62 -0
  28. data/lib/faraday/request/retry.rb +67 -0
  29. data/lib/faraday/request/token_authentication.rb +15 -0
  30. data/lib/faraday/request/url_encoded.rb +35 -0
  31. data/lib/faraday/response.rb +99 -0
  32. data/lib/faraday/response/logger.rb +34 -0
  33. data/lib/faraday/response/raise_error.rb +16 -0
  34. data/lib/faraday/upload_io.rb +23 -0
  35. data/lib/faraday/utils.rb +274 -0
  36. data/script/test +91 -0
  37. data/test/adapters/default_test.rb +14 -0
  38. data/test/adapters/em_http_test.rb +19 -0
  39. data/test/adapters/em_synchrony_test.rb +20 -0
  40. data/test/adapters/excon_test.rb +15 -0
  41. data/test/adapters/httpclient_test.rb +16 -0
  42. data/test/adapters/integration.rb +193 -0
  43. data/test/adapters/logger_test.rb +37 -0
  44. data/test/adapters/net_http_persistent_test.rb +11 -0
  45. data/test/adapters/net_http_test.rb +49 -0
  46. data/test/adapters/patron_test.rb +17 -0
  47. data/test/adapters/rack_test.rb +26 -0
  48. data/test/adapters/test_middleware_test.rb +70 -0
  49. data/test/adapters/typhoeus_test.rb +20 -0
  50. data/test/authentication_middleware_test.rb +65 -0
  51. data/test/connection_test.rb +375 -0
  52. data/test/env_test.rb +183 -0
  53. data/test/helper.rb +75 -0
  54. data/test/live_server.rb +57 -0
  55. data/test/middleware/retry_test.rb +62 -0
  56. data/test/middleware_stack_test.rb +203 -0
  57. data/test/middleware_test.rb +12 -0
  58. data/test/request_middleware_test.rb +108 -0
  59. data/test/response_middleware_test.rb +74 -0
  60. metadata +182 -0
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../integration', __FILE__)
2
+
3
+ module Adapters
4
+ class TyphoeusTest < Faraday::TestCase
5
+
6
+ def adapter() :typhoeus end
7
+
8
+ Integration.apply(self, :Parallel) do
9
+ # https://github.com/dbalatero/typhoeus/issues/75
10
+ undef :test_GET_with_body
11
+
12
+ def test_binds_local_socket
13
+ host = '1.2.3.4'
14
+ conn = create_connection :request => { :bind => { :host => host } }
15
+ assert_equal host, conn.options[:bind][:host]
16
+ end
17
+ end
18
+ end unless defined? RUBY_ENGINE and 'jruby' == RUBY_ENGINE
19
+ end
20
+
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class AuthenticationMiddlewareTest < Faraday::TestCase
4
+ def conn
5
+ Faraday::Connection.new('http://example.net/') do |builder|
6
+ yield builder
7
+ builder.adapter :test do |stub|
8
+ stub.get('/auth-echo') do |env|
9
+ [200, {}, env[:request_headers]['Authorization']]
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def test_basic_middleware_adds_basic_header
16
+ response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.get('/auth-echo')
17
+ assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', response.body
18
+ end
19
+
20
+ def test_basic_middleware_adds_basic_header_correctly_with_long_values
21
+ response = conn { |b| b.request :basic_auth, 'A' * 255, '' }.get('/auth-echo')
22
+ assert_equal "Basic #{'QUFB' * 85}Og==", response.body
23
+ end
24
+
25
+ def test_basic_middleware_does_not_interfere_with_existing_authorization
26
+ response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.
27
+ get('/auth-echo', nil, :authorization => 'Token token="bar"')
28
+ assert_equal 'Token token="bar"', response.body
29
+ end
30
+
31
+ def test_token_middleware_adds_token_header
32
+ response = conn { |b| b.request :token_auth, 'quux' }.get('/auth-echo')
33
+ assert_equal 'Token token="quux"', response.body
34
+ end
35
+
36
+ def test_token_middleware_includes_other_values_if_provided
37
+ response = conn { |b|
38
+ b.request :token_auth, 'baz', :foo => 42
39
+ }.get('/auth-echo')
40
+ assert_match /^Token /, response.body
41
+ assert_match /token="baz"/, response.body
42
+ assert_match /foo="42"/, response.body
43
+ end
44
+
45
+ def test_token_middleware_does_not_interfere_with_existing_authorization
46
+ response = conn { |b| b.request :token_auth, 'quux' }.
47
+ get('/auth-echo', nil, :authorization => 'Token token="bar"')
48
+ assert_equal 'Token token="bar"', response.body
49
+ end
50
+
51
+ def test_authorization_middleware_with_string
52
+ response = conn { |b|
53
+ b.request :authorization, 'custom', 'abc def'
54
+ }.get('/auth-echo')
55
+ assert_match /^custom abc def$/, response.body
56
+ end
57
+
58
+ def test_authorization_middleware_with_hash
59
+ response = conn { |b|
60
+ b.request :authorization, 'baz', :foo => 42
61
+ }.get('/auth-echo')
62
+ assert_match /^baz /, response.body
63
+ assert_match /foo="42"/, response.body
64
+ end
65
+ end
@@ -0,0 +1,375 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ require 'uri'
3
+
4
+ class TestConnection < Faraday::TestCase
5
+
6
+ def with_proxy_env(proxy)
7
+ old_proxy = ENV['http_proxy']
8
+ ENV['http_proxy'] = proxy
9
+ begin
10
+ yield
11
+ ensure
12
+ ENV['http_proxy'] = old_proxy
13
+ end
14
+ end
15
+
16
+ def test_initialize_parses_host_out_of_given_url
17
+ conn = Faraday::Connection.new "http://sushi.com"
18
+ assert_equal 'sushi.com', conn.host
19
+ end
20
+
21
+ def test_initialize_inherits_default_port_out_of_given_url
22
+ conn = Faraday::Connection.new "http://sushi.com"
23
+ assert_equal 80, conn.port
24
+ end
25
+
26
+ def test_initialize_parses_scheme_out_of_given_url
27
+ conn = Faraday::Connection.new "http://sushi.com"
28
+ assert_equal 'http', conn.scheme
29
+ end
30
+
31
+ def test_initialize_parses_port_out_of_given_url
32
+ conn = Faraday::Connection.new "http://sushi.com:815"
33
+ assert_equal 815, conn.port
34
+ end
35
+
36
+ def test_initialize_parses_nil_path_prefix_out_of_given_url
37
+ conn = Faraday::Connection.new "http://sushi.com"
38
+ assert_equal '/', conn.path_prefix
39
+ end
40
+
41
+ def test_initialize_parses_path_prefix_out_of_given_url
42
+ conn = Faraday::Connection.new "http://sushi.com/fish"
43
+ assert_equal '/fish', conn.path_prefix
44
+ end
45
+
46
+ def test_initialize_parses_path_prefix_out_of_given_url_option
47
+ conn = Faraday::Connection.new :url => "http://sushi.com/fish"
48
+ assert_equal '/fish', conn.path_prefix
49
+ end
50
+
51
+ def test_initialize_stores_default_params_from_options
52
+ conn = Faraday::Connection.new :params => {:a => 1}
53
+ assert_equal({'a' => 1}, conn.params)
54
+ end
55
+
56
+ def test_initialize_stores_default_params_from_uri
57
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
58
+ assert_equal({'a' => '1'}, conn.params)
59
+ end
60
+
61
+ def test_initialize_stores_default_params_from_uri_and_options
62
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}
63
+ assert_equal({'a' => 3, 'b' => '2'}, conn.params)
64
+ end
65
+
66
+ def test_initialize_stores_default_headers_from_options
67
+ conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
68
+ assert_equal 'Faraday', conn.headers['User-agent']
69
+ end
70
+
71
+ def test_basic_auth_sets_header
72
+ conn = Faraday::Connection.new
73
+ assert_nil conn.headers['Authorization']
74
+
75
+ conn.basic_auth 'Aladdin', 'open sesame'
76
+ assert auth = conn.headers['Authorization']
77
+ assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
78
+ end
79
+
80
+ def test_auto_parses_basic_auth_from_url_and_unescapes
81
+ conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
82
+ assert auth = conn.headers['Authorization']
83
+ assert_equal Faraday::Request::BasicAuthentication.header("foo@bar.com", "pass word"), auth
84
+ end
85
+
86
+ def test_token_auth_sets_header
87
+ conn = Faraday::Connection.new
88
+ assert_nil conn.headers['Authorization']
89
+
90
+ conn.token_auth 'abcdef', :nonce => 'abc'
91
+ assert auth = conn.headers['Authorization']
92
+ assert_match /^Token /, auth
93
+ assert_match /token="abcdef"/, auth
94
+ assert_match /nonce="abc"/, auth
95
+ end
96
+
97
+ def test_build_url_uses_connection_host_as_default_uri_host
98
+ conn = Faraday::Connection.new
99
+ conn.host = 'sushi.com'
100
+ uri = conn.build_url("/sake.html")
101
+ assert_equal 'sushi.com', uri.host
102
+ end
103
+
104
+ def test_build_url_overrides_connection_port_for_absolute_urls
105
+ conn = Faraday::Connection.new
106
+ conn.port = 23
107
+ uri = conn.build_url("http://sushi.com")
108
+ assert_equal 80, uri.port
109
+ end
110
+
111
+ def test_build_url_uses_connection_scheme_as_default_uri_scheme
112
+ conn = Faraday::Connection.new 'http://sushi.com'
113
+ uri = conn.build_url("/sake.html")
114
+ assert_equal 'http', uri.scheme
115
+ end
116
+
117
+ def test_build_url_uses_connection_path_prefix_to_customize_path
118
+ conn = Faraday::Connection.new
119
+ conn.path_prefix = '/fish'
120
+ uri = conn.build_url("sake.html")
121
+ assert_equal '/fish/sake.html', uri.path
122
+ end
123
+
124
+ def test_build_url_uses_root_connection_path_prefix_to_customize_path
125
+ conn = Faraday::Connection.new
126
+ conn.path_prefix = '/'
127
+ uri = conn.build_url("sake.html")
128
+ assert_equal '/sake.html', uri.path
129
+ end
130
+
131
+ def test_build_url_forces_connection_path_prefix_to_be_absolute
132
+ conn = Faraday::Connection.new
133
+ conn.path_prefix = 'fish'
134
+ uri = conn.build_url("sake.html")
135
+ assert_equal '/fish/sake.html', uri.path
136
+ end
137
+
138
+ def test_build_url_ignores_connection_path_prefix_trailing_slash
139
+ conn = Faraday::Connection.new
140
+ conn.path_prefix = '/fish/'
141
+ uri = conn.build_url("sake.html")
142
+ assert_equal '/fish/sake.html', uri.path
143
+ end
144
+
145
+ def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
146
+ conn = Faraday::Connection.new
147
+ conn.path_prefix = '/fish'
148
+ uri = conn.build_url("/sake.html")
149
+ assert_equal '/sake.html', uri.path
150
+ end
151
+
152
+ def test_build_url_parses_url_params_into_path
153
+ conn = Faraday::Connection.new
154
+ uri = conn.build_url("http://sushi.com/sake.html")
155
+ assert_equal '/sake.html', uri.path
156
+ end
157
+
158
+ def test_build_url_doesnt_add_ending_slash_given_nil_url
159
+ conn = Faraday::Connection.new
160
+ conn.url_prefix = "http://sushi.com/nigiri"
161
+ uri = conn.build_url(nil)
162
+ assert_equal "/nigiri", uri.path
163
+ end
164
+
165
+ def test_build_url_doesnt_add_ending_slash_given_empty_url
166
+ conn = Faraday::Connection.new
167
+ conn.url_prefix = "http://sushi.com/nigiri"
168
+ uri = conn.build_url('')
169
+ assert_equal "/nigiri", uri.path
170
+ end
171
+
172
+ def test_build_url_parses_url_params_into_query
173
+ conn = Faraday::Connection.new
174
+ uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
175
+ assert_equal "a%5Bb%5D=1+%2B+2", uri.query
176
+ end
177
+
178
+ def test_build_url_escapes_per_spec
179
+ conn = Faraday::Connection.new
180
+ uri = conn.build_url('http:/', 'a' => '1+2 foo~bar.-baz')
181
+ assert_equal "a=1%2B2+foo~bar.-baz", uri.query
182
+ end
183
+
184
+ def test_build_url_bracketizes_nested_params_in_query
185
+ conn = Faraday::Connection.new
186
+ uri = conn.build_url("http://sushi.com/sake.html", 'a' => {'b' => 'c'})
187
+ assert_equal "a%5Bb%5D=c", uri.query
188
+ end
189
+
190
+ def test_build_url_parses_url
191
+ conn = Faraday::Connection.new
192
+ uri = conn.build_url("http://sushi.com/sake.html")
193
+ assert_equal "http", uri.scheme
194
+ assert_equal "sushi.com", uri.host
195
+ assert_equal '/sake.html', uri.path
196
+ end
197
+
198
+ def test_build_url_parses_url_and_changes_scheme
199
+ conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
200
+ conn.scheme = 'https'
201
+ uri = conn.build_url("sake.html")
202
+ assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
203
+ end
204
+
205
+ def test_build_url_handles_uri_instances
206
+ conn = Faraday::Connection.new
207
+ uri = conn.build_url(URI('/sake.html'))
208
+ assert_equal '/sake.html', uri.path
209
+ end
210
+
211
+ def test_proxy_accepts_string
212
+ with_proxy_env "http://duncan.proxy.com:80" do
213
+ conn = Faraday::Connection.new
214
+ conn.proxy 'http://proxy.com'
215
+ assert_equal 'proxy.com', conn.proxy[:uri].host
216
+ assert_equal [:uri], conn.proxy.keys
217
+ end
218
+ end
219
+
220
+ def test_proxy_accepts_uri
221
+ with_proxy_env "http://duncan.proxy.com:80" do
222
+ conn = Faraday::Connection.new
223
+ conn.proxy URI.parse('http://proxy.com')
224
+ assert_equal 'proxy.com', conn.proxy[:uri].host
225
+ assert_equal [:uri], conn.proxy.keys
226
+ end
227
+ end
228
+
229
+ def test_proxy_accepts_hash_with_string_uri
230
+ with_proxy_env "http://duncan.proxy.com:80" do
231
+ conn = Faraday::Connection.new
232
+ conn.proxy :uri => 'http://proxy.com', :user => 'rick'
233
+ assert_equal 'proxy.com', conn.proxy[:uri].host
234
+ assert_equal 'rick', conn.proxy[:user]
235
+ end
236
+ end
237
+
238
+ def test_proxy_accepts_hash
239
+ with_proxy_env "http://duncan.proxy.com:80" do
240
+ conn = Faraday::Connection.new
241
+ conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
242
+ assert_equal 'proxy.com', conn.proxy[:uri].host
243
+ assert_equal 'rick', conn.proxy[:user]
244
+ end
245
+ end
246
+
247
+ def test_proxy_accepts_http_env
248
+ with_proxy_env "http://duncan.proxy.com:80" do
249
+ conn = Faraday::Connection.new
250
+ assert_equal 'duncan.proxy.com', conn.proxy[:uri].host
251
+ end
252
+ end
253
+
254
+ def test_proxy_requires_uri
255
+ conn = Faraday::Connection.new
256
+ assert_raises ArgumentError do
257
+ conn.proxy :uri => :bad_uri, :user => 'rick'
258
+ end
259
+ end
260
+
261
+ def test_dups_connection_object
262
+ conn = Faraday::Connection.new 'http://sushi.com/foo',
263
+ :ssl => { :verify => :none },
264
+ :headers => {'content-type' => 'text/plain'},
265
+ :params => {'a'=>'1'}
266
+
267
+ other = conn.dup
268
+
269
+ assert_equal conn.build_url(''), other.build_url('')
270
+ assert_equal 'text/plain', other.headers['content-type']
271
+ assert_equal '1', other.params['a']
272
+
273
+ other.basic_auth('', '')
274
+ other.headers['content-length'] = 12
275
+ other.params['b'] = '2'
276
+
277
+ assert_equal 2, other.builder.handlers.size
278
+ assert_equal 2, conn.builder.handlers.size
279
+ assert !conn.headers.key?('content-length')
280
+ assert !conn.params.key?('b')
281
+ end
282
+
283
+ def test_init_with_block
284
+ conn = Faraday::Connection.new { }
285
+ assert_equal 0, conn.builder.handlers.size
286
+ end
287
+
288
+ def test_init_with_block_yields_connection
289
+ conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
290
+ faraday.adapter :net_http
291
+ faraday.url_prefix = 'http://sushi.com/omnom'
292
+ assert_equal '1', faraday.params['a']
293
+ }
294
+ assert_equal 1, conn.builder.handlers.size
295
+ assert_equal '/omnom', conn.path_prefix
296
+ end
297
+ end
298
+
299
+ class TestRequestParams < Faraday::TestCase
300
+ def create_connection(*args)
301
+ @conn = Faraday::Connection.new(*args) do |conn|
302
+ yield conn if block_given?
303
+ class << conn
304
+ undef app
305
+ def app() lambda { |env| env } end
306
+ end
307
+ end
308
+ end
309
+
310
+ def get(*args)
311
+ env = @conn.get(*args) do |req|
312
+ yield req if block_given?
313
+ end
314
+ env[:url].query
315
+ end
316
+
317
+ def assert_query_equal(expected, query)
318
+ assert_equal expected, query.split('&').sort
319
+ end
320
+
321
+ def test_merges_connection_and_request_params
322
+ create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
323
+ query = get '?page=1', :limit => 5
324
+ assert_query_equal %w[format=json limit=5 page=1 token=abc], query
325
+ end
326
+
327
+ def test_overrides_connection_params
328
+ create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
329
+ conn.params[:b] = 'B'
330
+ assert_equal 'c', conn.params[:c]
331
+ end
332
+ assert_query_equal %w[a=A b=B c=c], get
333
+ end
334
+
335
+ def test_all_overrides_connection_params
336
+ create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
337
+ conn.params = {'b' => 'b'}
338
+ end
339
+ assert_query_equal %w[b=b], get
340
+ end
341
+
342
+ def test_overrides_request_params
343
+ create_connection
344
+ query = get '?p=1&a=a', :p => 2
345
+ assert_query_equal %w[a=a p=2], query
346
+ end
347
+
348
+ def test_overrides_request_params_block
349
+ create_connection
350
+ query = get '?p=1&a=a', :p => 2 do |req|
351
+ req.params[:p] = 3
352
+ end
353
+ assert_query_equal %w[a=a p=3], query
354
+ end
355
+
356
+ def test_overrides_request_params_block_url
357
+ create_connection
358
+ query = get nil, :p => 2 do |req|
359
+ req.url '?p=1&a=a', 'p' => 3
360
+ end
361
+ assert_query_equal %w[a=a p=3], query
362
+ end
363
+
364
+ def test_overrides_all_request_params
365
+ create_connection :params => {:c => 'c'}
366
+ query = get '?p=1&a=a', :p => 2 do |req|
367
+ assert_equal 'a', req.params[:a]
368
+ assert_equal 'c', req.params['c']
369
+ assert_equal 2, req.params['p']
370
+ req.params = {:b => 'b'}
371
+ assert_equal 'b', req.params['b']
372
+ end
373
+ assert_query_equal %w[b=b], query
374
+ end
375
+ end