Skip to content

Commit

Permalink
refactor adapter tests to use modules for shared tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Cheung authored and mislav committed Apr 14, 2012
1 parent e0bdfd2 commit 265c02e
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 244 deletions.
12 changes: 12 additions & 0 deletions test/adapters/default_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require File.expand_path('../integration', __FILE__)

module Adapters
class DefaultTest < Faraday::TestCase
include Integration
include Integration::NonParallel

def adapter; :default end

undef :test_POST_sends_files
end
end
13 changes: 13 additions & 0 deletions test/adapters/em_http_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require File.expand_path('../integration', __FILE__)

module Adapters
class EMHttpTest < Faraday::TestCase
include Integration
include Integration::Parallel

def adapter; :em_http end

# https://github.com/eventmachine/eventmachine/pull/289
undef :test_timeout
end
end
13 changes: 13 additions & 0 deletions test/adapters/em_synchrony_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require File.expand_path('../integration', __FILE__)

module Adapters
class EMSynchronyTest < Faraday::TestCase
include Integration
include Integration::Parallel

def adapter; :em_synchrony end

# https://github.com/eventmachine/eventmachine/pull/289
undef :test_timeout
end
end
16 changes: 16 additions & 0 deletions test/adapters/excon_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require File.expand_path('../integration', __FILE__)

module Adapters
class ExconTest < Faraday::TestCase
# https://github.com/geemus/excon/issues/98
if defined?(RUBY_ENGINE) && "rbx" != RUBY_ENGINE
include Integration
include Integration::NonParallel

def adapter; :excon end

# https://github.com/eventmachine/eventmachine/pull/289
undef :test_timeout
end
end
end
49 changes: 38 additions & 11 deletions test/adapters/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ def test_no_parallel_support
end
end

module Timeout
if Faraday::TestCase::LIVE_SERVER
def test_timeout
conn = create_connection(adapter, :request => {:timeout => 1, :open_timeout => 1})
assert_raise Faraday::Error::TimeoutError do
conn.get '/slow'
end
end
end
end

module Common
def test_GET_retrieves_the_response_body
assert_equal 'hello world', create_connection(adapter).get('hello_world').body
Expand All @@ -79,6 +68,18 @@ def test_GET_retrieves_the_response_headers
assert_match(/text\/html/, response.headers['content-type'], 'lowercase fail')
end

def test_GET_handles_headers_with_multiple_values
response = create_connection(adapter).get('multi')
assert_equal 'one, two', response.headers['set-cookie']
end

def test_GET_with_body
response = create_connection(adapter).get('echo') do |req|
req.body = {'bodyrock' => true}
end
assert_equal %(get {"bodyrock"=>"true"}), response.body
end

def test_POST_send_url_encoded_params
resp = create_connection(adapter).post do |req|
req.url 'echo_name'
Expand Down Expand Up @@ -107,6 +108,14 @@ def test_POST_sends_files
assert_equal "file integration.rb text/x-ruby", resp.body
end

def test_PUT_send_url_encoded_params
resp = create_connection(adapter).put do |req|
req.url 'echo_name'
req.body = {'name' => 'zack'}
end
assert_equal %("zack"), resp.body
end

def test_PUT_send_url_encoded_nested_params
resp = create_connection(adapter).put do |req|
req.url 'echo_name'
Expand All @@ -115,6 +124,15 @@ def test_PUT_send_url_encoded_nested_params
assert_equal %({"first"=>"zack"}), resp.body
end

def test_PUT_retrieves_the_response_headers
assert_match(/text\/html/, create_connection(adapter).put('echo_name').headers['content-type'])
end

def test_PATCH_send_url_encoded_params
resp = create_connection(adapter).patch('echo_name', 'name' => 'zack')
assert_equal %("zack"), resp.body
end

def test_OPTIONS
resp = create_connection(adapter).run_request(:options, '/options', nil, {})
assert_equal "hi", resp.body
Expand Down Expand Up @@ -143,6 +161,13 @@ def test_DELETE_retrieves_the_body
assert_match(/deleted/, create_connection(adapter).delete('delete_with_json').body)
end

def test_timeout
conn = create_connection(adapter, :request => {:timeout => 1, :open_timeout => 1})
assert_raise Faraday::Error::TimeoutError do
conn.get '/slow'
end
end

def adapter
raise NotImplementedError.new("Need to override #adapter")
end
Expand All @@ -165,6 +190,8 @@ def create_connection(adapter, options = {})

Faraday::Connection.new(Faraday::TestCase::LIVE_SERVER, options, &builder_block).tap do |conn|
conn.headers['X-Faraday-Adapter'] = adapter.to_s
adapter_handler = conn.builder.handlers.last
conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
end
end
end
Expand Down
229 changes: 0 additions & 229 deletions test/adapters/live_test.rb

This file was deleted.

10 changes: 10 additions & 0 deletions test/adapters/net_http_persistent_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.expand_path('../integration', __FILE__)

module Adapters
class NetHttpPersistentTest < Faraday::TestCase
include Integration
include Integration::NonParallel

def adapter; :net_http_persistent end
end
end
Loading

0 comments on commit 265c02e

Please sign in to comment.