allow Utils.default_uri_parser to take a URI object that responds to #parse

This commit is contained in:
risk danger olson 2013-05-08 09:01:07 -06:00
parent c24a02c73b
commit f94f30f1bf
3 changed files with 52 additions and 5 deletions

View File

@ -1,4 +1,3 @@
require 'uri'
require 'thread'
Faraday.require_libs 'parameters'
@ -187,11 +186,8 @@ module Faraday
class << self
attr_writer :default_params_encoder
attr_accessor :default_uri_parser
end
self.default_uri_parser = Kernel.method(:URI)
# Stolen from Rack
def normalize_params(params, name, v = nil)
name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
@ -244,6 +240,21 @@ module Faraday
end
end
def default_uri_parser
@default_uri_parser ||= begin
require 'uri'
Kernel.method(:URI)
end
end
def default_uri_parser=(parser)
@default_uri_parser = if parser.respond_to?(:call) || parser.nil?
parser
else
parser.method(:parse)
end
end
# Receives a String or URI and returns just the path with the query string sorted.
def normalize_path(url)
url = URI(url)

View File

@ -1,5 +1,4 @@
require File.expand_path('../helper', __FILE__)
require 'uri'
class TestConnection < Faraday::TestCase

37
test/utils_test.rb Normal file
View File

@ -0,0 +1,37 @@
require File.expand_path('../helper', __FILE__)
class TestUtils < Faraday::TestCase
def setup
@url = "http://example.com/abc"
end
def teardown
Faraday::Utils.default_uri_parser = nil
end
def test_parses_with_default
assert_equal %(#<Method: Kernel.URI>), Faraday::Utils.default_uri_parser.to_s
uri = normalize(@url)
assert_equal 'example.com', uri.host
end
def test_parses_with_URI
Faraday::Utils.default_uri_parser = ::URI
assert_equal %(#<Method: URI.parse>), Faraday::Utils.default_uri_parser.to_s
uri = normalize(@url)
assert_equal 'example.com', uri.host
end
def test_parses_with_block
Faraday::Utils.default_uri_parser = lambda do |uri|
"booya#{"!" * uri.size}"
end
assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
end
def normalize(url)
Faraday::Utils::URI(url)
end
end