mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-29 00:03:58 -04:00
remove dependency on Addressable::URI
This is a potentially breaking change for 3rd party code that relies on env[:url] being specifically Addressable. After this change all urls are passed around as instances of URI::HTTP.
This commit is contained in:
parent
125c50fded
commit
8f7f6694da
@ -32,7 +32,6 @@ Gem::Specification.new do |s|
|
||||
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
||||
s.require_paths = %w[lib]
|
||||
|
||||
s.add_dependency 'addressable', '~> 2.2'
|
||||
s.add_dependency 'multipart-post', '~> 1.1'
|
||||
s.add_development_dependency 'rake'
|
||||
s.add_development_dependency 'test-unit'
|
||||
|
@ -1,3 +1,4 @@
|
||||
require 'uri'
|
||||
require 'faraday/adapter/em_synchrony/parallel_manager'
|
||||
|
||||
module Faraday
|
||||
@ -30,10 +31,10 @@ module Faraday
|
||||
|
||||
if req = env[:request]
|
||||
if proxy = req[:proxy]
|
||||
uri = Addressable::URI.parse(proxy[:uri])
|
||||
uri = URI.parse(proxy[:uri])
|
||||
options[:proxy] = {
|
||||
:host => uri.host,
|
||||
:port => uri.inferred_port
|
||||
:port => uri.port
|
||||
}
|
||||
if proxy[:username] && proxy[:password]
|
||||
options[:proxy][:authorization] = [proxy[:username], proxy[:password]]
|
||||
|
@ -13,7 +13,7 @@ module Faraday
|
||||
url = env[:url]
|
||||
req = env[:request]
|
||||
|
||||
http = net_http_class(env).new(url.host, url.inferred_port)
|
||||
http = net_http_class(env).new(url.host, url.port)
|
||||
|
||||
if http.use_ssl = (url.scheme == 'https' && (ssl = env[:ssl]) && true)
|
||||
http.verify_mode = ssl[:verify_mode] || begin
|
||||
|
@ -34,6 +34,7 @@ module Faraday
|
||||
return false if !@stack.key?(request_method)
|
||||
stack = @stack[request_method]
|
||||
consumed = (@consumed[request_method] ||= [])
|
||||
path = normalize_path(path)
|
||||
|
||||
if stub = matches?(stack, path, body)
|
||||
consumed << stack.delete(stub)
|
||||
@ -87,12 +88,20 @@ module Faraday
|
||||
protected
|
||||
|
||||
def new_stub(request_method, path, body=nil, &block)
|
||||
(@stack[request_method] ||= []) << Stub.new(path, body, block)
|
||||
(@stack[request_method] ||= []) << Stub.new(normalize_path(path), body, block)
|
||||
end
|
||||
|
||||
def matches?(stack, path, body)
|
||||
stack.detect { |stub| stub.matches?(path, body) }
|
||||
end
|
||||
|
||||
# ensure leading + trailing slash
|
||||
def normalize_path(path)
|
||||
path = '/' + path if path.index('/') != 0
|
||||
path = path.sub('?', '/?')
|
||||
path = path + '/' unless $&
|
||||
path.gsub('//', '/')
|
||||
end
|
||||
end
|
||||
|
||||
class Stub < Struct.new(:path, :params, :body, :block)
|
||||
|
@ -1,20 +1,18 @@
|
||||
require 'addressable/uri'
|
||||
require 'base64'
|
||||
require 'cgi'
|
||||
require 'set'
|
||||
require 'forwardable'
|
||||
require 'uri'
|
||||
|
||||
Faraday.require_libs 'builder', 'request', 'response', 'utils'
|
||||
|
||||
module Faraday
|
||||
class Connection
|
||||
include Addressable
|
||||
|
||||
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
|
||||
METHODS_WITH_BODIES = Set.new [:post, :put, :patch, :options]
|
||||
|
||||
attr_accessor :host, :port, :scheme, :params, :headers, :parallel_manager
|
||||
attr_reader :path_prefix, :builder, :options, :ssl
|
||||
attr_accessor :params, :headers, :parallel_manager
|
||||
attr_reader :url_prefix, :builder, :options, :ssl
|
||||
|
||||
# :url
|
||||
# :params
|
||||
@ -43,8 +41,7 @@ module Faraday
|
||||
Builder.new(&block)
|
||||
end
|
||||
|
||||
self.url_prefix = url if url
|
||||
proxy(options[:proxy])
|
||||
self.url_prefix = url || 'http:/'
|
||||
|
||||
@params.update options[:params] if options[:params]
|
||||
@headers.update options[:headers] if options[:headers]
|
||||
@ -127,20 +124,25 @@ module Faraday
|
||||
def proxy(arg = nil)
|
||||
return @proxy if arg.nil?
|
||||
|
||||
@proxy =
|
||||
case arg
|
||||
when String then {:uri => proxy_arg_to_uri(arg)}
|
||||
when URI then {:uri => arg}
|
||||
when Hash
|
||||
if arg[:uri] = proxy_arg_to_uri(arg[:uri])
|
||||
arg
|
||||
else
|
||||
raise ArgumentError, "no :uri option."
|
||||
end
|
||||
end
|
||||
@proxy = if arg.is_a? Hash
|
||||
uri = arg.fetch(:uri) { raise ArgumentError, "no :uri option" }
|
||||
arg.merge :uri => self.class.URI(uri)
|
||||
else
|
||||
{:uri => self.class.URI(arg)}
|
||||
end
|
||||
end
|
||||
|
||||
# Parses the giving url with Addressable::URI and stores the individual
|
||||
# normalize URI() behavior across Ruby versions
|
||||
def self.URI url
|
||||
url.respond_to?(:host) ? url :
|
||||
url.respond_to?(:to_str) ? Kernel.URI(url) :
|
||||
raise(ArgumentError, "bad argument (expected URI object or URI string)")
|
||||
end
|
||||
|
||||
def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
|
||||
def_delegator :url_prefix, :path, :path_prefix
|
||||
|
||||
# Parses the giving url with URI and stores the individual
|
||||
# components in this connection. These components serve as defaults for
|
||||
# requests made by this connection.
|
||||
#
|
||||
@ -152,27 +154,27 @@ module Faraday
|
||||
# conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
|
||||
#
|
||||
def url_prefix=(url)
|
||||
uri = URI.parse(url)
|
||||
self.scheme = uri.scheme
|
||||
self.host = uri.host
|
||||
self.port = uri.port
|
||||
uri = @url_prefix = self.class.URI(url)
|
||||
self.path_prefix = uri.path
|
||||
|
||||
@params.merge_query(uri.query)
|
||||
uri.query = nil
|
||||
|
||||
if uri.user && uri.password
|
||||
basic_auth(CGI.unescape(uri.user), CGI.unescape(uri.password))
|
||||
uri.user = uri.password = nil
|
||||
end
|
||||
|
||||
uri
|
||||
end
|
||||
|
||||
# Ensures that the path prefix always has a leading / and no trailing /
|
||||
# Ensures that the path prefix always has a leading but no trailing slash
|
||||
def path_prefix=(value)
|
||||
if value
|
||||
value.chomp! "/"
|
||||
value.replace "/#{value}" if value !~ /^\//
|
||||
url_prefix.path = if value
|
||||
value = value.chomp '/'
|
||||
value = '/' + value unless value[0,1] == '/'
|
||||
value
|
||||
end
|
||||
@path_prefix = value
|
||||
end
|
||||
|
||||
def run_request(method, url, body, headers)
|
||||
@ -203,15 +205,13 @@ module Faraday
|
||||
# conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
|
||||
#
|
||||
def build_url(url, extra_params = nil)
|
||||
uri = URI.parse(url.to_s)
|
||||
if @path_prefix && uri.path !~ /^\//
|
||||
new_path = @path_prefix.size > 1 ? @path_prefix.dup : ''
|
||||
new_path << "/#{uri.path}" unless uri.path.empty?
|
||||
uri.path = new_path
|
||||
url = nil if url and url.empty?
|
||||
base = url_prefix
|
||||
if url and base.path and base.path !~ /\/$/
|
||||
base = base.dup
|
||||
base.path = base.path + '/' # ensure trailing slash
|
||||
end
|
||||
uri.host ||= @host
|
||||
uri.port ||= @port
|
||||
uri.scheme ||= @scheme
|
||||
uri = url ? base + url : base
|
||||
|
||||
params = @params.dup.merge_query(uri.query)
|
||||
params.update extra_params if extra_params
|
||||
@ -223,12 +223,5 @@ module Faraday
|
||||
def dup
|
||||
self.class.new(build_url(''), :headers => headers.dup, :params => params.dup, :builder => builder.dup, :ssl => ssl.dup)
|
||||
end
|
||||
|
||||
def proxy_arg_to_uri(arg)
|
||||
case arg
|
||||
when String then URI.parse(arg)
|
||||
when URI then arg
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -58,7 +58,7 @@ module Faraday
|
||||
# ENV Keys
|
||||
# :method - a symbolized request method (:get, :post)
|
||||
# :body - the request body that will eventually be converted to a string.
|
||||
# :url - Addressable::URI instance of the URI for the current request.
|
||||
# :url - URI instance for the current request.
|
||||
# :status - HTTP response status code
|
||||
# :request_headers - hash of HTTP Headers to be sent to the server
|
||||
# :response_headers - Hash of HTTP headers from the server
|
||||
|
@ -23,7 +23,7 @@ module Adapters
|
||||
end
|
||||
|
||||
def test_logs_method_and_url
|
||||
assert_match "get /hello", @io.string
|
||||
assert_match "get http:/hello", @io.string
|
||||
end
|
||||
|
||||
def test_logs_request_headers
|
||||
|
@ -1,4 +1,5 @@
|
||||
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
||||
require 'uri'
|
||||
|
||||
class TestConnection < Faraday::TestCase
|
||||
|
||||
@ -17,9 +18,9 @@ class TestConnection < Faraday::TestCase
|
||||
assert_equal 'sushi.com', conn.host
|
||||
end
|
||||
|
||||
def test_initialize_parses_nil_port_out_of_given_url
|
||||
def test_initialize_inherits_default_port_out_of_given_url
|
||||
conn = Faraday::Connection.new "http://sushi.com"
|
||||
assert_nil conn.port
|
||||
assert_equal 80, conn.port
|
||||
end
|
||||
|
||||
def test_initialize_parses_scheme_out_of_given_url
|
||||
@ -94,11 +95,11 @@ class TestConnection < Faraday::TestCase
|
||||
assert_equal 'sushi.com', uri.host
|
||||
end
|
||||
|
||||
def test_build_url_uses_connection_port_as_default_uri_port
|
||||
def test_build_url_overrides_connection_port_for_absolute_urls
|
||||
conn = Faraday::Connection.new
|
||||
conn.port = 23
|
||||
uri = conn.build_url("http://sushi.com")
|
||||
assert_equal 23, uri.port
|
||||
assert_equal 80, uri.port
|
||||
end
|
||||
|
||||
def test_build_url_uses_connection_scheme_as_default_uri_scheme
|
||||
@ -184,10 +185,9 @@ class TestConnection < Faraday::TestCase
|
||||
def test_build_url_parses_url
|
||||
conn = Faraday::Connection.new
|
||||
uri = conn.build_url("http://sushi.com/sake.html")
|
||||
assert_equal "http", uri.scheme
|
||||
assert_equal "sushi.com", uri.host
|
||||
assert_equal "http", uri.scheme
|
||||
assert_equal "sushi.com", uri.host
|
||||
assert_equal '/sake.html', uri.path
|
||||
assert_nil uri.port
|
||||
end
|
||||
|
||||
def test_build_url_parses_url_and_changes_scheme
|
||||
@ -209,7 +209,7 @@ class TestConnection < Faraday::TestCase
|
||||
def test_proxy_accepts_uri
|
||||
with_proxy_env "http://duncan.proxy.com:80" do
|
||||
conn = Faraday::Connection.new
|
||||
conn.proxy Addressable::URI.parse('http://proxy.com')
|
||||
conn.proxy URI.parse('http://proxy.com')
|
||||
assert_equal 'proxy.com', conn.proxy[:uri].host
|
||||
assert_equal [:uri], conn.proxy.keys
|
||||
end
|
||||
@ -227,7 +227,7 @@ class TestConnection < Faraday::TestCase
|
||||
def test_proxy_accepts_hash
|
||||
with_proxy_env "http://duncan.proxy.com:80" do
|
||||
conn = Faraday::Connection.new
|
||||
conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
|
||||
conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
|
||||
assert_equal 'proxy.com', conn.proxy[:uri].host
|
||||
assert_equal 'rick', conn.proxy[:user]
|
||||
end
|
||||
|
@ -17,7 +17,7 @@ class EnvTest < Faraday::TestCase
|
||||
assert_equal :get, env[:method]
|
||||
end
|
||||
|
||||
def test_request_create_stores_addressable_uri
|
||||
def test_request_create_stores_uri
|
||||
env = make_env do |req|
|
||||
req.url 'foo.json', 'a' => 1
|
||||
end
|
||||
|
@ -51,8 +51,8 @@ class ResponseNoBodyMiddleWareTest < Faraday::TestCase
|
||||
@conn = Faraday.new do |b|
|
||||
b.response :raise_error
|
||||
b.adapter :test do |stub|
|
||||
stub.get('not modified') { [304, nil, nil] }
|
||||
stub.get('no content') { [204, nil, nil] }
|
||||
stub.get('not_modified') { [304, nil, nil] }
|
||||
stub.get('no_content') { [204, nil, nil] }
|
||||
end
|
||||
end
|
||||
@conn.builder.insert(0, NotCalled)
|
||||
@ -65,10 +65,10 @@ class ResponseNoBodyMiddleWareTest < Faraday::TestCase
|
||||
end
|
||||
|
||||
def test_204
|
||||
assert_equal nil, @conn.get('no content').body
|
||||
assert_equal nil, @conn.get('no_content').body
|
||||
end
|
||||
|
||||
def test_304
|
||||
assert_equal nil, @conn.get('not modified').body
|
||||
assert_equal nil, @conn.get('not_modified').body
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user