shin-faraday 0.4.7

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.
@@ -0,0 +1,26 @@
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::Connection.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
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
2
+
3
+ if Faraday::Adapter::Typhoeus.loaded?
4
+ module Adapters
5
+ class TestTyphoeus < Faraday::TestCase
6
+ def setup
7
+ @adapter = Faraday::Adapter::Typhoeus.new
8
+ end
9
+
10
+ def test_parse_response_headers_leaves_http_status_line_out
11
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
12
+ assert_equal %w(content-type), headers.keys
13
+ end
14
+
15
+ def test_parse_response_headers_parses_lower_cased_header_name_and_value
16
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
17
+ assert_equal 'text/html', headers['content-type']
18
+ end
19
+
20
+ def test_parse_response_headers_parses_lower_cased_header_name_and_value_with_colon
21
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n")
22
+ assert_equal 'http://sushi.com/', headers['location']
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestConnectionApps < Faraday::TestCase
4
+ class TestAdapter
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ [200, {}, env[:test]]
11
+ end
12
+ end
13
+
14
+ class TestMiddleWare
15
+ def initialize(app)
16
+ @app = app
17
+ end
18
+
19
+ def call(env)
20
+ env[:test] = 'hi'
21
+ @app.call(env)
22
+ end
23
+ end
24
+
25
+ def setup
26
+ @conn = Faraday::Connection.new do |b|
27
+ b.use TestMiddleWare
28
+ b.use TestAdapter
29
+ end
30
+ end
31
+
32
+ def test_builder_is_built_from_faraday_connection
33
+ assert_kind_of Faraday::Builder, @conn.builder
34
+ assert_equal 3, @conn.builder.handlers.size
35
+ end
36
+
37
+ def test_builder_adds_middleware_to_builder_stack
38
+ assert_kind_of TestMiddleWare, @conn.builder[0].call(nil)
39
+ assert_kind_of TestAdapter, @conn.builder[1].call(nil)
40
+ end
41
+
42
+ def test_to_app_returns_rack_object
43
+ assert @conn.to_app.respond_to?(:call)
44
+ end
45
+
46
+ def test_builder_is_passed_to_new_faraday_connection
47
+ new_conn = Faraday::Connection.new :builder => @conn.builder
48
+ assert_equal @conn.builder, new_conn.builder
49
+ end
50
+
51
+ def test_builder_is_built_on_new_faraday_connection
52
+ new_conn = Faraday::Connection.new
53
+ new_conn.build do |b|
54
+ b.run @conn.builder[0]
55
+ b.run @conn.builder[1]
56
+ end
57
+ assert_kind_of TestMiddleWare, new_conn.builder[0].call(nil)
58
+ assert_kind_of TestAdapter, new_conn.builder[1].call(nil)
59
+ end
60
+ end
@@ -0,0 +1,284 @@
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 1, conn.params['a']
42
+ end
43
+
44
+ def test_initialize_stores_default_params_from_uri
45
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1", :params => {'b' => '2'}
46
+ assert_equal '1', conn.params['a']
47
+ assert_equal '2', conn.params['b']
48
+ end
49
+
50
+ def test_initialize_stores_default_headers_from_options
51
+ conn = Faraday::Connection.new :headers => {:a => 1}
52
+ assert_equal '1', conn.headers['A']
53
+ end
54
+
55
+ def test_basic_auth_sets_authorization_header
56
+ conn = Faraday::Connection.new
57
+ conn.basic_auth 'Aladdin', 'open sesame'
58
+ assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
59
+ end
60
+
61
+ def test_auto_parses_basic_auth_from_url
62
+ conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
63
+ assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
64
+ end
65
+
66
+ def test_token_auth_sets_authorization_header
67
+ conn = Faraday::Connection.new
68
+ conn.token_auth 'abcdef'
69
+ assert_equal 'Token token="abcdef"', conn.headers['Authorization']
70
+ end
71
+
72
+ def test_token_auth_with_options_sets_authorization_header
73
+ conn = Faraday::Connection.new
74
+ conn.token_auth 'abcdef', :nonce => 'abc'
75
+ assert_equal 'Token token="abcdef",
76
+ nonce="abc"', conn.headers['Authorization']
77
+ end
78
+
79
+ def test_build_url_uses_connection_host_as_default_uri_host
80
+ conn = Faraday::Connection.new
81
+ conn.host = 'sushi.com'
82
+ uri = conn.build_url("/sake.html")
83
+ assert_equal 'sushi.com', uri.host
84
+ end
85
+
86
+ def test_build_url_uses_connection_port_as_default_uri_port
87
+ conn = Faraday::Connection.new
88
+ conn.port = 23
89
+ uri = conn.build_url("http://sushi.com")
90
+ assert_equal 23, uri.port
91
+ end
92
+
93
+ def test_build_url_uses_connection_scheme_as_default_uri_scheme
94
+ conn = Faraday::Connection.new 'http://sushi.com'
95
+ uri = conn.build_url("/sake.html")
96
+ assert_equal 'http', uri.scheme
97
+ end
98
+
99
+ def test_build_url_uses_connection_path_prefix_to_customize_path
100
+ conn = Faraday::Connection.new
101
+ conn.path_prefix = '/fish'
102
+ uri = conn.build_url("sake.html")
103
+ assert_equal '/fish/sake.html', uri.path
104
+ end
105
+
106
+ def test_build_url_uses_root_connection_path_prefix_to_customize_path
107
+ conn = Faraday::Connection.new
108
+ conn.path_prefix = '/'
109
+ uri = conn.build_url("sake.html")
110
+ assert_equal '/sake.html', uri.path
111
+ end
112
+
113
+ def test_build_url_forces_connection_path_prefix_to_be_absolute
114
+ conn = Faraday::Connection.new
115
+ conn.path_prefix = 'fish'
116
+ uri = conn.build_url("sake.html")
117
+ assert_equal '/fish/sake.html', uri.path
118
+ end
119
+
120
+ def test_build_url_ignores_connection_path_prefix_trailing_slash
121
+ conn = Faraday::Connection.new
122
+ conn.path_prefix = '/fish/'
123
+ uri = conn.build_url("sake.html")
124
+ assert_equal '/fish/sake.html', uri.path
125
+ end
126
+
127
+ def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
128
+ conn = Faraday::Connection.new
129
+ conn.path_prefix = '/fish'
130
+ uri = conn.build_url("/sake.html")
131
+ assert_equal '/sake.html', uri.path
132
+ end
133
+
134
+ def test_build_url_parses_url_params_into_path
135
+ conn = Faraday::Connection.new
136
+ uri = conn.build_url("http://sushi.com/sake.html")
137
+ assert_equal '/sake.html', uri.path
138
+ end
139
+
140
+ def test_build_url_parses_url_params_into_query
141
+ conn = Faraday::Connection.new
142
+ uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
143
+ assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
144
+ end
145
+
146
+ def test_build_url_mashes_default_and_given_params_together
147
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
148
+ url = conn.build_url("nigiri?page=1", :limit => 5)
149
+ assert_match /limit=5/, url.query
150
+ assert_match /page=1/, url.query
151
+ assert_match /format=json/, url.query
152
+ assert_match /token=abc/, url.query
153
+ end
154
+
155
+ def test_build_url_overrides_default_params_with_given_params
156
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
157
+ url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
158
+ assert_match /limit=5/, url.query
159
+ assert_match /page=1/, url.query
160
+ assert_match /format=xml/, url.query
161
+ assert_match /token=def/, url.query
162
+ assert_no_match /format=json/, url.query
163
+ assert_no_match /token=abc/, url.query
164
+ end
165
+
166
+ def test_build_url_parses_url
167
+ conn = Faraday::Connection.new
168
+ uri = conn.build_url("http://sushi.com/sake.html")
169
+ assert_equal "http", uri.scheme
170
+ assert_equal "sushi.com", uri.host
171
+ assert_equal '/sake.html', uri.path
172
+ assert_nil uri.port
173
+ end
174
+
175
+ def test_build_url_parses_url_and_changes_scheme
176
+ conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
177
+ conn.scheme = 'https'
178
+ uri = conn.build_url("sake.html")
179
+ assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
180
+ end
181
+
182
+ def test_proxy_accepts_string
183
+ conn = Faraday::Connection.new
184
+ conn.proxy 'http://proxy.com'
185
+ assert_equal 'proxy.com', conn.proxy.host
186
+ end
187
+
188
+ def test_proxy_accepts_string
189
+ conn = Faraday::Connection.new
190
+ conn.proxy 'http://proxy.com'
191
+ assert_equal 'proxy.com', conn.proxy[:uri].host
192
+ assert_equal [:uri], conn.proxy.keys
193
+ end
194
+
195
+ def test_proxy_accepts_uri
196
+ conn = Faraday::Connection.new
197
+ conn.proxy Addressable::URI.parse('http://proxy.com')
198
+ assert_equal 'proxy.com', conn.proxy[:uri].host
199
+ assert_equal [:uri], conn.proxy.keys
200
+ end
201
+
202
+ def test_proxy_accepts_hash_with_string_uri
203
+ conn = Faraday::Connection.new
204
+ conn.proxy :uri => 'http://proxy.com', :user => 'rick'
205
+ assert_equal 'proxy.com', conn.proxy[:uri].host
206
+ assert_equal 'rick', conn.proxy[:user]
207
+ end
208
+
209
+ def test_proxy_accepts_hash
210
+ conn = Faraday::Connection.new
211
+ conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
212
+ assert_equal 'proxy.com', conn.proxy[:uri].host
213
+ assert_equal 'rick', conn.proxy[:user]
214
+ end
215
+
216
+ def test_proxy_requires_uri
217
+ conn = Faraday::Connection.new
218
+ assert_raises ArgumentError do
219
+ conn.proxy :uri => :bad_uri, :user => 'rick'
220
+ end
221
+ end
222
+
223
+ def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
224
+ conn = Faraday::Connection.new
225
+ class << conn
226
+ public :build_query
227
+ end
228
+ assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
229
+ end
230
+
231
+ def test_dups_connection_object
232
+ conn = Faraday::Connection.new 'http://sushi.com/foo' do |b|
233
+ b.adapter :net_http
234
+ end
235
+ conn.headers['content-type'] = 'text/plain'
236
+ conn.params['a'] = '1'
237
+
238
+ duped = conn.dup
239
+ assert_equal conn.build_url(''), duped.build_url('')
240
+ [:headers, :params, :builder].each do |attr|
241
+ assert_equal conn.send(attr), duped.send(attr)
242
+ assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
243
+ end
244
+ end
245
+
246
+ def test_allows_rebuilding_of_connection_handlers
247
+ conn = Faraday::Connection.new
248
+ conn.to_app
249
+ inner = conn.builder.handlers[0]
250
+ mware = conn.builder.handlers[1].call({})
251
+ assert_kind_of Faraday::Adapter::NetHttp, mware
252
+
253
+ conn.build do |b|
254
+ b.adapter :test
255
+ end
256
+ mware = conn.builder.handlers[1].call({})
257
+ assert_kind_of Faraday::Adapter::Test, mware
258
+ assert_equal inner, conn.builder.handlers[0]
259
+ end
260
+
261
+ def test_allows_extending_of_existing_connection_handlers
262
+ conn = Faraday::Connection.new
263
+ conn.to_app
264
+ mware = conn.builder.handlers[1].call({})
265
+ assert_kind_of Faraday::Adapter::NetHttp, mware
266
+ assert_equal 2, conn.builder.handlers.size
267
+
268
+ conn.build :keep => true do |b|
269
+ b.adapter :test
270
+ end
271
+ mware = conn.builder.handlers[1].call({})
272
+ assert_kind_of Faraday::Adapter::Test, mware
273
+ assert_equal 3, conn.builder.handlers.size
274
+ end
275
+
276
+ def test_sets_default_adapter_if_none_set
277
+ conn = Faraday::Connection.new
278
+ assert_equal 0, conn.builder.handlers.size
279
+
280
+ app = conn.to_app
281
+ mware = conn.builder.handlers[1].call({})
282
+ assert_kind_of Faraday::Adapter::NetHttp, mware
283
+ end
284
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestEnv < Faraday::TestCase
4
+ def setup
5
+ @conn = Faraday::Connection.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
6
+ @conn.options[:timeout] = 3
7
+ @conn.options[:open_timeout] = 5
8
+ @conn.ssl[:verify] = false
9
+ @conn.proxy 'http://proxy.com'
10
+ @input = {
11
+ :body => 'abc',
12
+ :headers => {'Server' => 'Faraday'}}
13
+ @env = env_for @conn do |req|
14
+ req.url 'foo.json', 'a' => 1
15
+ req['Server'] = 'Faraday'
16
+ req.body = @input[:body]
17
+ end
18
+ end
19
+
20
+ def test_request_create_stores_method
21
+ assert_equal :get, @env[:method]
22
+ end
23
+
24
+ def test_request_create_stores_addressable_uri
25
+ assert_equal 'http://sushi.com/api/foo.json?a=1', @env[:url].to_s
26
+ end
27
+
28
+ def test_request_create_stores_headers
29
+ assert_kind_of Rack::Utils::HeaderHash, @env[:request_headers]
30
+ assert_equal @input[:headers].merge('Mime-Version' => '1.0'), @env[:request_headers]
31
+ end
32
+
33
+ def test_request_create_stores_body
34
+ assert_equal @input[:body], @env[:body]
35
+ end
36
+
37
+ def test_request_create_stores_ssl_options
38
+ assert_equal 3, @env[:request][:timeout]
39
+ assert_equal 5, @env[:request][:open_timeout]
40
+ end
41
+
42
+ def test_request_create_stores_ssl_options
43
+ assert_equal false, @env[:ssl][:verify]
44
+ end
45
+
46
+ def test_request_create_stores_proxy_options
47
+ assert_equal 'proxy.com', @env[:request][:proxy][:uri].host
48
+ end
49
+
50
+ def env_for(connection)
51
+ env_setup = Faraday::Request.create do |req|
52
+ yield req
53
+ end
54
+ env_setup.to_env_hash(connection, :get)
55
+ end
56
+ end