faraday 0.9.0.rc1 → 0.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Faraday Changelog
2
+
3
+ ## v0.9.0.pre
4
+
5
+ * Add HTTPClient adapter (@hakanensari)
6
+ * Improve Retry handler (@mislav)
7
+ * Remove autoloading by default (@technoweenie)
8
+ * Improve internal docs (@technoweenie, @mislav)
9
+ * Respect user/password in http proxy string (@mislav)
10
+ * Adapter options are structs. Reinforces consistent options across adapters
11
+ (@technoweenie)
12
+
data/faraday.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency 'multipart-post', '~> 1.1'
21
21
  spec.add_development_dependency 'bundler', '~> 1.0'
22
22
 
23
- spec.files = %w(.document CONTRIBUTING.md Gemfile LICENSE.md README.md Rakefile)
23
+ spec.files = %w(.document CHANGELOG.md CONTRIBUTING.md Gemfile LICENSE.md README.md Rakefile)
24
24
  spec.files << "#{lib}.gemspec"
25
25
  spec.files += Dir.glob("lib/**/*.rb")
26
26
  spec.files += Dir.glob("test/**/*.rb")
data/lib/faraday.rb CHANGED
@@ -14,7 +14,7 @@ require 'forwardable'
14
14
  # conn.get '/'
15
15
  #
16
16
  module Faraday
17
- VERSION = "0.9.0.rc1"
17
+ VERSION = "0.9.0.rc2"
18
18
 
19
19
  class << self
20
20
  # Public: Gets or sets the root path that Faraday is being loaded from.
@@ -19,7 +19,7 @@ module Faraday
19
19
 
20
20
  def call(env)
21
21
  super
22
- request = EventMachine::HttpRequest.new(URI::parse(env[:url].to_s), connection_config(env))
22
+ request = EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env))
23
23
 
24
24
  http_method = env[:method].to_s.downcase.to_sym
25
25
 
@@ -322,7 +322,6 @@ module Faraday
322
322
  # Returns the new String path prefix.
323
323
  def path_prefix=(value)
324
324
  url_prefix.path = if value
325
- value = value.chomp '/'
326
325
  value = '/' + value unless value[0,1] == '/'
327
326
  value
328
327
  end
@@ -363,27 +362,6 @@ module Faraday
363
362
  end
364
363
  end
365
364
 
366
- # Takes a relative url for a request and combines it with the defaults
367
- # set on the connection instance.
368
- #
369
- # conn = Faraday::Connection.new { ... }
370
- # conn.url_prefix = "https://sushi.com/api?token=abc"
371
- # conn.scheme # => https
372
- # conn.path_prefix # => "/api"
373
- #
374
- # conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
375
- # conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
376
- #
377
- def build_url(url, extra_params = nil)
378
- uri = build_exclusive_url(url)
379
-
380
- query_values = self.params.dup.merge_query(uri.query, options.params_encoder)
381
- query_values.update extra_params if extra_params
382
- uri.query = query_values.empty? ? nil : query_values.to_query(options.params_encoder)
383
-
384
- uri
385
- end
386
-
387
365
  # Internal: Build an absolute URL based on url_prefix.
388
366
  #
389
367
  # url - A String or URI-like object
@@ -391,7 +369,7 @@ module Faraday
391
369
  # of the resulting url (default: nil).
392
370
  #
393
371
  # Returns the resulting URI instance.
394
- def build_exclusive_url(url, params = nil)
372
+ def build_exclusive_url(url = nil, params = nil)
395
373
  url = nil if url.respond_to?(:empty?) and url.empty?
396
374
  base = url_prefix
397
375
  if url and base.path and base.path !~ /\/$/
@@ -408,7 +386,7 @@ module Faraday
408
386
  #
409
387
  # Returns a Faraday::Connection.
410
388
  def dup
411
- self.class.new(build_url(''), :headers => headers.dup, :params => params.dup, :builder => builder.dup, :ssl => ssl.dup)
389
+ self.class.new(build_exclusive_url, :headers => headers.dup, :params => params.dup, :builder => builder.dup, :ssl => ssl.dup)
412
390
  end
