mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-30 00:03:09 -04:00
set default Faraday::Connection#path_prefix
This commit is contained in:
parent
f5128bad51
commit
637909c690
@ -8,20 +8,26 @@ module Faraday
|
||||
|
||||
def initialize(url = nil)
|
||||
if url
|
||||
uri = URI.parse(url)
|
||||
@host = uri.host
|
||||
@port = uri.port
|
||||
@path_prefix = uri.path if !uri.path.empty?
|
||||
uri = URI.parse(url)
|
||||
self.host = uri.host
|
||||
self.port = uri.port
|
||||
self.path_prefix = uri.path
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Override in a subclass, or include an adapter
|
||||
#
|
||||
# def _get(uri, headers)
|
||||
# end
|
||||
#
|
||||
def get(url, params = {}, headers = {})
|
||||
_get(build_uri(url, params), headers)
|
||||
end
|
||||
|
||||
def path_prefix=(value)
|
||||
if value
|
||||
value.chomp! "/"
|
||||
value.chomp! "/"
|
||||
value.replace "/#{value}" if value !~ /^\//
|
||||
end
|
||||
@path_prefix = value
|
||||
@ -32,7 +38,7 @@ module Faraday
|
||||
uri.host ||= @host
|
||||
uri.port ||= @port
|
||||
if @path_prefix && uri.path !~ /^\//
|
||||
uri.path = "#{@path_prefix}/#{uri.path}"
|
||||
uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
|
||||
end
|
||||
uri.query = params_to_query(params)
|
||||
uri
|
||||
@ -43,9 +49,5 @@ module Faraday
|
||||
memo << "#{URI.escape(key)}=#{URI.escape(val)}"
|
||||
end.join("&")
|
||||
end
|
||||
|
||||
def _get(uri, headers)
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
end
|
@ -19,7 +19,7 @@ class ConnectionTest < Faraday::TestCase
|
||||
|
||||
it "parses nil @path_prefix out of given url" do
|
||||
conn = TestConnection.new "http://sushi.com"
|
||||
assert_nil conn.path_prefix
|
||||
assert_equal '/', conn.path_prefix
|
||||
end
|
||||
|
||||
it "parses @path_prefix out of given url" do
|
||||
@ -50,6 +50,13 @@ class ConnectionTest < Faraday::TestCase
|
||||
assert_equal '/fish/sake.html', uri.path
|
||||
end
|
||||
|
||||
it "uses '/' Connection#path_prefix to customize the path" do
|
||||
conn = TestConnection.new
|
||||
conn.path_prefix = '/'
|
||||
uri = conn.build_uri("sake.html")
|
||||
assert_equal '/sake.html', uri.path
|
||||
end
|
||||
|
||||
it "forces Connection#path_prefix to be absolute" do
|
||||
conn = TestConnection.new
|
||||
conn.path_prefix = 'fish'
|
||||
|
Loading…
x
Reference in New Issue
Block a user