mirror of
https://github.com/lostisland/faraday.git
synced 2025-09-29 00:01:53 -04:00
add basic NetHttp adapter with GET support. supply sinatra server for testing
This commit is contained in:
parent
5e05987404
commit
a7416caeeb
@ -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
|
16
lib/faraday/adapter/net_http.rb
Normal file
16
lib/faraday/adapter/net_http.rb
Normal 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
|
18
test/adapter/net_http_test.rb
Normal file
18
test/adapter/net_http_test.rb
Normal 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
|
@ -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
6
test/live_server.rb
Normal file
@ -0,0 +1,6 @@
|
||||
require 'rubygems'
|
||||
require 'sinatra'
|
||||
|
||||
get '/hello_world' do
|
||||
'hello world'
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user