faraday 0.7.4 → 1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +276 -0
- data/LICENSE.md +1 -1
- data/README.md +40 -153
- data/Rakefile +4 -139
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday/adapter/em_http.rb +286 -0
- data/lib/faraday/adapter/em_http_ssl_patch.rb +62 -0
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +69 -0
- data/lib/faraday/adapter/em_synchrony.rb +120 -36
- data/lib/faraday/adapter/excon.rb +108 -12
- data/lib/faraday/adapter/httpclient.rb +152 -0
- data/lib/faraday/adapter/net_http.rb +187 -43
- data/lib/faraday/adapter/net_http_persistent.rb +91 -0
- data/lib/faraday/adapter/patron.rb +106 -10
- data/lib/faraday/adapter/rack.rb +75 -0
- data/lib/faraday/adapter/test.rb +160 -61
- data/lib/faraday/adapter/typhoeus.rb +7 -46
- data/lib/faraday/adapter.rb +105 -33
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +95 -0
- data/lib/faraday/connection.rb +525 -157
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +122 -30
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +14 -22
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +222 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +5 -0
- data/lib/faraday/rack_builder.rb +248 -0
- data/lib/faraday/request/authorization.rb +55 -0
- data/lib/faraday/request/basic_authentication.rb +20 -0
- data/lib/faraday/request/instrumentation.rb +54 -0
- data/lib/faraday/request/multipart.rb +84 -48
- data/lib/faraday/request/retry.rb +239 -0
- data/lib/faraday/request/token_authentication.rb +20 -0
- data/lib/faraday/request/url_encoded.rb +46 -27
- data/lib/faraday/request.rb +112 -50
- data/lib/faraday/response/logger.rb +24 -25
- data/lib/faraday/response/raise_error.rb +40 -11
- data/lib/faraday/response.rb +44 -35
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +72 -117
- data/lib/faraday.rb +142 -64
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +45 -0
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +34 -0
- data/spec/faraday/params_encoders/nested_spec.rb +134 -0
- data/spec/faraday/rack_builder_spec.rb +196 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +274 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +126 -126
- data/Gemfile +0 -29
- data/config.ru +0 -6
- data/faraday.gemspec +0 -92
- data/lib/faraday/adapter/action_dispatch.rb +0 -29
- data/lib/faraday/builder.rb +0 -160
- data/lib/faraday/request/json.rb +0 -35
- data/lib/faraday/upload_io.rb +0 -23
- data/test/adapters/live_test.rb +0 -205
- data/test/adapters/logger_test.rb +0 -37
- data/test/adapters/net_http_test.rb +0 -33
- data/test/adapters/test_middleware_test.rb +0 -70
- data/test/connection_test.rb +0 -254
- data/test/env_test.rb +0 -158
- data/test/helper.rb +0 -41
- data/test/live_server.rb +0 -45
- data/test/middleware_stack_test.rb +0 -118
- data/test/request_middleware_test.rb +0 -116
- data/test/response_middleware_test.rb +0 -74
data/test/adapters/live_test.rb
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
|
2
|
-
|
|
3
|
-
if !Faraday::TestCase::LIVE_SERVER
|
|
4
|
-
warn "warning: test server not specified; skipping live server tests"
|
|
5
|
-
else
|
|
6
|
-
module Adapters
|
|
7
|
-
class LiveTest < Faraday::TestCase
|
|
8
|
-
adapters = if ENV['ADAPTER']
|
|
9
|
-
ENV['ADAPTER'].split(':').map { |name| Faraday::Adapter.lookup_module name.to_sym }
|
|
10
|
-
else
|
|
11
|
-
loaded_adapters = Faraday::Adapter.all_loaded_constants
|
|
12
|
-
loaded_adapters -= [Faraday::Adapter::ActionDispatch]
|
|
13
|
-
loaded_adapters << :default
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
adapters.each do |adapter|
|
|
17
|
-
define_method "test_#{adapter}_GET_retrieves_the_response_body" do
|
|
18
|
-
assert_equal 'hello world', create_connection(adapter).get('hello_world').body
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
define_method "test_#{adapter}_GET_send_url_encoded_params" do
|
|
22
|
-
resp = create_connection(adapter).get do |req|
|
|
23
|
-
req.url 'hello', 'name' => 'zack'
|
|
24
|
-
end
|
|
25
|
-
assert_equal('hello zack', resp.body)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
define_method "test_#{adapter}_GET_retrieves_the_response_headers" do
|
|
29
|
-
response = create_connection(adapter).get('hello_world')
|
|
30
|
-
assert_match /text\/html/, response.headers['Content-Type'], 'original case fail'
|
|
31
|
-
assert_match /text\/html/, response.headers['content-type'], 'lowercase fail'
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# https://github.com/geemus/excon/issues/10
|
|
35
|
-
unless %[Faraday::Adapter::Excon] == adapter.to_s
|
|
36
|
-
define_method "test_#{adapter}_GET_handles_headers_with_multiple_values" do
|
|
37
|
-
response = create_connection(adapter).get('multi')
|
|
38
|
-
assert_equal 'one, two', response.headers['set-cookie']
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
define_method "test_#{adapter}_POST_send_url_encoded_params" do
|
|
43
|
-
resp = create_connection(adapter).post do |req|
|
|
44
|
-
req.url 'echo_name'
|
|
45
|
-
req.body = {'name' => 'zack'}
|
|
46
|
-
end
|
|
47
|
-
assert_equal %("zack"), resp.body
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
define_method "test_#{adapter}_POST_send_url_encoded_nested_params" do
|
|
51
|
-
resp = create_connection(adapter).post do |req|
|
|
52
|
-
req.url 'echo_name'
|
|
53
|
-
req.body = {'name' => {'first' => 'zack'}}
|
|
54
|
-
end
|
|
55
|
-
assert_equal %({"first"=>"zack"}), resp.body
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
define_method "test_#{adapter}_POST_retrieves_the_response_headers" do
|
|
59
|
-
assert_match /text\/html/, create_connection(adapter).post('echo_name').headers['content-type']
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
define_method "test_#{adapter}_POST_sends_files" do
|
|
63
|
-
resp = create_connection(adapter).post do |req|
|
|
64
|
-
req.url 'file'
|
|
65
|
-
req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
|
|
66
|
-
end
|
|
67
|
-
assert_equal "file live_test.rb text/x-ruby", resp.body
|
|
68
|
-
end unless :default == adapter # isn't configured for multipart
|
|
69
|
-
|
|
70
|
-
# https://github.com/toland/patron/issues/9
|
|
71
|
-
if ENV['FORCE'] || %[Faraday::Adapter::Patron] != adapter.to_s
|
|
72
|
-
define_method "test_#{adapter}_PUT_send_url_encoded_params" do
|
|
73
|
-
resp = create_connection(adapter).put do |req|
|
|
74
|
-
req.url 'echo_name'
|
|
75
|
-
req.body = {'name' => 'zack'}
|
|
76
|
-
end
|
|
77
|
-
assert_equal %("zack"), resp.body
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
define_method "test_#{adapter}_PUT_send_url_encoded_nested_params" do
|
|
81
|
-
resp = create_connection(adapter).put do |req|
|
|
82
|
-
req.url 'echo_name'
|
|
83
|
-
req.body = {'name' => {'first' => 'zack'}}
|
|
84
|
-
end
|
|
85
|
-
assert_equal %({"first"=>"zack"}), resp.body
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# https://github.com/toland/patron/issues/9
|
|
90
|
-
# https://github.com/dbalatero/typhoeus/issues/84
|
|
91
|
-
if ENV['FORCE'] || !%w[Faraday::Adapter::Patron Faraday::Adapter::Typhoeus].include?(adapter.to_s)
|
|
92
|
-
define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do
|
|
93
|
-
assert_match /text\/html/, create_connection(adapter).put('echo_name').headers['content-type']
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# https://github.com/toland/patron/issues/34
|
|
98
|
-
unless %w[Faraday::Adapter::Patron Faraday::Adapter::EMSynchrony].include? adapter.to_s
|
|
99
|
-
define_method "test_#{adapter}_PATCH_send_url_encoded_params" do
|
|
100
|
-
resp = create_connection(adapter).patch('echo_name', 'name' => 'zack')
|
|
101
|
-
assert_equal %("zack"), resp.body
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
unless %[Faraday::Adapter::EMSynchrony] == adapter.to_s
|
|
106
|
-
define_method "test_#{adapter}_OPTIONS" do
|
|
107
|
-
resp = create_connection(adapter).run_request(:options, '/options', nil, {})
|
|
108
|
-
assert_equal "hi", resp.body
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
|
|
113
|
-
resp = create_connection(adapter).head do |req|
|
|
114
|
-
req.url 'hello', 'name' => 'zack'
|
|
115
|
-
end
|
|
116
|
-
assert_match /text\/html/, resp.headers['content-type']
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
define_method "test_#{adapter}_HEAD_retrieves_no_response_body" do
|
|
120
|
-
assert_equal '', create_connection(adapter).head('hello_world').body.to_s
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
define_method "test_#{adapter}_HEAD_retrieves_the_response_headers" do
|
|
124
|
-
assert_match /text\/html/, create_connection(adapter).head('hello_world').headers['content-type']
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
define_method "test_#{adapter}_DELETE_retrieves_the_response_headers" do
|
|
128
|
-
assert_match /text\/html/, create_connection(adapter).delete('delete_with_json').headers['content-type']
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
define_method "test_#{adapter}_DELETE_retrieves_the_body" do
|
|
132
|
-
assert_match /deleted/, create_connection(adapter).delete('delete_with_json').body
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
define_method "test_#{adapter}_async_requests_clear_parallel_manager_after_running_a_single_request" do
|
|
136
|
-
connection = create_connection(adapter)
|
|
137
|
-
assert !connection.in_parallel?
|
|
138
|
-
resp = connection.get('hello_world')
|
|
139
|
-
assert !connection.in_parallel?
|
|
140
|
-
assert_equal 'hello world', connection.get('hello_world').body
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
define_method "test_#{adapter}_async_requests_uses_parallel_manager_to_run_multiple_json_requests" do
|
|
144
|
-
resp1, resp2 = nil, nil
|
|
145
|
-
|
|
146
|
-
connection = create_connection(adapter)
|
|
147
|
-
adapter = real_adapter_for(adapter)
|
|
148
|
-
|
|
149
|
-
connection.in_parallel(adapter.setup_parallel_manager) do
|
|
150
|
-
resp1 = connection.get('json')
|
|
151
|
-
resp2 = connection.get('json')
|
|
152
|
-
if adapter.supports_parallel_requests?
|
|
153
|
-
assert connection.in_parallel?
|
|
154
|
-
assert_nil resp1.body
|
|
155
|
-
assert_nil resp2.body
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
assert !connection.in_parallel?
|
|
159
|
-
assert_equal '[1,2,3]', resp1.body
|
|
160
|
-
assert_equal '[1,2,3]', resp2.body
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
if adapter.to_s == "Faraday::Adapter::EMSynchrony"
|
|
164
|
-
instance_methods.grep(%r{Faraday::Adapter::EMSynchrony}).each do |method|
|
|
165
|
-
em = method.to_s.sub %r{^test_}, "test_under_em_"
|
|
166
|
-
define_method em do
|
|
167
|
-
EM.run do
|
|
168
|
-
Fiber.new do
|
|
169
|
-
self.send method
|
|
170
|
-
EM.stop
|
|
171
|
-
end.resume
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def create_connection(adapter)
|
|
179
|
-
if adapter == :default
|
|
180
|
-
builder_block = nil
|
|
181
|
-
else
|
|
182
|
-
builder_block = Proc.new do |b|
|
|
183
|
-
b.request :multipart
|
|
184
|
-
b.request :url_encoded
|
|
185
|
-
b.use adapter
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
Faraday::Connection.new(LIVE_SERVER, &builder_block).tap do |conn|
|
|
190
|
-
conn.headers['X-Faraday-Adapter'] = adapter.to_s
|
|
191
|
-
adapter_handler = conn.builder.handlers.last
|
|
192
|
-
conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
def real_adapter_for(adapter)
|
|
197
|
-
if adapter == :default
|
|
198
|
-
Faraday::Adapter.lookup_module(Faraday.default_adapter)
|
|
199
|
-
else
|
|
200
|
-
adapter
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
end
|
|
205
|
-
end
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
|
2
|
-
require 'stringio'
|
|
3
|
-
require 'logger'
|
|
4
|
-
|
|
5
|
-
module Adapters
|
|
6
|
-
class LoggerTest < Faraday::TestCase
|
|
7
|
-
def setup
|
|
8
|
-
@io = StringIO.new
|
|
9
|
-
@logger = Logger.new(@io)
|
|
10
|
-
@logger.level = Logger::DEBUG
|
|
11
|
-
|
|
12
|
-
@conn = Faraday.new do |b|
|
|
13
|
-
b.response :logger, @logger
|
|
14
|
-
b.adapter :test do |stubs|
|
|
15
|
-
stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
@resp = @conn.get '/hello', :accept => 'text/html'
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def test_still_returns_output
|
|
22
|
-
assert_equal 'hello', @resp.body
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def test_logs_method_and_url
|
|
26
|
-
assert_match "get /hello", @io.string
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def test_logs_request_headers
|
|
30
|
-
assert_match %(Accept: "text/html), @io.string
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def test_logs_response_headers
|
|
34
|
-
assert_match %(Content-Type: "text/html), @io.string
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
|
2
|
-
|
|
3
|
-
module Adapters
|
|
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
|
-
|
|
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
|
|
24
|
-
|
|
25
|
-
def test_connect_error_gets_wrapped
|
|
26
|
-
stub_request(:get, 'disney.com/hello').to_raise(Errno::ECONNREFUSED)
|
|
27
|
-
|
|
28
|
-
assert_raise Faraday::Error::ConnectionFailed do
|
|
29
|
-
@connection.get('/hello')
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
|
2
|
-
|
|
3
|
-
module Adapters
|
|
4
|
-
class TestMiddleware < Faraday::TestCase
|
|
5
|
-
def setup
|
|
6
|
-
@stubs = Faraday::Adapter::Test::Stubs.new
|
|
7
|
-
@conn = Faraday.new do |builder|
|
|
8
|
-
builder.adapter :test, @stubs
|
|
9
|
-
end
|
|
10
|
-
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
|
11
|
-
@resp = @conn.get('/hello')
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def test_middleware_with_simple_path_sets_status
|
|
15
|
-
assert_equal 200, @resp.status
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def test_middleware_with_simple_path_sets_headers
|
|
19
|
-
assert_equal 'text/html', @resp.headers['Content-Type']
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def test_middleware_with_simple_path_sets_body
|
|
23
|
-
assert_equal 'hello', @resp.body
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def test_middleware_can_be_called_several_times
|
|
27
|
-
assert_equal 'hello', @conn.get("/hello").body
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def test_middleware_with_get_params
|
|
31
|
-
@stubs.get('/param?a=1') { [200, {}, 'a'] }
|
|
32
|
-
assert_equal 'a', @conn.get('/param?a=1').body
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def test_middleware_ignores_unspecified_get_params
|
|
36
|
-
@stubs.get('/optional?a=1') { [200, {}, 'a'] }
|
|
37
|
-
assert_equal 'a', @conn.get('/optional?a=1&b=1').body
|
|
38
|
-
assert_equal 'a', @conn.get('/optional?a=1').body
|
|
39
|
-
assert_raise Faraday::Adapter::Test::Stubs::NotFound do
|
|
40
|
-
@conn.get('/optional')
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def test_middleware_allow_different_outcomes_for_the_same_request
|
|
45
|
-
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
|
46
|
-
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
|
47
|
-
assert_equal 'hello', @conn.get("/hello").body
|
|
48
|
-
assert_equal 'world', @conn.get("/hello").body
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def test_yields_env_to_stubs
|
|
52
|
-
@stubs.get '/hello' do |env|
|
|
53
|
-
assert_equal '/hello', env[:url].path
|
|
54
|
-
assert_equal 'foo.com', env[:url].host
|
|
55
|
-
assert_equal '1', env[:params]['a']
|
|
56
|
-
assert_equal 'text/plain', env[:request_headers]['Accept']
|
|
57
|
-
[200, {}, 'a']
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
@conn.headers['Accept'] = 'text/plain'
|
|
61
|
-
assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def test_raises_an_error_if_no_stub_is_found_for_request
|
|
65
|
-
assert_raise Faraday::Adapter::Test::Stubs::NotFound do
|
|
66
|
-
@conn.get('/invalid'){ [200, {}, []] }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
data/test/connection_test.rb
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
|
2
|
-
|
|
3
|
-
class TestConnection < Faraday::TestCase
|
|
4
|
-
def test_initialize_parses_host_out_of_given_url
|
|
5
|
-
conn = Faraday::Connection.new "http://sushi.com"
|
|
6
|
-
assert_equal 'sushi.com', conn.host
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def test_initialize_parses_nil_port_out_of_given_url
|
|
10
|
-
conn = Faraday::Connection.new "http://sushi.com"
|
|
11
|
-
assert_nil conn.port
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def test_initialize_parses_scheme_out_of_given_url
|
|
15
|
-
conn = Faraday::Connection.new "http://sushi.com"
|
|
16
|
-
assert_equal 'http', conn.scheme
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def test_initialize_parses_port_out_of_given_url
|
|
20
|
-
conn = Faraday::Connection.new "http://sushi.com:815"
|
|
21
|
-
assert_equal 815, conn.port
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def test_initialize_parses_nil_path_prefix_out_of_given_url
|
|
25
|
-
conn = Faraday::Connection.new "http://sushi.com"
|
|
26
|
-
assert_equal '/', conn.path_prefix
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def test_initialize_parses_path_prefix_out_of_given_url
|
|
30
|
-
conn = Faraday::Connection.new "http://sushi.com/fish"
|
|
31
|
-
assert_equal '/fish', conn.path_prefix
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def test_initialize_parses_path_prefix_out_of_given_url_option
|
|
35
|
-
conn = Faraday::Connection.new :url => "http://sushi.com/fish"
|
|
36
|
-
assert_equal '/fish', conn.path_prefix
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def test_initialize_stores_default_params_from_options
|
|
40
|
-
conn = Faraday::Connection.new :params => {:a => 1}
|
|
41
|
-
assert_equal({'a' => 1}, conn.params)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def test_initialize_stores_default_params_from_uri
|
|
45
|
-
conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
|
|
46
|
-
assert_equal({'a' => '1'}, conn.params)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def test_initialize_stores_default_params_from_uri_and_options
|
|
50
|
-
conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}
|
|
51
|
-
assert_equal({'a' => 3, 'b' => '2'}, conn.params)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def test_initialize_stores_default_headers_from_options
|
|
55
|
-
conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
|
|
56
|
-
assert_equal 'Faraday', conn.headers['User-agent']
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def test_basic_auth_sets_authorization_header
|
|
60
|
-
conn = Faraday::Connection.new
|
|
61
|
-
conn.basic_auth 'Aladdin', 'open sesame'
|
|
62
|
-
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def test_long_basic_auth_sets_authorization_header_without_new_lines
|
|
66
|
-
conn = Faraday::Connection.new
|
|
67
|
-
conn.basic_auth "A" * 255, ""
|
|
68
|
-
assert_equal "Basic #{'QUFB' * 85}Og==", conn.headers['Authorization']
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def test_auto_parses_basic_auth_from_url
|
|
72
|
-
conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
|
|
73
|
-
assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def test_token_auth_sets_authorization_header
|
|
77
|
-
conn = Faraday::Connection.new
|
|
78
|
-
conn.token_auth 'abcdef'
|
|
79
|
-
assert_equal 'Token token="abcdef"', conn.headers['Authorization']
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def test_token_auth_with_options_sets_authorization_header
|
|
83
|
-
conn = Faraday::Connection.new
|
|
84
|
-
conn.token_auth 'abcdef', :nonce => 'abc'
|
|
85
|
-
assert_equal 'Token token="abcdef",
|
|
86
|
-
nonce="abc"', conn.headers['Authorization']
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def test_build_url_uses_connection_host_as_default_uri_host
|
|
90
|
-
conn = Faraday::Connection.new
|
|
91
|
-
conn.host = 'sushi.com'
|
|
92
|
-
uri = conn.build_url("/sake.html")
|
|
93
|
-
assert_equal 'sushi.com', uri.host
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def test_build_url_uses_connection_port_as_default_uri_port
|
|
97
|
-
conn = Faraday::Connection.new
|
|
98
|
-
conn.port = 23
|
|
99
|
-
uri = conn.build_url("http://sushi.com")
|
|
100
|
-
assert_equal 23, uri.port
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def test_build_url_uses_connection_scheme_as_default_uri_scheme
|
|
104
|
-
conn = Faraday::Connection.new 'http://sushi.com'
|
|
105
|
-
uri = conn.build_url("/sake.html")
|
|
106
|
-
assert_equal 'http', uri.scheme
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def test_build_url_uses_connection_path_prefix_to_customize_path
|
|
110
|
-
conn = Faraday::Connection.new
|
|
111
|
-
conn.path_prefix = '/fish'
|
|
112
|
-
uri = conn.build_url("sake.html")
|
|
113
|
-
assert_equal '/fish/sake.html', uri.path
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def test_build_url_uses_root_connection_path_prefix_to_customize_path
|
|
117
|
-
conn = Faraday::Connection.new
|
|
118
|
-
conn.path_prefix = '/'
|
|
119
|
-
uri = conn.build_url("sake.html")
|
|
120
|
-
assert_equal '/sake.html', uri.path
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def test_build_url_forces_connection_path_prefix_to_be_absolute
|
|
124
|
-
conn = Faraday::Connection.new
|
|
125
|
-
conn.path_prefix = 'fish'
|
|
126
|
-
uri = conn.build_url("sake.html")
|
|
127
|
-
assert_equal '/fish/sake.html', uri.path
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def test_build_url_ignores_connection_path_prefix_trailing_slash
|
|
131
|
-
conn = Faraday::Connection.new
|
|
132
|
-
conn.path_prefix = '/fish/'
|
|
133
|
-
uri = conn.build_url("sake.html")
|
|
134
|
-
assert_equal '/fish/sake.html', uri.path
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
|
|
138
|
-
conn = Faraday::Connection.new
|
|
139
|
-
conn.path_prefix = '/fish'
|
|
140
|
-
uri = conn.build_url("/sake.html")
|
|
141
|
-
assert_equal '/sake.html', uri.path
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
def test_build_url_parses_url_params_into_path
|
|
145
|
-
conn = Faraday::Connection.new
|
|
146
|
-
uri = conn.build_url("http://sushi.com/sake.html")
|
|
147
|
-
assert_equal '/sake.html', uri.path
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
def test_build_url_doesnt_add_ending_slash
|
|
151
|
-
conn = Faraday::Connection.new
|
|
152
|
-
conn.url_prefix = "http://sushi.com/nigiri"
|
|
153
|
-
uri = conn.build_url(nil)
|
|
154
|
-
assert_equal "/nigiri", uri.path
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
def test_build_url_parses_url_params_into_query
|
|
158
|
-
conn = Faraday::Connection.new
|
|
159
|
-
uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
|
|
160
|
-
assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def test_build_url_mashes_default_and_given_params_together
|
|
164
|
-
conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
|
|
165
|
-
url = conn.build_url("nigiri?page=1", :limit => 5)
|
|
166
|
-
assert_equal %w[format=json limit=5 page=1 token=abc], url.query.split('&').sort
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
def test_build_url_overrides_default_params_with_given_params
|
|
170
|
-
conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
|
|
171
|
-
url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
|
|
172
|
-
assert_equal %w[format=xml limit=5 page=1 token=def], url.query.split('&').sort
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
def test_default_params_hash_has_indifferent_access
|
|
176
|
-
conn = Faraday::Connection.new :params => {'format' => 'json'}
|
|
177
|
-
assert conn.params.has_key?(:format)
|
|
178
|
-
conn.params[:format] = 'xml'
|
|
179
|
-
url = conn.build_url("")
|
|
180
|
-
assert_equal %w[format=xml], url.query.split('&').sort
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
def test_build_url_parses_url
|
|
184
|
-
conn = Faraday::Connection.new
|
|
185
|
-
uri = conn.build_url("http://sushi.com/sake.html")
|
|
186
|
-
assert_equal "http", uri.scheme
|
|
187
|
-
assert_equal "sushi.com", uri.host
|
|
188
|
-
assert_equal '/sake.html', uri.path
|
|
189
|
-
assert_nil uri.port
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
def test_build_url_parses_url_and_changes_scheme
|
|
193
|
-
conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
|
|
194
|
-
conn.scheme = 'https'
|
|
195
|
-
uri = conn.build_url("sake.html")
|
|
196
|
-
assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
def test_proxy_accepts_string
|
|
200
|
-
conn = Faraday::Connection.new
|
|
201
|
-
conn.proxy 'http://proxy.com'
|
|
202
|
-
assert_equal 'proxy.com', conn.proxy[:uri].host
|
|
203
|
-
assert_equal [:uri], conn.proxy.keys
|
|
204
|
-
end
|
|
205
|
-
|
|
206
|
-
def test_proxy_accepts_uri
|
|
207
|
-
conn = Faraday::Connection.new
|
|
208
|
-
conn.proxy Addressable::URI.parse('http://proxy.com')
|
|
209
|
-
assert_equal 'proxy.com', conn.proxy[:uri].host
|
|
210
|
-
assert_equal [:uri], conn.proxy.keys
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
def test_proxy_accepts_hash_with_string_uri
|
|
214
|
-
conn = Faraday::Connection.new
|
|
215
|
-
conn.proxy :uri => 'http://proxy.com', :user => 'rick'
|
|
216
|
-
assert_equal 'proxy.com', conn.proxy[:uri].host
|
|
217
|
-
assert_equal 'rick', conn.proxy[:user]
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
def test_proxy_accepts_hash
|
|
221
|
-
conn = Faraday::Connection.new
|
|
222
|
-
conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
|
|
223
|
-
assert_equal 'proxy.com', conn.proxy[:uri].host
|
|
224
|
-
assert_equal 'rick', conn.proxy[:user]
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
def test_proxy_requires_uri
|
|
228
|
-
conn = Faraday::Connection.new
|
|
229
|
-
assert_raises ArgumentError do
|
|
230
|
-
conn.proxy :uri => :bad_uri, :user => 'rick'
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
|
|
235
|
-
conn = Faraday::Connection.new
|
|
236
|
-
url = conn.build_url('', 'a[b]' => '1 + 2')
|
|
237
|
-
assert_equal "a%5Bb%5D=1%20%2B%202", url.query
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
def test_dups_connection_object
|
|
241
|
-
conn = Faraday::Connection.new 'http://sushi.com/foo' do |b|
|
|
242
|
-
b.adapter :net_http
|
|
243
|
-
end
|
|
244
|
-
conn.headers['content-type'] = 'text/plain'
|
|
245
|
-
conn.params['a'] = '1'
|
|
246
|
-
|
|
247
|
-
duped = conn.dup
|
|
248
|
-
assert_equal conn.build_url(''), duped.build_url('')
|
|
249
|
-
[:headers, :params, :builder].each do |attr|
|
|
250
|
-
assert_equal conn.send(attr), duped.send(attr)
|
|
251
|
-
assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
|
-
end
|