move uri logic to Faraday::Connection#build_uri. add #path_prefix option.

This commit is contained in:
rick 2009-12-10 05:36:52 -08:00
parent e1667ee024
commit 78a6e7331d
2 changed files with 78 additions and 10 deletions

View File

@ -3,10 +3,30 @@ module Faraday
class Connection
include Addressable
attr_accessor :host, :port
attr_reader :path_prefix
def get(url, params = {}, headers = {})
uri = URI.parse(url)
uri.query = params_to_query(params)
_get(uri, headers)
_get(build_uri(url, params), headers)
end
def path_prefix=(value)
if value
value.chomp! "/"
value.replace "/#{value}" if value !~ /^\//
end
@path_prefix = value
end
def build_uri(url, params = {})
uri = URI.parse(url)
uri.host ||= @host
uri.port ||= @port
if @path_prefix && uri.path !~ /^\//
uri.path = "#{@path_prefix}/#{uri.path}"
end
uri.query = params_to_query(params)
uri
end
def params_to_query(params)

View File

@ -1,23 +1,71 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class TestConnection < Faraday::TestCase
describe "#get" do
describe "#build_uri" do
it "uses Connection#host as default URI host" do
conn = FakeConnection.new
conn.host = 'sushi.com'
uri = conn.build_uri("/sake.html")
assert_equal 'sushi.com', uri.host
end
it "uses Connection#port as default URI port" do
conn = FakeConnection.new
conn.port = 23
uri = conn.build_uri("http://sushi.com")
assert_equal 23, uri.port
end
it "uses Connection#path_prefix to customize the path" do
conn = FakeConnection.new
conn.path_prefix = '/fish'
uri = conn.build_uri("sake.html")
assert_equal '/fish/sake.html', uri.path
end
it "forces Connection#path_prefix to be absolute" do
conn = FakeConnection.new
conn.path_prefix = 'fish'
uri = conn.build_uri("sake.html")
assert_equal '/fish/sake.html', uri.path
end
it "ignores Connection#path_prefix trailing slash" do
conn = FakeConnection.new
conn.path_prefix = '/fish/'
uri = conn.build_uri("sake.html")
assert_equal '/fish/sake.html', uri.path
end
it "allows absolute URI to ignore Connection#path_prefix" do
conn = FakeConnection.new
conn.path_prefix = '/fish'
uri = conn.build_uri("/sake.html")
assert_equal '/sake.html', uri.path
end
it "parses url/params into #path" do
conn = FakeConnection.new
resp = conn.get("http://abc.com/def.html")
assert_equal '/def.html', resp.uri.path
uri = conn.build_uri("http://sushi.com/sake.html")
assert_equal '/sake.html', uri.path
end
it "parses url/params into #query" do
conn = FakeConnection.new
resp = conn.get("http://abc.com/def.html", 'a[b]' => '1 + 2')
assert_equal "a%5Bb%5D=1%20+%202", resp.uri.query
uri = conn.build_uri("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
assert_equal "a%5Bb%5D=1%20+%202", uri.query
end
it "parses url into #host" do
conn = FakeConnection.new
resp = conn.get("http://abc.com/def.html")
assert_equal "abc.com", resp.uri.host
uri = conn.build_uri("http://sushi.com/sake.html")
assert_equal "sushi.com", uri.host
end
it "parses url into #port" do
conn = FakeConnection.new
uri = conn.build_uri("http://sushi.com/sake.html")
assert_nil uri.port
end
end