parse @scheme from the uri in Faraday::Connection

This commit is contained in:
rick 2009-12-10 08:31:48 -08:00
parent 899837f181
commit 93d08dd305
2 changed files with 19 additions and 5 deletions

View File

@ -3,7 +3,7 @@ module Faraday
class Connection
include Addressable
attr_accessor :host, :port
attr_accessor :host, :port, :scheme
attr_reader :path_prefix
attr_writer :response_class
@ -11,6 +11,7 @@ module Faraday
@response_class = nil
if url
uri = URI.parse(url)
self.scheme = uri.scheme
self.host = uri.host
self.port = uri.port
self.path_prefix = uri.path
@ -39,13 +40,15 @@ module Faraday
end
def build_uri(url, params = {})
uri = URI.parse(url)
uri.host ||= @host
uri.port ||= @port
uri = URI.parse(url)
uri.scheme ||= @scheme
uri.host ||= @host
uri.port ||= @port
if @path_prefix && uri.path !~ /^\//
uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
end
uri.query = params_to_query(params)
query = params_to_query(params)
if !query.empty? then uri.query = query end
uri
end

View File

@ -12,6 +12,11 @@ class ConnectionTest < Faraday::TestCase
assert_nil conn.port
end
it "parses @scheme out of given url" do
conn = Faraday::Connection.new "http://sushi.com"
assert_equal 'http', conn.scheme
end
it "parses @port out of given url" do
conn = Faraday::Connection.new "http://sushi.com:815"
assert_equal 815, conn.port
@ -43,6 +48,12 @@ class ConnectionTest < Faraday::TestCase
assert_equal 23, uri.port
end
it "uses Connection#scheme as default URI scheme" do
conn = Faraday::Connection.new 'http://sushi.com'
uri = conn.build_uri("/sake.html")
assert_equal 'http', uri.scheme
end
it "uses Connection#path_prefix to customize the path" do
conn = Faraday::Connection.new
conn.path_prefix = '/fish'