413
391
 
414
392
  # Internal: Yields username and password extracted from a URI if they both exist.
data/lib/faraday/utils.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'uri'
2
1
  require 'thread'
3
2
  Faraday.require_libs 'parameters'
4
3
 
@@ -235,12 +234,27 @@ module Faraday
235
234
  if url.respond_to?(:host)
236
235
  url
237
236
  elsif url.respond_to?(:to_str)
238
- Kernel.URI(url)
237
+ default_uri_parser.call(url)
239
238
  else
240
239
  raise ArgumentError, "bad argument (expected URI object or URI string)"
241
240
  end
242
241
  end
243
242
 
243
+ def default_uri_parser
244
+ @default_uri_parser ||= begin
245
+ require 'uri'
246
+ Kernel.method(:URI)
247
+ end
248
+ end
249
+
250
+ def default_uri_parser=(parser)
251
+ @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
252
+ parser
253
+ else
254
+ parser.method(:parse)
255
+ end
256
+ end
257
+
244
258
  # Receives a String or URI and returns just the path with the query string sorted.
245
259
  def normalize_path(url)
246
260
  url = URI(url)
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
- require 'uri'
3
2
 
4
3
  class TestConnection < Faraday::TestCase
5
4
 
@@ -105,112 +104,108 @@ class TestConnection < Faraday::TestCase
105
104
  def test_build_url_uses_connection_host_as_default_uri_host
106
105
  conn = Faraday::Connection.new
107
106
  conn.host = 'sushi.com'
108
- uri = conn.build_url("/sake.html")
107
+ uri = conn.build_exclusive_url("/sake.html")
109
108
  assert_equal 'sushi.com', uri.host
110
109
  end
111
110
 
112
111
  def test_build_url_overrides_connection_port_for_absolute_urls
113
112
  conn = Faraday::Connection.new
114
113
  conn.port = 23
115
- uri = conn.build_url("http://sushi.com")
114
+ uri = conn.build_exclusive_url("http://sushi.com")
116
115
  assert_equal 80, uri.port
117
116
  end
118
117
 
119
118
  def test_build_url_uses_connection_scheme_as_default_uri_scheme
120
119
  conn = Faraday::Connection.new 'http://sushi.com'
121
- uri = conn.build_url("/sake.html")
120
+ uri = conn.build_exclusive_url("/sake.html")
122
121
  assert_equal 'http', uri.scheme
123
122
  end
124
123
 
125
124
  def test_build_url_uses_connection_path_prefix_to_customize_path
126
125
  conn = Faraday::Connection.new
127
126
  conn.path_prefix = '/fish'
128
- uri = conn.build_url("sake.html")
127
+ uri = conn.build_exclusive_url("sake.html")
129
128
  assert_equal '/fish/sake.html', uri.path
130
129
  end
131
130
 
132
131
  def test_build_url_uses_root_connection_path_prefix_to_customize_path
133
132
  conn = Faraday::Connection.new
134
133
  conn.path_prefix = '/'
135
- uri = conn.build_url("sake.html")
134
+ uri = conn.build_exclusive_url("sake.html")
136
135
  assert_equal '/sake.html', uri.path
137
136
  end
138
137
 
139
138
  def test_build_url_forces_connection_path_prefix_to_be_absolute
140
139
  conn = Faraday::Connection.new
141
140
  conn.path_prefix = 'fish'
142
- uri = conn.build_url("sake.html")
141
+ uri = conn.build_exclusive_url("sake.html")
143
142
  assert_equal '/fish/sake.html', uri.path
144
143
  end
145
144
 
146
145
  def test_build_url_ignores_connection_path_prefix_trailing_slash
147
146
  conn = Faraday::Connection.new
148
147
  conn.path_prefix = '/fish/'
149
- uri = conn.build_url("sake.html")
148
+ uri = conn.build_exclusive_url("sake.html")
150
149
  assert_equal '/fish/sake.html', uri.path
151
150
  end
152
151
 
153
152
  def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
154
153
  conn = Faraday::Connection.new
155
154
  conn.path_prefix = '/fish'
