add basic NetHttp adapter with GET support. supply sinatra server for testing

This commit is contained in:
rick 2009-12-10 06:21:41 -08:00
parent 5e05987404
commit a7416caeeb
5 changed files with 49 additions and 0 deletions

View File

@ -8,4 +8,11 @@ module Faraday
end
autoload :Connection, 'faraday/connection'
class Response < Struct.new(:headers, :body)
end
module Adapter
autoload :NetHttp, 'faraday/adapter/net_http'
end
end

View File

@ -0,0 +1,16 @@
require 'net/http'
module Faraday
module Adapter
module NetHttp
def _get(uri, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
resp = http.get(uri.path, request_headers)
headers = {}
resp.each_header do |key, value|
headers[key] = value
end
Faraday::Response.new(headers, resp.body)
end
end
end
end

View File

@ -0,0 +1,18 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
class NetHttpAdapterTest < Faraday::TestCase
class Connection < Faraday::Connection
include Faraday::Adapter::NetHttp
end
Faraday::Connection.send :include, Faraday::Adapter::NetHttp
describe "#get" do
it "retrieves the response body" do
assert_equal 'hello world', Faraday::Connection.new(LIVE_SERVER).get('hello_world').body
end
it "retrieves the response headers" do
assert_equal 'text/html', Faraday::Connection.new(LIVE_SERVER).get('hello_world').headers['content-type']
end
end
end

View File

@ -7,6 +7,8 @@ require 'faraday'
module Faraday
class TestCase < Test::Unit::TestCase
LIVE_SERVER = 'http://localhost:4567'
class TestConnection < Faraday::Connection
def _get(uri, headers)
TestResponse.new(uri, nil, headers)

6
test/live_server.rb Normal file
View File

@ -0,0 +1,6 @@
require 'rubygems'
require 'sinatra'
get '/hello_world' do
'hello world'
end