finished conversion, tests pass on 1.8.x and 1.9.1

This commit is contained in:
rick 2010-02-18 14:04:14 -08:00
parent 57dfcfcb11
commit f3c927c451
3 changed files with 38 additions and 33 deletions

View File

@ -73,7 +73,7 @@ module Faraday
class Stub < Struct.new(:path, :body, :block) class Stub < Struct.new(:path, :body, :block)
def matches?(request_path, request_body) def matches?(request_path, request_body)
request_path == path && request_body == body request_path == path && (body.to_s.size.zero? || request_body == body)
end end
def to_s def to_s

View File

@ -4,29 +4,23 @@ if Faraday::TestCase::LIVE_SERVER
module Adapters module Adapters
class LiveTest < Faraday::TestCase class LiveTest < Faraday::TestCase
Faraday::Adapter.all_loaded_constants.each do |adapter| Faraday::Adapter.all_loaded_constants.each do |adapter|
define_method "setup" do
@connection = Faraday::Connection.new LIVE_SERVER do |b|
b.use adapter
end
end
define_method "test_#{adapter}_GET_retrieves_the_response_body" do define_method "test_#{adapter}_GET_retrieves_the_response_body" do
assert_equal 'hello world', @connection.get('hello_world').body assert_equal 'hello world', create_connection(adapter).get('hello_world').body
end end
define_method "test_#{adapter}_GET_send_url_encoded_params" do define_method "test_#{adapter}_GET_send_url_encoded_params" do
resp = @connection.get do |req| resp = create_connection(adapter).get do |req|
req.url 'hello', 'name' => 'zack' req.url 'hello', 'name' => 'zack'
end end
assert_equal('hello zack', resp.body) assert_equal('hello zack', resp.body)
end end
define_method "test_#{adapter}_GET_retrieves_the_response_headers" do define_method "test_#{adapter}_GET_retrieves_the_response_headers" do
assert_equal 'text/html', @connection.get('hello_world').headers['content-type'] assert_equal 'text/html', create_connection(adapter).get('hello_world').headers['content-type']
end end
define_method "test_#{adapter}_POST_send_url_encoded_params" do define_method "test_#{adapter}_POST_send_url_encoded_params" do
resp = @connection.post do |req| resp = create_connection(adapter).post do |req|
req.url 'echo_name' req.url 'echo_name'
req.body = {'name' => 'zack'} req.body = {'name' => 'zack'}
end end
@ -34,7 +28,7 @@ if Faraday::TestCase::LIVE_SERVER
end end
define_method "test_#{adapter}_POST_send_url_encoded_nested_params" do define_method "test_#{adapter}_POST_send_url_encoded_nested_params" do
resp = @connection.post do |req| resp = create_connection(adapter).post do |req|
req.url 'echo_name' req.url 'echo_name'
req.body = {'name' => {'first' => 'zack'}} req.body = {'name' => {'first' => 'zack'}}
end end
@ -42,13 +36,13 @@ if Faraday::TestCase::LIVE_SERVER
end end
define_method "test_#{adapter}_POST_retrieves_the_response_headers" do define_method "test_#{adapter}_POST_retrieves_the_response_headers" do
assert_equal 'text/html', @connection.post('echo_name').headers['content-type'] assert_equal 'text/html', create_connection(adapter).post('echo_name').headers['content-type']
end end
# http://github.com/toland/patron/issues/#issue/9 # http://github.com/toland/patron/issues/#issue/9
if ENV['FORCE'] || adapter != Faraday::Adapter::Patron if ENV['FORCE'] || adapter != Faraday::Adapter::Patron
define_method "test_#{adapter}_PUT_send_url_encoded_params" do define_method "test_#{adapter}_PUT_send_url_encoded_params" do
resp = @connection.put do |req| resp = create_connection(adapter).put do |req|
req.url 'echo_name' req.url 'echo_name'
req.body = {'name' => 'zack'} req.body = {'name' => 'zack'}
end end
@ -56,7 +50,7 @@ if Faraday::TestCase::LIVE_SERVER
end end
define_method "test_#{adapter}_PUT_send_url_encoded_nested_params" do define_method "test_#{adapter}_PUT_send_url_encoded_nested_params" do
resp = @connection.put do |req| resp = create_connection(adapter).put do |req|
req.url 'echo_name' req.url 'echo_name'
req.body = {'name' => {'first' => 'zack'}} req.body = {'name' => {'first' => 'zack'}}
end end
@ -64,60 +58,69 @@ if Faraday::TestCase::LIVE_SERVER
end end
define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do
assert_equal 'text/html', @connection.put('echo_name').headers['content-type'] assert_equal 'text/html', create_connection(adapter).put('echo_name').headers['content-type']
end end
end end
# http://github.com/pauldix/typhoeus/issues#issue/7 # http://github.com/pauldix/typhoeus/issues#issue/7
if ENV['FORCE'] || adapter != Faraday::Adapter::Typhoeus if ENV['FORCE'] || adapter != Faraday::Adapter::Typhoeus
define_method "test_#{adapter}_HEAD_send_url_encoded_params" do define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
resp = @connection.head do |req| resp = create_connection(adapter).head do |req|
req.url 'hello', 'name' => 'zack' req.url 'hello', 'name' => 'zack'
end end
assert_equal 'text/html', resp.headers['content-type'] assert_equal 'text/html', resp.headers['content-type']
end end
define_method "test_#{adapter}_HEAD_retrieves_no_response_body" do define_method "test_#{adapter}_HEAD_retrieves_no_response_body" do
assert_equal '', @connection.head('hello_world').body.to_s assert_equal '', create_connection(adapter).head('hello_world').body.to_s
end end
define_method "test_#{adapter}_HEAD_retrieves_the_response_headers" do define_method "test_#{adapter}_HEAD_retrieves_the_response_headers" do
assert_equal 'text/html', @connection.head('hello_world').headers['content-type'] assert_equal 'text/html', create_connection(adapter).head('hello_world').headers['content-type']
end end
end end
define_method "test_#{adapter}_DELETE_retrieves_the_response_headers" do define_method "test_#{adapter}_DELETE_retrieves_the_response_headers" do
assert_equal 'text/html', @connection.delete('delete_with_json').headers['content-type'] assert_equal 'text/html', create_connection(adapter).delete('delete_with_json').headers['content-type']
end end
define_method "test_#{adapter}_DELETE_retrieves_the_body" do define_method "test_#{adapter}_DELETE_retrieves_the_body" do
assert_match /deleted/, @connection.delete('delete_with_json').body assert_match /deleted/, create_connection(adapter).delete('delete_with_json').body
end end
define_method "test_#{adapter}_async_requests_clear_parallel_manager_after_running_a_single_request" do define_method "test_#{adapter}_async_requests_clear_parallel_manager_after_running_a_single_request" do
assert !@connection.in_parallel? connection = create_connection(adapter)
resp = @connection.get('hello_world') assert !connection.in_parallel?
assert !@connection.in_parallel? resp = connection.get('hello_world')
assert_equal 'hello world', @connection.get('hello_world').body assert !connection.in_parallel?
assert_equal 'hello world', connection.get('hello_world').body
end end
define_method "test_#{adapter}_async_requests_uses_parallel_manager_to_run_multiple_json_requests" do define_method "test_#{adapter}_async_requests_uses_parallel_manager_to_run_multiple_json_requests" do
resp1, resp2 = nil, nil resp1, resp2 = nil, nil
@connection.in_parallel(adapter.setup_parallel_manager) do connection = create_connection(adapter)
resp1 = @connection.get('json')
resp2 = @connection.get('json') connection.in_parallel(adapter.setup_parallel_manager) do
resp1 = connection.get('json')
resp2 = connection.get('json')
if adapter.supports_parallel_requests? if adapter.supports_parallel_requests?
assert @connection.in_parallel? assert connection.in_parallel?
assert_nil resp1.body assert_nil resp1.body
assert_nil resp2.body assert_nil resp2.body
end end
end end
assert !@connection.in_parallel? assert !connection.in_parallel?
assert_equal '[1,2,3]', resp1.body assert_equal '[1,2,3]', resp1.body
assert_equal '[1,2,3]', resp2.body assert_equal '[1,2,3]', resp2.body
end end
end end
def create_connection(adapter)
Faraday::Connection.new LIVE_SERVER do |b|
b.use adapter
end
end
end end
end end
end end

View File

@ -6,7 +6,9 @@ class RequestMiddlewareTest < Faraday::TestCase
next if !encoder.loaded? next if !encoder.loaded?
define_method "test_encodes_json_with_#{key}" do define_method "test_encodes_json_with_#{key}" do
assert_equal %({"a":1}), create_json_connection(encoder).post('echo_body', :a => 1).body raw_json = create_json_connection(encoder).post('echo_body', :a => 1).body
raw_json.gsub! /: 1/, ':1' # sometimes rails_json adds a space
assert_equal %({"a":1}), raw_json
end end
end end
@ -15,7 +17,7 @@ private
Faraday::Connection.new do |b| Faraday::Connection.new do |b|
b.use encoder b.use encoder
b.adapter :test do |stub| b.adapter :test do |stub|
stub.post('echo_body', '{"a":1}') { |env| [200, {'Content-Type' => 'text/html'}, env[:body]] } stub.post('echo_body') { |env| [200, {'Content-Type' => 'text/html'}, env[:body]] }
end end
end end
end end