faraday 0.6.1 → 0.7.4
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.
- data/Gemfile +24 -14
- data/{LICENSE → LICENSE.md} +1 -1
- data/README.md +29 -27
- data/Rakefile +10 -2
- data/config.ru +6 -0
- data/faraday.gemspec +10 -7
- data/lib/faraday/adapter/net_http.rb +7 -7
- data/lib/faraday/adapter/patron.rb +11 -5
- data/lib/faraday/adapter/test.rb +30 -4
- data/lib/faraday/adapter/typhoeus.rb +7 -0
- data/lib/faraday/builder.rb +24 -1
- data/lib/faraday/connection.rb +51 -29
- data/lib/faraday/error.rb +2 -0
- data/lib/faraday/request/json.rb +5 -1
- data/lib/faraday/request.rb +19 -26
- data/lib/faraday/response.rb +2 -2
- data/lib/faraday/utils.rb +79 -18
- data/lib/faraday.rb +1 -1
- data/test/adapters/live_test.rb +30 -11
- data/test/adapters/test_middleware_test.rb +28 -1
- data/test/connection_test.rb +29 -20
- data/test/env_test.rb +45 -21
- data/test/helper.rb +4 -8
- data/test/live_server.rb +7 -3
- data/test/middleware_stack_test.rb +29 -2
- data/test/request_middleware_test.rb +33 -2
- data/test/response_middleware_test.rb +27 -0
- metadata +35 -13
data/lib/faraday/request.rb
CHANGED
|
@@ -9,7 +9,7 @@ module Faraday
|
|
|
9
9
|
# req.body = 'abc'
|
|
10
10
|
# end
|
|
11
11
|
#
|
|
12
|
-
class Request < Struct.new(:path, :params, :headers, :body)
|
|
12
|
+
class Request < Struct.new(:path, :params, :headers, :body, :options)
|
|
13
13
|
extend AutoloadHelper
|
|
14
14
|
|
|
15
15
|
autoload_all 'faraday/request',
|
|
@@ -22,16 +22,19 @@ module Faraday
|
|
|
22
22
|
:url_encoded => :UrlEncoded,
|
|
23
23
|
:multipart => :Multipart
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
attr_reader :method
|
|
26
|
+
|
|
27
|
+
def self.create(request_method)
|
|
28
|
+
new(request_method).tap do |request|
|
|
29
|
+
yield request if block_given?
|
|
30
|
+
end
|
|
29
31
|
end
|
|
30
32
|
|
|
31
|
-
def
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
def initialize(request_method)
|
|
34
|
+
@method = request_method
|
|
35
|
+
self.params = {}
|
|
36
|
+
self.headers = {}
|
|
37
|
+
self.options = {}
|
|
35
38
|
end
|
|
36
39
|
|
|
37
40
|
def url(path, params = {})
|
|
@@ -63,29 +66,19 @@ module Faraday
|
|
|
63
66
|
# :user - Proxy server username
|
|
64
67
|
# :password - Proxy server password
|
|
65
68
|
# :ssl - Hash of options for configuring SSL requests.
|
|
66
|
-
def
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
connection.
|
|
70
|
-
|
|
69
|
+
def to_env(connection)
|
|
70
|
+
env_params = connection.params.merge(params)
|
|
71
|
+
env_headers = connection.headers.merge(headers)
|
|
72
|
+
request_options = Utils.deep_merge(connection.options, options)
|
|
73
|
+
Utils.deep_merge!(request_options, :proxy => connection.proxy)
|
|
71
74
|
|
|
72
|
-
{ :method =>
|
|
75
|
+
{ :method => method,
|
|
73
76
|
:body => body,
|
|
74
77
|
:url => connection.build_url(path, env_params),
|
|
75
78
|
:request_headers => env_headers,
|
|
76
79
|
:parallel_manager => connection.parallel_manager,
|
|
77
|
-
:request =>
|
|
80
|
+
:request => request_options,
|
|
78
81
|
:ssl => connection.ssl}
|
|
79
82
|
end
|
|
80
|
-
|
|
81
|
-
def run(connection, request_method)
|
|
82
|
-
app = lambda { |env|
|
|
83
|
-
response = Response.new
|
|
84
|
-
response.finish(env) unless env[:parallel_manager]
|
|
85
|
-
env[:response] = response
|
|
86
|
-
}
|
|
87
|
-
env = to_env_hash(connection, request_method)
|
|
88
|
-
connection.builder.to_app(app).call(env)
|
|
89
|
-
end
|
|
90
83
|
end
|
|
91
84
|
end
|
data/lib/faraday/response.rb
CHANGED
|
@@ -14,7 +14,7 @@ module Faraday
|
|
|
14
14
|
# Calls the `parse` method if defined
|
|
15
15
|
def on_complete(env)
|
|
16
16
|
if respond_to? :parse
|
|
17
|
-
env[:body] = parse(env[:body])
|
|
17
|
+
env[:body] = parse(env[:body]) unless [204,304].index env[:status]
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
end
|
|
@@ -72,7 +72,7 @@ module Faraday
|
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def success?
|
|
75
|
-
status
|
|
75
|
+
(200..299).include?(status)
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
# because @on_complete_callbacks cannot be marshalled
|
data/lib/faraday/utils.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'rack/utils'
|
|
2
|
+
|
|
2
3
|
module Faraday
|
|
3
4
|
module Utils
|
|
4
5
|
include Rack::Utils
|
|
@@ -17,7 +18,7 @@ module Faraday
|
|
|
17
18
|
end
|
|
18
19
|
end
|
|
19
20
|
KeyMap[:etag] = "ETag"
|
|
20
|
-
|
|
21
|
+
|
|
21
22
|
def [](k)
|
|
22
23
|
super(KeyMap[k])
|
|
23
24
|
end
|
|
@@ -27,9 +28,9 @@ module Faraday
|
|
|
27
28
|
v = v.to_ary.join(', ') if v.respond_to? :to_ary
|
|
28
29
|
super(KeyMap[k], v)
|
|
29
30
|
end
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
alias_method :update, :merge!
|
|
32
|
-
|
|
33
|
+
|
|
33
34
|
def parse(header_string)
|
|
34
35
|
return unless header_string && !header_string.empty?
|
|
35
36
|
header_string.split(/\r\n/).
|
|
@@ -44,8 +45,65 @@ module Faraday
|
|
|
44
45
|
end
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
#
|
|
48
|
-
|
|
48
|
+
# hash with stringified keys
|
|
49
|
+
class ParamsHash < Hash
|
|
50
|
+
def [](key)
|
|
51
|
+
super(convert_key(key))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def []=(key, value)
|
|
55
|
+
super(convert_key(key), value)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def delete(key)
|
|
59
|
+
super(convert_key(key))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def include?(key)
|
|
63
|
+
super(convert_key(key))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
alias_method :has_key?, :include?
|
|
67
|
+
alias_method :member?, :include?
|
|
68
|
+
alias_method :key?, :include?
|
|
69
|
+
|
|
70
|
+
def update(params)
|
|
71
|
+
params.each do |key, value|
|
|
72
|
+
self[key] = value
|
|
73
|
+
end
|
|
74
|
+
self
|
|
75
|
+
end
|
|
76
|
+
alias_method :merge!, :update
|
|
77
|
+
|
|
78
|
+
def merge(params)
|
|
79
|
+
dup.update(params)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def replace(other)
|
|
83
|
+
clear
|
|
84
|
+
update(other)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def merge_query(query)
|
|
88
|
+
if query && !query.empty?
|
|
89
|
+
update Utils.parse_query(query)
|
|
90
|
+
end
|
|
91
|
+
self
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def to_query
|
|
95
|
+
Utils.build_query(self)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def convert_key(key)
|
|
101
|
+
key.to_s
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Make Rack::Utils methods public.
|
|
106
|
+
public :build_query, :parse_query
|
|
49
107
|
|
|
50
108
|
# Override Rack's version since it doesn't handle non-String values
|
|
51
109
|
def build_nested_query(value, prefix = nil)
|
|
@@ -72,24 +130,27 @@ module Faraday
|
|
|
72
130
|
end
|
|
73
131
|
end
|
|
74
132
|
|
|
75
|
-
#
|
|
76
|
-
def
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
end
|
|
133
|
+
# Receives a URL and returns just the path with the query string sorted.
|
|
134
|
+
def normalize_path(url)
|
|
135
|
+
(url.path != "" ? url.path : "/") +
|
|
136
|
+
(url.query ? "?#{sort_query_params(url.query)}" : "")
|
|
80
137
|
end
|
|
81
138
|
|
|
82
|
-
#
|
|
83
|
-
def
|
|
84
|
-
|
|
85
|
-
|
|
139
|
+
# Recursive hash update
|
|
140
|
+
def deep_merge!(target, hash)
|
|
141
|
+
hash.each do |key, value|
|
|
142
|
+
if Hash === value and Hash === target[key]
|
|
143
|
+
target[key] = deep_merge(target[key], value)
|
|
144
|
+
else
|
|
145
|
+
target[key] = value
|
|
146
|
+
end
|
|
86
147
|
end
|
|
148
|
+
target
|
|
87
149
|
end
|
|
88
150
|
|
|
89
|
-
#
|
|
90
|
-
def
|
|
91
|
-
(
|
|
92
|
-
(url.query ? "?#{sort_query_params(url.query)}" : "")
|
|
151
|
+
# Recursive hash merge
|
|
152
|
+
def deep_merge(source, hash)
|
|
153
|
+
deep_merge!(source.dup, hash)
|
|
93
154
|
end
|
|
94
155
|
|
|
95
156
|
protected
|
data/lib/faraday.rb
CHANGED
data/test/adapters/live_test.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
|
2
2
|
|
|
3
3
|
if !Faraday::TestCase::LIVE_SERVER
|
|
4
|
-
warn "warning: test server
|
|
4
|
+
warn "warning: test server not specified; skipping live server tests"
|
|
5
5
|
else
|
|
6
6
|
module Adapters
|
|
7
7
|
class LiveTest < Faraday::TestCase
|
|
@@ -67,8 +67,8 @@ else
|
|
|
67
67
|
assert_equal "file live_test.rb text/x-ruby", resp.body
|
|
68
68
|
end unless :default == adapter # isn't configured for multipart
|
|
69
69
|
|
|
70
|
-
#
|
|
71
|
-
if ENV['FORCE'] ||
|
|
70
|
+
# https://github.com/toland/patron/issues/9
|
|
71
|
+
if ENV['FORCE'] || %[Faraday::Adapter::Patron] != adapter.to_s
|
|
72
72
|
define_method "test_#{adapter}_PUT_send_url_encoded_params" do
|
|
73
73
|
resp = create_connection(adapter).put do |req|
|
|
74
74
|
req.url 'echo_name'
|
|
@@ -84,12 +84,31 @@ else
|
|
|
84
84
|
end
|
|
85
85
|
assert_equal %({"first"=>"zack"}), resp.body
|
|
86
86
|
end
|
|
87
|
+
end
|
|
87
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)
|
|
88
92
|
define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do
|
|
89
93
|
assert_match /text\/html/, create_connection(adapter).put('echo_name').headers['content-type']
|
|
90
94
|
end
|
|
91
95
|
end
|
|
92
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
|
+
|
|
93
112
|
define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
|
|
94
113
|
resp = create_connection(adapter).head do |req|
|
|
95
114
|
req.url 'hello', 'name' => 'zack'
|
|
@@ -158,19 +177,19 @@ else
|
|
|
158
177
|
|
|
159
178
|
def create_connection(adapter)
|
|
160
179
|
if adapter == :default
|
|
161
|
-
|
|
162
|
-
conn.url_prefix = LIVE_SERVER
|
|
163
|
-
conn.headers['X-Faraday-Adapter'] = adapter.to_s
|
|
164
|
-
end
|
|
180
|
+
builder_block = nil
|
|
165
181
|
else
|
|
166
|
-
|
|
182
|
+
builder_block = Proc.new do |b|
|
|
167
183
|
b.request :multipart
|
|
168
184
|
b.request :url_encoded
|
|
169
185
|
b.use adapter
|
|
170
186
|
end
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
|
|
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
|
|
174
193
|
end
|
|
175
194
|
end
|
|
176
195
|
|
|
@@ -27,6 +27,20 @@ module Adapters
|
|
|
27
27
|
assert_equal 'hello', @conn.get("/hello").body
|
|
28
28
|
end
|
|
29
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
|
+
|
|
30
44
|
def test_middleware_allow_different_outcomes_for_the_same_request
|
|
31
45
|
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
|
32
46
|
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
|
@@ -34,8 +48,21 @@ module Adapters
|
|
|
34
48
|
assert_equal 'world', @conn.get("/hello").body
|
|
35
49
|
end
|
|
36
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
|
+
|
|
37
64
|
def test_raises_an_error_if_no_stub_is_found_for_request
|
|
38
|
-
assert_raise
|
|
65
|
+
assert_raise Faraday::Adapter::Test::Stubs::NotFound do
|
|
39
66
|
@conn.get('/invalid'){ [200, {}, []] }
|
|
40
67
|
end
|
|
41
68
|
end
|
data/test/connection_test.rb
CHANGED
|
@@ -38,18 +38,22 @@ class TestConnection < Faraday::TestCase
|
|
|
38
38
|
|
|
39
39
|
def test_initialize_stores_default_params_from_options
|
|
40
40
|
conn = Faraday::Connection.new :params => {:a => 1}
|
|
41
|
-
assert_equal 1, conn.params
|
|
41
|
+
assert_equal({'a' => 1}, conn.params)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def test_initialize_stores_default_params_from_uri
|
|
45
|
-
conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
|
|
46
|
-
assert_equal '1', conn.params
|
|
47
|
-
|
|
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)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
def test_initialize_stores_default_headers_from_options
|
|
51
|
-
conn = Faraday::Connection.new :headers => {:
|
|
52
|
-
assert_equal '
|
|
55
|
+
conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
|
|
56
|
+
assert_equal 'Faraday', conn.headers['User-agent']
|
|
53
57
|
end
|
|
54
58
|
|
|
55
59
|
def test_basic_auth_sets_authorization_header
|
|
@@ -143,6 +147,13 @@ class TestConnection < Faraday::TestCase
|
|
|
143
147
|
assert_equal '/sake.html', uri.path
|
|
144
148
|
end
|
|
145
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
|
+
|
|
146
157
|
def test_build_url_parses_url_params_into_query
|
|
147
158
|
conn = Faraday::Connection.new
|
|
148
159
|
uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
|
|
@@ -152,21 +163,21 @@ class TestConnection < Faraday::TestCase
|
|
|
152
163
|
def test_build_url_mashes_default_and_given_params_together
|
|
153
164
|
conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
|
|
154
165
|
url = conn.build_url("nigiri?page=1", :limit => 5)
|
|
155
|
-
|
|
156
|
-
assert_match /page=1/, url.query
|
|
157
|
-
assert_match /format=json/, url.query
|
|
158
|
-
assert_match /token=abc/, url.query
|
|
166
|
+
assert_equal %w[format=json limit=5 page=1 token=abc], url.query.split('&').sort
|
|
159
167
|
end
|
|
160
168
|
|
|
161
169
|
def test_build_url_overrides_default_params_with_given_params
|
|
162
170
|
conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
|
|
163
171
|
url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
|
170
181
|
end
|
|
171
182
|
|
|
172
183
|
def test_build_url_parses_url
|
|
@@ -222,10 +233,8 @@ class TestConnection < Faraday::TestCase
|
|
|
222
233
|
|
|
223
234
|
def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
|
|
224
235
|
conn = Faraday::Connection.new
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
end
|
|
228
|
-
assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
|
|
236
|
+
url = conn.build_url('', 'a[b]' => '1 + 2')
|
|
237
|
+
assert_equal "a%5Bb%5D=1%20%2B%202", url.query
|
|
229
238
|
end
|
|
230
239
|
|
|
231
240
|
def test_dups_connection_object
|
data/test/env_test.rb
CHANGED
|
@@ -2,55 +2,79 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
|
|
2
2
|
|
|
3
3
|
class EnvTest < Faraday::TestCase
|
|
4
4
|
def setup
|
|
5
|
-
@conn = Faraday.new :url => 'http://sushi.com/api',
|
|
5
|
+
@conn = Faraday.new :url => 'http://sushi.com/api',
|
|
6
|
+
:headers => {'Mime-Version' => '1.0'},
|
|
7
|
+
:request => {:oauth => {:consumer_key => 'anonymous'}}
|
|
8
|
+
|
|
6
9
|
@conn.options[:timeout] = 3
|
|
7
10
|
@conn.options[:open_timeout] = 5
|
|
8
11
|
@conn.ssl[:verify] = false
|
|
9
12
|
@conn.proxy 'http://proxy.com'
|
|
10
|
-
@input = { :body => 'abc' }
|
|
11
|
-
@env = env_for @conn do |req|
|
|
12
|
-
req.url 'foo.json', 'a' => 1
|
|
13
|
-
req['Server'] = 'Faraday'
|
|
14
|
-
req.body = @input[:body]
|
|
15
|
-
end
|
|
16
13
|
end
|
|
17
14
|
|
|
18
15
|
def test_request_create_stores_method
|
|
19
|
-
|
|
16
|
+
env = make_env(:get)
|
|
17
|
+
assert_equal :get, env[:method]
|
|
20
18
|
end
|
|
21
19
|
|
|
22
20
|
def test_request_create_stores_addressable_uri
|
|
23
|
-
|
|
21
|
+
env = make_env do |req|
|
|
22
|
+
req.url 'foo.json', 'a' => 1
|
|
23
|
+
end
|
|
24
|
+
assert_equal 'http://sushi.com/api/foo.json?a=1', env[:url].to_s
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
def test_request_create_stores_headers
|
|
27
|
-
|
|
28
|
+
env = make_env do |req|
|
|
29
|
+
req['Server'] = 'Faraday'
|
|
30
|
+
end
|
|
31
|
+
headers = env[:request_headers]
|
|
28
32
|
assert_equal '1.0', headers['mime-version']
|
|
29
33
|
assert_equal 'Faraday', headers['server']
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
def test_request_create_stores_body
|
|
33
|
-
|
|
37
|
+
env = make_env do |req|
|
|
38
|
+
req.body = 'hi'
|
|
39
|
+
end
|
|
40
|
+
assert_equal 'hi', env[:body]
|
|
34
41
|
end
|
|
35
42
|
|
|
36
|
-
def
|
|
37
|
-
|
|
38
|
-
assert_equal
|
|
43
|
+
def test_global_request_options
|
|
44
|
+
env = make_env
|
|
45
|
+
assert_equal 3, env[:request][:timeout]
|
|
46
|
+
assert_equal 5, env[:request][:open_timeout]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_per_request_options
|
|
50
|
+
env = make_env do |req|
|
|
51
|
+
req.options[:timeout] = 10
|
|
52
|
+
req.options[:custom] = true
|
|
53
|
+
req.options[:oauth] = {:consumer_secret => 'xyz'}
|
|
54
|
+
end
|
|
55
|
+
assert_equal 10, env[:request][:timeout]
|
|
56
|
+
assert_equal 5, env[:request][:open_timeout]
|
|
57
|
+
assert_equal true, env[:request][:custom]
|
|
58
|
+
|
|
59
|
+
oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'}
|
|
60
|
+
assert_equal oauth_expected, env[:request][:oauth]
|
|
39
61
|
end
|
|
40
62
|
|
|
41
63
|
def test_request_create_stores_ssl_options
|
|
42
|
-
|
|
64
|
+
env = make_env
|
|
65
|
+
assert_equal false, env[:ssl][:verify]
|
|
43
66
|
end
|
|
44
67
|
|
|
45
68
|
def test_request_create_stores_proxy_options
|
|
46
|
-
|
|
69
|
+
env = make_env
|
|
70
|
+
assert_equal 'proxy.com', env[:request][:proxy][:uri].host
|
|
47
71
|
end
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def make_env(method = :get, connection = @conn, &block)
|
|
76
|
+
request = Faraday::Request.create(method, &block)
|
|
77
|
+
request.to_env(connection)
|
|
54
78
|
end
|
|
55
79
|
end
|
|
56
80
|
|
data/test/helper.rb
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
+
require 'rubygems'
|
|
1
2
|
require 'test/unit'
|
|
2
|
-
unless ENV['BUNDLE_GEMFILE']
|
|
3
|
-
require 'rubygems'
|
|
4
|
-
require 'bundler'
|
|
5
|
-
Bundler.setup
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
require 'webmock/test_unit'
|
|
9
|
-
WebMock.disable_net_connect!(:allow_localhost => true)
|
|
10
3
|
|
|
11
4
|
if ENV['LEFTRIGHT']
|
|
12
5
|
begin
|
|
@@ -43,3 +36,6 @@ module Faraday
|
|
|
43
36
|
end unless defined? ::MiniTest
|
|
44
37
|
end
|
|
45
38
|
end
|
|
39
|
+
|
|
40
|
+
require 'webmock/test_unit'
|
|
41
|
+
WebMock.disable_net_connect!(:allow => Faraday::TestCase::LIVE_SERVER)
|
data/test/live_server.rb
CHANGED
|
@@ -20,18 +20,22 @@ post '/file' do
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
[:get, :post].each do |method|
|
|
24
24
|
send(method, '/hello') do
|
|
25
25
|
"hello #{params[:name]}"
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
%w[
|
|
30
|
-
send(
|
|
29
|
+
%w[POST PUT PATCH].each do |http_method|
|
|
30
|
+
settings.send(:route, http_method, '/echo_name') do
|
|
31
31
|
params[:name].inspect
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
options '/options' do
|
|
36
|
+
'hi'
|
|
37
|
+
end
|
|
38
|
+
|
|
35
39
|
delete '/delete_with_json' do
|
|
36
40
|
%/{"deleted":true}/
|
|
37
41
|
end
|
|
@@ -26,8 +26,6 @@ class MiddlewareStackTest < Faraday::TestCase
|
|
|
26
26
|
|
|
27
27
|
def test_allows_rebuilding
|
|
28
28
|
build_stack Apple
|
|
29
|
-
assert_handlers %w[Apple]
|
|
30
|
-
|
|
31
29
|
build_stack Orange
|
|
32
30
|
assert_handlers %w[Orange]
|
|
33
31
|
end
|
|
@@ -67,6 +65,35 @@ class MiddlewareStackTest < Faraday::TestCase
|
|
|
67
65
|
assert_handlers %w[Orange]
|
|
68
66
|
end
|
|
69
67
|
|
|
68
|
+
def test_stack_is_locked_after_making_requests
|
|
69
|
+
build_stack Apple
|
|
70
|
+
assert !@builder.locked?
|
|
71
|
+
@conn.get('/')
|
|
72
|
+
assert @builder.locked?
|
|
73
|
+
|
|
74
|
+
assert_raises Faraday::Builder::StackLocked do
|
|
75
|
+
@conn.use Orange
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_duped_stack_is_unlocked
|
|
80
|
+
build_stack Apple
|
|
81
|
+
assert !@builder.locked?
|
|
82
|
+
@builder.lock!
|
|
83
|
+
assert @builder.locked?
|
|
84
|
+
|
|
85
|
+
duped_connection = @conn.dup
|
|
86
|
+
assert_equal @builder, duped_connection.builder
|
|
87
|
+
assert !duped_connection.builder.locked?
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_handler_comparison
|
|
91
|
+
build_stack Apple
|
|
92
|
+
assert_equal @builder.handlers.first, Apple
|
|
93
|
+
assert_equal @builder.handlers[0,1], [Apple]
|
|
94
|
+
assert_equal @builder.handlers.first, Faraday::Builder::Handler.new(Apple)
|
|
95
|
+
end
|
|
96
|
+
|
|
70
97
|
private
|
|
71
98
|
|
|
72
99
|
# make a stack with test adapter that reflects the order of middleware
|