Skip to content

Commit

Permalink
Add support for getting the raw request object
Browse files Browse the repository at this point in the history
  • Loading branch information
titanous committed Nov 29, 2010
1 parent 86c4028 commit 991ca51
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
16 changes: 3 additions & 13 deletions lib/active_merchant/common/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ def request(method, body, headers = {})
end

info "--> %d %s (%d %.4fs)" % [result.code, result.message, result.body ? result.body.length : 0, realtime], tag
response = handle_response(result)
debug response
response
debug result.body
result
rescue EOFError => e
raise ConnectionError, "The remote server dropped the connection"
rescue Errno::ECONNRESET => e
Expand Down Expand Up @@ -147,15 +146,6 @@ def retry_exceptions
end
end

def handle_response(response)
case response.code.to_i
when 200...300
response.body
else
raise ResponseError.new(response)
end
end

def debug(message, tag = nil)
log(:debug, message, tag)
end
Expand All @@ -169,4 +159,4 @@ def log(level, message, tag)
logger.send(level, message) if logger
end
end
end
end
22 changes: 18 additions & 4 deletions lib/active_merchant/common/posts_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ def ssl_get(endpoint, headers={})
def ssl_post(endpoint, data, headers = {})
ssl_request(:post, endpoint, data, headers)
end

private
def ssl_request(method, endpoint, data, headers = {})

def ssl_request(method, endpoint, data, headers)
handle_response(raw_ssl_request(method, endpoint, data, headers))
end

def raw_ssl_request(method, endpoint, data, headers = {})
connection = Connection.new(endpoint)
connection.open_timeout = open_timeout
connection.read_timeout = read_timeout
Expand All @@ -42,6 +45,17 @@ def ssl_request(method, endpoint, data, headers = {})

connection.request(method, data, headers)
end

private

def handle_response(response)
case response.code.to_i
when 200...300
response.body
else
raise ResponseError.new(response)
end
end

end
end
end
14 changes: 4 additions & 10 deletions test/unit/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ConnectionTest < Test::Unit::TestCase

def setup
@ok = stub(:code => 200, :message => 'OK', :body => 'success')
@internal_server_error = stub(:code => 500, :message => 'Internal Server Error', :body => 'failure')

@endpoint = 'https://example.com/tx.php'
@connection = ActiveMerchant::Connection.new(@endpoint)
Expand All @@ -29,13 +28,13 @@ def test_connection_endpoint_raises_uri_error
def test_successful_get_request
Net::HTTP.any_instance.expects(:get).with('/tx.php', {}).returns(@ok)
response = @connection.request(:get, nil, {})
assert_equal 'success', response
assert_equal 'success', response.body
end

def test_successful_post_request
Net::HTTP.any_instance.expects(:post).with('/tx.php', 'data', ActiveMerchant::Connection::RUBY_184_POST_HEADERS).returns(@ok)
response = @connection.request(:post, 'data', {})
assert_equal 'success', response
assert_equal 'success', response.body
end

def test_get_raises_argument_error_if_passed_data
Expand All @@ -50,12 +49,7 @@ def test_request_raises_when_request_method_not_supported
end
end

def test_500_response_during_request_raises_client_error
Net::HTTP.any_instance.stubs(:post).returns(@internal_server_error)
assert_raises(ActiveMerchant::ResponseError) do
@connection.request(:post, '', {})
end
end


def test_default_read_timeout
assert_equal ActiveMerchant::Connection::READ_TIMEOUT, @connection.read_timeout
Expand Down Expand Up @@ -134,4 +128,4 @@ def test_failure_with_ssl_certificate
end
end

end
end
24 changes: 19 additions & 5 deletions test/unit/posts_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,43 @@ class PostsDataTests < Test::Unit::TestCase
def setup
@url = 'http://example.com'
@gateway = SimpleTestGateway.new
@ok = stub(:body => '', :code => '200', :message => 'OK')
@error = stub(:code => 500, :message => 'Internal Server Error', :body => 'failure')
end

def teardown
SimpleTestGateway.retry_safe = false
end

def test_single_successful_post
ActiveMerchant::Connection.any_instance.expects(:request).returns('')
ActiveMerchant::Connection.any_instance.expects(:request).returns(@ok)

assert_nothing_raised do
@gateway.ssl_post(@url, '')
end
end

def test_multiple_successful_posts
ActiveMerchant::Connection.any_instance.expects(:request).times(2).returns('', '')
ActiveMerchant::Connection.any_instance.expects(:request).times(2).returns(@ok, @ok)

assert_nothing_raised do
@gateway.ssl_post(@url, '')
@gateway.ssl_post(@url, '')
end
end


def test_500_response_during_request_raises_client_error
ActiveMerchant::Connection.any_instance.expects(:request).returns(@error)
assert_raises(ActiveMerchant::ResponseError) do
@gateway.ssl_post('', {})
end
end

def test_successful_raw_request
ActiveMerchant::Connection.any_instance.expects(:request).returns(@ok)
assert_equal @ok, @gateway.raw_ssl_request(:post, @url, '')
end

def test_setting_ssl_strict_outside_class_definition
assert_equal SimpleTestGateway.ssl_strict, SubclassGateway.ssl_strict
SimpleTestGateway.ssl_strict = !SimpleTestGateway.ssl_strict
Expand All @@ -37,12 +51,12 @@ def test_setting_ssl_strict_outside_class_definition
def test_setting_timeouts
@gateway.class.open_timeout = 50
@gateway.class.read_timeout = 37
ActiveMerchant::Connection.any_instance.expects(:request).returns('')
ActiveMerchant::Connection.any_instance.expects(:request).returns(@ok)
ActiveMerchant::Connection.any_instance.expects(:open_timeout=).with(50)
ActiveMerchant::Connection.any_instance.expects(:read_timeout=).with(37)

assert_nothing_raised do
@gateway.ssl_post(@url, '')
end
end
end
end

0 comments on commit 991ca51

Please sign in to comment.