add initial Faraday::Connection with #get method

This commit is contained in:
rick 2009-12-10 05:14:53 -08:00
parent 5b500c5307
commit e1667ee024
4 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,3 @@
module Faraday
autoload :Connection, 'faraday/connection'
end

22
lib/faraday/connection.rb Normal file
View File

@ -0,0 +1,22 @@
require 'addressable/uri'
module Faraday
class Connection
include Addressable
def get(url, params = {}, headers = {})
uri = URI.parse(url)
uri.query = params_to_query(params)
_get(uri, headers)
end
def params_to_query(params)
params.inject([]) do |memo, (key, val)|
memo << "#{URI.escape(key)}=#{URI.escape(val)}"
end.join("&")
end
def _get(uri, headers)
raise NotImplementedError
end
end
end

30
test/connection_test.rb Normal file
View File

@ -0,0 +1,30 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class TestConnection < Faraday::TestCase
describe "#get" do
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
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
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
end
end
describe "#params_to_query" do
it "converts hash of params to URI-escaped query string" do
conn = Faraday::Connection.new
assert_equal "a%5Bb%5D=1%20+%202", conn.params_to_query('a[b]' => '1 + 2')
end
end
end

View File

@ -1,9 +1,19 @@
require 'rubygems'
require 'test/unit'
require 'context'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'faraday'
class Test::Unit::TestCase
module Faraday
class TestCase < Test::Unit::TestCase
class FakeConnection < Faraday::Connection
def _get(uri, headers)
FakeResponse.new(uri, nil, headers)
end
end
class FakeResponse < Struct.new(:uri, :content, :headers)
end
end
end