From f3c927c451e53eaf8b147f49826c6f62a595da91 Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 18 Feb 2010 14:04:14 -0800 Subject: [PATCH] finished conversion, tests pass on 1.8.x and 1.9.1 --- lib/faraday/adapter/test.rb | 2 +- test/adapters/live_test.rb | 63 +++++++++++++++++---------------- test/request_middleware_test.rb | 6 ++-- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/lib/faraday/adapter/test.rb b/lib/faraday/adapter/test.rb index 0c6e88b0..1f44152d 100644 --- a/lib/faraday/adapter/test.rb +++ b/lib/faraday/adapter/test.rb @@ -73,7 +73,7 @@ module Faraday class Stub < Struct.new(:path, :body, :block) def matches?(request_path, request_body) - request_path == path && request_body == body + request_path == path && (body.to_s.size.zero? || request_body == body) end def to_s diff --git a/test/adapters/live_test.rb b/test/adapters/live_test.rb index 259f96c2..d6593fa4 100644 --- a/test/adapters/live_test.rb +++ b/test/adapters/live_test.rb @@ -4,29 +4,23 @@ if Faraday::TestCase::LIVE_SERVER module Adapters class LiveTest < Faraday::TestCase 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 - assert_equal 'hello world', @connection.get('hello_world').body + assert_equal 'hello world', create_connection(adapter).get('hello_world').body end 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' end assert_equal('hello zack', resp.body) end 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 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.body = {'name' => 'zack'} end @@ -34,7 +28,7 @@ if Faraday::TestCase::LIVE_SERVER end 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.body = {'name' => {'first' => 'zack'}} end @@ -42,13 +36,13 @@ if Faraday::TestCase::LIVE_SERVER end 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 # http://github.com/toland/patron/issues/#issue/9 if ENV['FORCE'] || adapter != Faraday::Adapter::Patron 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.body = {'name' => 'zack'} end @@ -56,7 +50,7 @@ if Faraday::TestCase::LIVE_SERVER end 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.body = {'name' => {'first' => 'zack'}} end @@ -64,60 +58,69 @@ if Faraday::TestCase::LIVE_SERVER end 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 # http://github.com/pauldix/typhoeus/issues#issue/7 if ENV['FORCE'] || adapter != Faraday::Adapter::Typhoeus 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' end assert_equal 'text/html', resp.headers['content-type'] end 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 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 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 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 define_method "test_#{adapter}_async_requests_clear_parallel_manager_after_running_a_single_request" do - assert !@connection.in_parallel? - resp = @connection.get('hello_world') - assert !@connection.in_parallel? - assert_equal 'hello world', @connection.get('hello_world').body + connection = create_connection(adapter) + assert !connection.in_parallel? + resp = connection.get('hello_world') + assert !connection.in_parallel? + assert_equal 'hello world', connection.get('hello_world').body end define_method "test_#{adapter}_async_requests_uses_parallel_manager_to_run_multiple_json_requests" do resp1, resp2 = nil, nil - - @connection.in_parallel(adapter.setup_parallel_manager) do - resp1 = @connection.get('json') - resp2 = @connection.get('json') + + connection = create_connection(adapter) + + connection.in_parallel(adapter.setup_parallel_manager) do + resp1 = connection.get('json') + resp2 = connection.get('json') if adapter.supports_parallel_requests? - assert @connection.in_parallel? + assert connection.in_parallel? assert_nil resp1.body assert_nil resp2.body end end - assert !@connection.in_parallel? + assert !connection.in_parallel? assert_equal '[1,2,3]', resp1.body assert_equal '[1,2,3]', resp2.body end end + + def create_connection(adapter) + Faraday::Connection.new LIVE_SERVER do |b| + b.use adapter + end + end end end end \ No newline at end of file diff --git a/test/request_middleware_test.rb b/test/request_middleware_test.rb index 38823ac8..bfb1cf55 100644 --- a/test/request_middleware_test.rb +++ b/test/request_middleware_test.rb @@ -6,7 +6,9 @@ class RequestMiddlewareTest < Faraday::TestCase next if !encoder.loaded? 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 @@ -15,7 +17,7 @@ private Faraday::Connection.new do |b| b.use encoder 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