156
- uri = conn.build_url("/sake.html")
155
+ uri = conn.build_exclusive_url("/sake.html")
157
156
  assert_equal '/sake.html', uri.path
158
157
  end
159
158
 
160
159
  def test_build_url_parses_url_params_into_path
161
160
  conn = Faraday::Connection.new
162
- uri = conn.build_url("http://sushi.com/sake.html")
161
+ uri = conn.build_exclusive_url("http://sushi.com/sake.html")
163
162
  assert_equal '/sake.html', uri.path
164
163
  end
165
164
 
166
165
  def test_build_url_doesnt_add_ending_slash_given_nil_url
167
166
  conn = Faraday::Connection.new
168
167
  conn.url_prefix = "http://sushi.com/nigiri"
169
- uri = conn.build_url(nil)
168
+ uri = conn.build_exclusive_url
170
169
  assert_equal "/nigiri", uri.path
171
170
  end
172
171
 
173
172
  def test_build_url_doesnt_add_ending_slash_given_empty_url
174
173
  conn = Faraday::Connection.new
175
174
  conn.url_prefix = "http://sushi.com/nigiri"
176
- uri = conn.build_url('')
175
+ uri = conn.build_exclusive_url('')
177
176
  assert_equal "/nigiri", uri.path
178
177
  end
179
178
 
180
179
  def test_build_url_parses_url_params_into_query
181
- conn = Faraday::Connection.new
182
- uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
180
+ uri = build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
183
181
  assert_equal "a%5Bb%5D=1+%2B+2", uri.query
184
182
  end
185
183
 
186
184
  def test_build_url_escapes_per_spec
187
- conn = Faraday::Connection.new
188
- uri = conn.build_url('http:/', 'a' => '1+2 foo~bar.-baz')
185
+ uri = build_url('http:/', 'a' => '1+2 foo~bar.-baz')
189
186
  assert_equal "a=1%2B2+foo~bar.-baz", uri.query
190
187
  end
191
188
 
192
189
  def test_build_url_bracketizes_nested_params_in_query
193
- conn = Faraday::Connection.new
194
- uri = conn.build_url("http://sushi.com/sake.html", 'a' => {'b' => 'c'})
195
- assert_equal "a%5Bb%5D=c", uri.query
190
+ url = build_url nil, 'a' => {'b' => 'c'}
191
+ assert_equal "a%5Bb%5D=c", url.query
196
192
  end
197
193
 
198
194
  def test_build_url_bracketizes_repeated_params_in_query
199
- conn = Faraday::Connection.new
200
- uri = conn.build_url("http://sushi.com/sake.html", 'a' => [1, 2])
195
+ uri = build_url("http://sushi.com/sake.html", 'a' => [1, 2])
201
196
  assert_equal "a%5B%5D=1&a%5B%5D=2", uri.query
202
197
  end
203
198
 
204
199
  def test_build_url_without_braketizing_repeated_params_in_query
205
- conn = Faraday::Connection.new
206
- conn.options.params_encoder = Faraday::FlatParamsEncoder
207
- uri = conn.build_url("http://sushi.com/sake.html", 'a' => [1, 2])
200
+ uri = build_url 'http://sushi.com', 'a' => [1, 2] do |conn|
201
+ conn.options.params_encoder = Faraday::FlatParamsEncoder
202
+ end
208
203
  assert_equal "a=1&a=2", uri.query
209
204
  end
210
205
 
211
206
  def test_build_url_parses_url
212
207
  conn = Faraday::Connection.new
213
- uri = conn.build_url("http://sushi.com/sake.html")
208
+ uri = conn.build_exclusive_url("http://sushi.com/sake.html")
214
209
  assert_equal "http", uri.scheme
215
210
  assert_equal "sushi.com", uri.host
216
211
  assert_equal '/sake.html', uri.path
@@ -219,13 +214,31 @@ class TestConnection < Faraday::TestCase
219
214
  def test_build_url_parses_url_and_changes_scheme
220
215
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
221
216
  conn.scheme = 'https'
