faraday 0.6.0 → 0.9.0

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 (83) hide show
  1. checksums.yaml +7 -0
  2. data/.document +6 -0
  3. data/CHANGELOG.md +15 -0
  4. data/CONTRIBUTING.md +36 -0
  5. data/Gemfile +25 -15
  6. data/{LICENSE → LICENSE.md} +1 -1
  7. data/README.md +187 -137
  8. data/Rakefile +38 -101
  9. data/faraday.gemspec +26 -81
  10. data/lib/faraday/adapter/em_http.rb +237 -0
  11. data/lib/faraday/adapter/em_http_ssl_patch.rb +56 -0
  12. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
  13. data/lib/faraday/adapter/em_synchrony.rb +65 -38
  14. data/lib/faraday/adapter/excon.rb +60 -9
  15. data/lib/faraday/adapter/httpclient.rb +106 -0
  16. data/lib/faraday/adapter/net_http.rb +89 -38
  17. data/lib/faraday/adapter/net_http_persistent.rb +47 -0
  18. data/lib/faraday/adapter/patron.rb +46 -9
  19. data/lib/faraday/adapter/rack.rb +58 -0
  20. data/lib/faraday/adapter/test.rb +68 -28
  21. data/lib/faraday/adapter/typhoeus.rb +94 -16
  22. data/lib/faraday/adapter.rb +33 -25
  23. data/lib/faraday/autoload.rb +85 -0
  24. data/lib/faraday/connection.rb +342 -134
  25. data/lib/faraday/error.rb +44 -29
  26. data/lib/faraday/middleware.rb +18 -10
  27. data/lib/faraday/options.rb +350 -0
  28. data/lib/faraday/parameters.rb +193 -0
  29. data/lib/faraday/rack_builder.rb +212 -0
  30. data/lib/faraday/request/authorization.rb +42 -0
  31. data/lib/faraday/request/basic_authentication.rb +13 -0
  32. data/lib/faraday/request/instrumentation.rb +36 -0
  33. data/lib/faraday/request/multipart.rb +15 -15
  34. data/lib/faraday/request/retry.rb +118 -0
  35. data/lib/faraday/request/token_authentication.rb +15 -0
  36. data/lib/faraday/request/url_encoded.rb +8 -9
  37. data/lib/faraday/request.rb +46 -45
  38. data/lib/faraday/response/logger.rb +4 -4
  39. data/lib/faraday/response/raise_error.rb +8 -3
  40. data/lib/faraday/response.rb +20 -25
  41. data/lib/faraday/upload_io.rb +51 -7
  42. data/lib/faraday/utils.rb +240 -44
  43. data/lib/faraday.rb +218 -38
  44. data/script/console +7 -0
  45. data/script/generate_certs +42 -0
  46. data/script/package +7 -0
  47. data/script/proxy-server +42 -0
  48. data/script/release +17 -0
  49. data/script/server +36 -0
  50. data/script/test +172 -0
  51. data/test/adapters/default_test.rb +14 -0
  52. data/test/adapters/em_http_test.rb +20 -0
  53. data/test/adapters/em_synchrony_test.rb +20 -0
  54. data/test/adapters/excon_test.rb +20 -0
  55. data/test/adapters/httpclient_test.rb +21 -0
  56. data/test/adapters/integration.rb +254 -0
  57. data/test/adapters/logger_test.rb +3 -3
  58. data/test/adapters/net_http_persistent_test.rb +20 -0
  59. data/test/adapters/net_http_test.rb +6 -25
  60. data/test/adapters/patron_test.rb +20 -0
  61. data/test/adapters/rack_test.rb +31 -0
  62. data/test/adapters/test_middleware_test.rb +74 -3
  63. data/test/adapters/typhoeus_test.rb +28 -0
  64. data/test/authentication_middleware_test.rb +65 -0
  65. data/test/composite_read_io_test.rb +111 -0
  66. data/test/connection_test.rb +381 -104
  67. data/test/env_test.rb +116 -40
  68. data/test/helper.rb +61 -25
  69. data/test/live_server.rb +55 -29
  70. data/test/middleware/instrumentation_test.rb +88 -0
  71. data/test/middleware/retry_test.rb +109 -0
  72. data/test/middleware_stack_test.rb +87 -5
  73. data/test/multibyte.txt +1 -0
  74. data/test/options_test.rb +252 -0
  75. data/test/request_middleware_test.rb +78 -21
  76. data/test/response_middleware_test.rb +32 -7
  77. data/test/strawberry.rb +2 -0
  78. data/test/utils_test.rb +58 -0
  79. metadata +116 -117
  80. data/lib/faraday/adapter/action_dispatch.rb +0 -30
  81. data/lib/faraday/builder.rb +0 -137
  82. data/lib/faraday/request/json.rb +0 -31
  83. data/test/adapters/live_test.rb +0 -186
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../integration', __FILE__)
2
+
3
+ module Adapters
4
+ class ExconTest < Faraday::TestCase
5
+
6
+ def adapter() :excon end
7
+
8
+ Integration.apply(self, :NonParallel) do
9
+ # https://github.com/geemus/excon/issues/126 ?
10
+ undef :test_timeout if ssl_mode?
11
+
12
+ # Excon lets OpenSSL::SSL::SSLError be raised without any way to
13
+ # distinguish whether it happened because of a 407 proxy response
14
+ undef :test_proxy_auth_fail if ssl_mode?
15
+
16
+ # https://github.com/geemus/excon/issues/358
17
+ undef :test_connection_error if RUBY_VERSION >= '2.1.0'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../integration', __FILE__)
2
+
3
+ module Adapters
4
+ class HttpclientTest < Faraday::TestCase
5
+
6
+ def adapter() :httpclient end
7
+
8
+ Integration.apply(self, :NonParallel) do
9
+ def setup
10
+ require 'httpclient' unless defined?(HTTPClient)
11
+ HTTPClient::NO_PROXY_HOSTS.delete('localhost')
12
+ end
13
+
14
+ def test_binds_local_socket
15
+ host = '1.2.3.4'
16
+ conn = create_connection :request => { :bind => { :host => host } }
17
+ assert_equal host, conn.options[:bind][:host]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,254 @@
1
+ require 'forwardable'
2
+ require File.expand_path("../../helper", __FILE__)
3
+ Faraday.require_lib 'autoload'
4
+
5
+ module Adapters
6
+ # Adapter integration tests. To use, implement two methods:
7
+ #
8
+ # `#adapter` required. returns a symbol for the adapter middleware name
9
+ # `#adapter_options` optional. extra arguments for building an adapter
10
+ module Integration
11
+ def self.apply(base, *extra_features)
12
+ if base.live_server?
13
+ features = [:Common]
14
+ features.concat extra_features
15
+ features << :SSL if base.ssl_mode?
16
+ features.each {|name| base.send(:include, self.const_get(name)) }
17
+ yield if block_given?
18
+ elsif !defined? @warned
19
+ warn "Warning: Not running integration tests against a live server."
20
+ warn "Start the server `ruby test/live_server.rb` and set the LIVE=1 env variable."
21
+ @warned = true
22
+ end
23
+ end
24
+
25
+ module Parallel
26
+ def test_in_parallel
27
+ resp1, resp2 = nil, nil
28
+
29
+ connection = create_connection
30
+ connection.in_parallel do
31
+ resp1 = connection.get('echo?a=1')
32
+ resp2 = connection.get('echo?b=2')
33
+ assert connection.in_parallel?
34
+ assert_nil resp1.body
35
+ assert_nil resp2.body
36
+ end
37
+ assert !connection.in_parallel?
38
+ assert_equal 'get ?{"a"=>"1"}', resp1.body
39
+ assert_equal 'get ?{"b"=>"2"}', resp2.body
40
+ end
41
+ end
42
+
43
+ module NonParallel
44
+ def test_no_parallel_support
45
+ connection = create_connection
46
+ response = nil
47
+
48
+ err = capture_warnings do
49
+ connection.in_parallel do
50
+ response = connection.get('echo').body
51
+ end
52
+ end
53
+ assert response
54
+ assert_match "no parallel-capable adapter on Faraday stack", err
55
+ assert_match __FILE__, err
56
+ end
57
+ end
58
+
59
+ module Compression
60
+ def test_GET_handles_compression
61
+ res = get('echo_header', :name => 'accept-encoding')
62
+ assert_match(/gzip;.+\bdeflate\b/, res.body)
63
+ end
64
+ end
65
+
66
+ module SSL
67
+ def test_GET_ssl_fails_with_bad_cert
68
+ ca_file = 'tmp/faraday-different-ca-cert.crt'
69
+ conn = create_connection(:ssl => {:ca_file => ca_file})
70
+ err = assert_raises Faraday::SSLError do
71
+ conn.get('/ssl')
72
+ end
73
+ assert_includes err.message, "certificate"
74
+ end
75
+ end
76
+
77
+ module Common
78
+ extend Forwardable
79
+ def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
80
+
81
+ def test_GET_retrieves_the_response_body
82
+ assert_equal 'get', get('echo').body
83
+ end
84
+
85
+ def test_GET_send_url_encoded_params
86
+ assert_equal %(get ?{"name"=>"zack"}), get('echo', :name => 'zack').body
87
+ end
88
+
89
+ def test_GET_retrieves_the_response_headers
90
+ response = get('echo')
91
+ assert_match(/text\/plain/, response.headers['Content-Type'], 'original case fail')
92
+ assert_match(/text\/plain/, response.headers['content-type'], 'lowercase fail')
93
+ end
94
+
95
+ def test_GET_handles_headers_with_multiple_values
96
+ assert_equal 'one, two', get('multi').headers['set-cookie']
97
+ end
98
+
99
+ def test_GET_with_body
100
+ response = get('echo') do |req|
101
+ req.body = {'bodyrock' => true}
102
+ end
103
+ assert_equal %(get {"bodyrock"=>"true"}), response.body
104
+ end
105
+
106
+ def test_GET_sends_user_agent
107
+ response = get('echo_header', {:name => 'user-agent'}, :user_agent => 'Agent Faraday')
108
+ assert_equal 'Agent Faraday', response.body
109
+ end
110
+
111
+ def test_GET_ssl
112
+ expected = self.class.ssl_mode?.to_s
113
+ assert_equal expected, get('ssl').body
114
+ end
115
+
116
+ def test_POST_send_url_encoded_params
117
+ assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
118
+ end
119
+
120
+ def test_POST_send_url_encoded_nested_params
121
+ resp = post('echo', 'name' => {'first' => 'zack'})
122
+ assert_equal %(post {"name"=>{"first"=>"zack"}}), resp.body
123
+ end
124
+
125
+ def test_POST_retrieves_the_response_headers
126
+ assert_match(/text\/plain/, post('echo').headers['content-type'])
127
+ end
128
+
129
+ def test_POST_sends_files
130
+ resp = post('file') do |req|
131
+ req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
132
+ end
133
+ assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
134
+ end
135
+
136
+ def test_PUT_send_url_encoded_params
137
+ assert_equal %(put {"name"=>"zack"}), put('echo', :name => 'zack').body
138
+ end
139
+
140
+ def test_PUT_send_url_encoded_nested_params
141
+ resp = put('echo', 'name' => {'first' => 'zack'})
142
+ assert_equal %(put {"name"=>{"first"=>"zack"}}), resp.body
143
+ end
144
+
145
+ def test_PUT_retrieves_the_response_headers
146
+ assert_match(/text\/plain/, put('echo').headers['content-type'])
147
+ end
148
+
149
+ def test_PATCH_send_url_encoded_params
150
+ assert_equal %(patch {"name"=>"zack"}), patch('echo', :name => 'zack').body
151
+ end
152
+
153
+ def test_OPTIONS
154
+ resp = run_request(:options, 'echo', nil, {})
155
+ assert_equal 'options', resp.body
156
+ end
157
+
158
+ def test_HEAD_retrieves_no_response_body
159
+ assert_equal '', head('echo').body
160
+ end
161
+
162
+ def test_HEAD_retrieves_the_response_headers
163
+ assert_match(/text\/plain/, head('echo').headers['content-type'])
164
+ end
165
+
166
+ def test_DELETE_retrieves_the_response_headers
167
+ assert_match(/text\/plain/, delete('echo').headers['content-type'])
168
+ end
169
+
170
+ def test_DELETE_retrieves_the_body
171
+ assert_equal %(delete), delete('echo').body
172
+ end
173
+
174
+ def test_timeout
175
+ conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
176
+ assert_raises Faraday::Error::TimeoutError do
177
+ conn.get '/slow'
178
+ end
179
+ end
180
+
181
+ def test_connection_error
182
+ assert_raises Faraday::Error::ConnectionFailed do
183
+ get 'http://localhost:4'
184
+ end
185
+ end
186
+
187
+ def test_proxy
188
+ proxy_uri = URI(ENV['LIVE_PROXY'])
189
+ conn = create_connection(:proxy => proxy_uri)
190
+
191
+ res = conn.get '/echo'
192
+ assert_equal 'get', res.body
193
+
194
+ unless self.class.ssl_mode?
195
+ # proxy can't append "Via" header for HTTPS responses
196
+ assert_match(/:#{proxy_uri.port}$/, res['via'])
197
+ end
198
+ end
199
+
200
+ def test_proxy_auth_fail
201
+ proxy_uri = URI(ENV['LIVE_PROXY'])
202
+ proxy_uri.password = 'WRONG'
203
+ conn = create_connection(:proxy => proxy_uri)
204
+
205
+ err = assert_raises Faraday::Error::ConnectionFailed do
206
+ conn.get '/echo'
207
+ end
208
+
209
+ unless self.class.ssl_mode? && self.class.jruby?
210
+ # JRuby raises "End of file reached" which cannot be distinguished from a 407
211
+ assert_equal %{407 "Proxy Authentication Required "}, err.message
212
+ end
213
+ end
214
+
215
+ def test_empty_body_response_represented_as_blank_string
216
+ response = get('204')
217
+ assert_equal '', response.body
218
+ end
219
+
220
+ def adapter
221
+ raise NotImplementedError.new("Need to override #adapter")
222
+ end
223
+
224
+ # extra options to pass when building the adapter
225
+ def adapter_options
226
+ []
227
+ end
228
+
229
+ def create_connection(options = {})
230
+ if adapter == :default
231
+ builder_block = nil
232
+ else
233
+ builder_block = Proc.new do |b|
234
+ b.request :multipart
235
+ b.request :url_encoded
236
+ b.adapter adapter, *adapter_options
237
+ end
238
+ end
239
+
240
+ server = self.class.live_server
241
+ url = '%s://%s:%d' % [server.scheme, server.host, server.port]
242
+
243
+ options[:ssl] ||= {}
244
+ options[:ssl][:ca_file] ||= ENV['SSL_FILE']
245
+
246
+ Faraday::Connection.new(url, options, &builder_block).tap do |conn|
247
+ conn.headers['X-Faraday-Adapter'] = adapter.to_s
248
+ adapter_handler = conn.builder.handlers.last
249
+ conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
250
+ end
251
+ end
252
+ end
253
+ end
254
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
1
+ require File.expand_path('../../helper', __FILE__)
2
2
  require 'stringio'
3
3
  require 'logger'
4
4
 
@@ -15,7 +15,7 @@ module Adapters
15
15
  stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
16
16
  end
17
17
  end
18
- @resp = @conn.get '/hello', :accept => 'text/html'
18
+ @resp = @conn.get '/hello', nil, :accept => 'text/html'
19
19
  end
20
20
 
21
21
  def test_still_returns_output
@@ -23,7 +23,7 @@ module Adapters
23
23
  end
24
24
 
25
25
  def test_logs_method_and_url
26
- assert_match "get /hello", @io.string
26
+ assert_match "get http:/hello", @io.string
27
27
  end
28
28
 
29
29
  def test_logs_request_headers
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../integration', __FILE__)
2
+
3
+ module Adapters
4
+ class NetHttpPersistentTest < Faraday::TestCase
5
+
6
+ def adapter() :net_http_persistent end
7
+
8
+ Integration.apply(self, :NonParallel) do
9
+ def setup
10
+ if defined?(Net::HTTP::Persistent)
11
+ # work around problems with mixed SSL certificates
12
+ # https://github.com/drbrain/net-http-persistent/issues/45
13
+ http = Net::HTTP::Persistent.new('Faraday')
14
+ http.ssl_cleanup(4)
15
+ end
16
+ end if ssl_mode?
17
+ end
18
+
19
+ end
20
+ end
@@ -1,33 +1,14 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
1
+ require File.expand_path('../integration', __FILE__)
2
2
 
3
3
  module Adapters
4
4
  class NetHttpTest < Faraday::TestCase
5
- def setup
6
- @connection = Faraday.new('http://disney.com') do |b|
7
- b.adapter :net_http
8
- end
9
- end
10
5
 
11
- def test_handles_compression_transparently_on_get
12
- stub_request(:get, 'disney.com/hello').with { |request|
13
- accept_encoding = request.headers['Accept-Encoding']
14
- if RUBY_VERSION.index('1.8') == 0
15
- # ruby 1.8 doesn't do any gzip/deflate automatically
16
- accept_encoding == nil
17
- else
18
- # test for a value such as "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
19
- accept_encoding =~ /gzip;.+\bdeflate\b/
20
- end
21
- }
22
- @connection.get('/hello')
23
- end
6
+ def adapter() :net_http end
24
7
 
25
- def test_connect_error_gets_wrapped
26
- stub_request(:get, 'disney.com/hello').to_raise(Errno::ECONNREFUSED)
8
+ behaviors = [:NonParallel]
9
+ behaviors << :Compression if RUBY_VERSION >= '1.9'
10
+
11
+ Integration.apply(self, *behaviors)
27
12
 
28
- assert_raise Faraday::Error::ConnectionFailed do
29
- @connection.get('/hello')
30
- end
31
- end
32
13
  end
33
14
  end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../integration', __FILE__)
2
+
3
+ module Adapters
4
+ class Patron < Faraday::TestCase
5
+
6
+ def adapter() :patron end
7
+
8
+ Integration.apply(self, :NonParallel) do
9
+ # https://github.com/toland/patron/issues/34
10
+ undef :test_PATCH_send_url_encoded_params
11
+
12
+ # https://github.com/toland/patron/issues/52
13
+ undef :test_GET_with_body
14
+
15
+ # no support for SSL peer verification
16
+ undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
17
+ end unless jruby?
18
+
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path("../integration", __FILE__)
2
+ require File.expand_path('../../live_server', __FILE__)
3
+
4
+ module Adapters
5
+ class RackTest < Faraday::TestCase
6
+
7
+ def adapter() :rack end
8
+
9
+ def adapter_options
10
+ [Faraday::LiveServer]
11
+ end
12
+
13
+ # no Integration.apply because this doesn't require a server as a separate process
14
+ include Integration::Common
15
+ include Integration::NonParallel
16
+
17
+ # not using shared test because error is swallowed by Sinatra
18
+ def test_timeout
19
+ conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
20
+ begin
21
+ conn.get '/slow'
22
+ rescue Faraday::Error::ClientError
23
+ end
24
+ end
25
+
26
+ # test not applicable
27
+ undef test_connection_error
28
+ undef test_proxy
29
+ undef test_proxy_auth_fail
30
+ end
31
+ end
@@ -1,9 +1,10 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
1
+ require File.expand_path('../../helper', __FILE__)
2
2
 
3
3
  module Adapters
4
4
  class TestMiddleware < Faraday::TestCase
5
+ Stubs = Faraday::Adapter.lookup_middleware(:test)::Stubs
5
6
  def setup
6
- @stubs = Faraday::Adapter::Test::Stubs.new
7
+ @stubs = Stubs.new
7
8
  @conn = Faraday.new do |builder|
8
9
  builder.adapter :test, @stubs
9
10
  end
@@ -27,6 +28,27 @@ module Adapters
27
28
  assert_equal 'hello', @conn.get("/hello").body
28
29
  end
29
30
 
31
+ def test_middleware_with_get_params
32
+ @stubs.get('/param?a=1') { [200, {}, 'a'] }
33
+ assert_equal 'a', @conn.get('/param?a=1').body
34
+ end
35
+
36
+ def test_middleware_ignores_unspecified_get_params
37
+ @stubs.get('/optional?a=1') { [200, {}, 'a'] }
38
+ assert_equal 'a', @conn.get('/optional?a=1&b=1').body
39
+ assert_equal 'a', @conn.get('/optional?a=1').body
40
+ assert_raises Faraday::Adapter::Test::Stubs::NotFound do
41
+ @conn.get('/optional')
42
+ end
43
+ end
44
+
45
+ def test_middleware_with_http_headers
46
+ @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
47
+ @stubs.get('/yo') { [200, {}, 'b'] }
48
+ assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
49
+ assert_equal 'b', @conn.get('/yo').body
50
+ end
51
+
30
52
  def test_middleware_allow_different_outcomes_for_the_same_request
31
53
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
32
54
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
@@ -34,10 +56,59 @@ module Adapters
34
56
  assert_equal 'world', @conn.get("/hello").body
35
57
  end
36
58
 
59
+ def test_yields_env_to_stubs
60
+ @stubs.get '/hello' do |env|
61
+ assert_equal '/hello', env[:url].path
62
+ assert_equal 'foo.com', env[:url].host
63
+ assert_equal '1', env[:params]['a']
64
+ assert_equal 'text/plain', env[:request_headers]['Accept']
65
+ [200, {}, 'a']
66
+ end
67
+
68
+ @conn.headers['Accept'] = 'text/plain'
69
+ assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
70
+ end
71
+
72
+ def test_parses_params_with_default_encoder
73
+ @stubs.get '/hello' do |env|
74
+ assert_equal '1', env[:params]['a']['b']
75
+ [200, {}, 'a']
76
+ end
77
+
78
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
79
+ end
80
+
81
+ def test_parses_params_with_nested_encoder
82
+ @stubs.get '/hello' do |env|
83
+ assert_equal '1', env[:params]['a']['b']
84
+ [200, {}, 'a']
85
+ end
86
+
87
+ @conn.options.params_encoder = Faraday::NestedParamsEncoder
88
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
89
+ end
90
+
91
+ def test_parses_params_with_flat_encoder
92
+ @stubs.get '/hello' do |env|
93
+ assert_equal '1', env[:params]['a[b]']
94
+ [200, {}, 'a']
95
+ end
96
+
97
+ @conn.options.params_encoder = Faraday::FlatParamsEncoder
98
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
99
+ end
100
+
37
101
  def test_raises_an_error_if_no_stub_is_found_for_request
38
- assert_raise RuntimeError do
102
+ assert_raises Stubs::NotFound do
39
103
  @conn.get('/invalid'){ [200, {}, []] }
40
104
  end
41
105
  end
106
+
107
+ def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
108
+ @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
109
+ assert_raises Faraday::Adapter::Test::Stubs::NotFound do
110
+ @conn.get('/yo')
111
+ end
112
+ end
42
113
  end
43
114
  end
@@ -0,0 +1,28 @@
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
+ # Not a Typhoeus bug, but WEBrick inability to handle "100-continue"
13
+ # which libcurl seems to generate for this particular request:
14
+ undef :test_POST_sends_files
15
+
16
+ # inconsistent outcomes ranging from successful response to connection error
17
+ undef :test_proxy_auth_fail if ssl_mode?
18
+
19
+ def test_binds_local_socket
20
+ host = '1.2.3.4'
21
+ conn = create_connection :request => { :bind => { :host => host } }
22
+ assert_equal host, conn.options[:bind][:host]
23
+ end
24
+
25
+ end unless jruby?
26
+ end
27
+ end
28
+
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../helper', __FILE__)
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