222
- uri = conn.build_url("sake.html")
217
+ uri = conn.build_exclusive_url("sake.html")
223
218
  assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
224
219
  end
225
220
 
221
+ def test_build_url_joins_url_to_base_with_ending_slash
222
+ conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
223
+ uri = conn.build_exclusive_url("sake.html")
224
+ assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s
225
+ end
226
+
227
+ def test_build_url_used_default_base_with_ending_slash
228
+ conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
229
+ uri = conn.build_exclusive_url
230
+ assert_equal 'http://sushi.com/sushi/', uri.to_s
231
+ end
232
+
233
+ def test_build_url_overrides_base
234
+ conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
235
+ uri = conn.build_exclusive_url('/sake/')
236
+ assert_equal 'http://sushi.com/sake/', uri.to_s
237
+ end
238
+
226
239
  def test_build_url_handles_uri_instances
227
240
  conn = Faraday::Connection.new
228
- uri = conn.build_url(URI('/sake.html'))
241
+ uri = conn.build_exclusive_url(URI('/sake.html'))
229
242
  assert_equal '/sake.html', uri.path
230
243
  end
231
244
 
@@ -286,7 +299,7 @@ class TestConnection < Faraday::TestCase
286
299
 
287
300
  other = conn.dup
288
301
 
289
- assert_equal conn.build_url(''), other.build_url('')
302
+ assert_equal conn.build_exclusive_url, other.build_exclusive_url
290
303
  assert_equal 'text/plain', other.headers['content-type']
291
304
  assert_equal '1', other.params['a']
292
305
 
@@ -314,6 +327,13 @@ class TestConnection < Faraday::TestCase
314
327
  assert_equal 1, conn.builder.handlers.size
315
328
  assert_equal '/omnom', conn.path_prefix
316
329
  end
330
+
331
+ def build_url(url, params)
332
+ conn = Faraday::Connection.new(url, :params => params)
333
+ yield conn if block_given?
334
+ req = conn.build_request(:get)
335
+ req.to_env(conn).url
336
+ end
317
337
  end
318
338
 
319
339
  class TestRequestParams < Faraday::TestCase
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class TestUtils < Faraday::TestCase
4
+ def setup
5
+ @url = "http://example.com/abc"
6
+ end
7
+
8
+ def teardown
9
+ Faraday::Utils.default_uri_parser = nil
10
+ end
11
+
12
+ def test_parses_with_default
13
+ assert_equal %(#<Method: Kernel.URI>), Faraday::Utils.default_uri_parser.to_s
14
+ uri = normalize(@url)
15
+ assert_equal 'example.com', uri.host
16
+ end
17
+
18
+ def test_parses_with_URI
19
+ Faraday::Utils.default_uri_parser = ::URI
20
+ assert_equal %(#<Method: URI.parse>), Faraday::Utils.default_uri_parser.to_s
21
+ uri = normalize(@url)
22
+ assert_equal 'example.com', uri.host
23
+ end
24
+
25
+ def test_parses_with_block
26
+ Faraday::Utils.default_uri_parser = lambda do |uri|
27
+ "booya#{"!" * uri.size}"
28
+ end
29
+
30
+ assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
31
+ end
32
+
33
+ def normalize(url)
34
+ Faraday::Utils::URI(url)
35
+ end
36
+ end
37
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.rc1
4
+ version: 0.9.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-08 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multipart-post
@@ -50,6 +50,7 @@ extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
52
  - .document
53
+ - CHANGELOG.md
53
54
  - CONTRIBUTING.md
54
55
  - Gemfile
55
56
  - LICENSE.md
@@ -115,6 +116,7 @@ files:
115
116
  - test/request_middleware_test.rb
116
117
  - test/response_middleware_test.rb
117
118
  - test/strawberry.rb
119
+ - test/utils_test.rb
118
120
  - script/console
119
121
  - script/generate_certs
120
122
  - script/package
@@ -173,3 +175,4 @@ test_files:
173
175
  - test/request_middleware_test.rb
174
176
  - test/response_middleware_test.rb
175
177
  - test/strawberry.rb
178
+ - test/utils_test.rb