diff --git a/lib/active_merchant/common/connection.rb b/lib/active_merchant/common/connection.rb index aa12dc5b865..218083669b3 100644 --- a/lib/active_merchant/common/connection.rb +++ b/lib/active_merchant/common/connection.rb @@ -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 @@ -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 @@ -169,4 +159,4 @@ def log(level, message, tag) logger.send(level, message) if logger end end -end \ No newline at end of file +end diff --git a/lib/active_merchant/common/posts_data.rb b/lib/active_merchant/common/posts_data.rb index aba2c9996ce..bce2d98fc68 100644 --- a/lib/active_merchant/common/posts_data.rb +++ b/lib/active_merchant/common/posts_data.rb @@ -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 @@ -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 \ No newline at end of file +end diff --git a/test/unit/connection_test.rb b/test/unit/connection_test.rb index 374764ae11e..ddcf7deb622 100644 --- a/test/unit/connection_test.rb +++ b/test/unit/connection_test.rb @@ -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) @@ -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 @@ -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 @@ -134,4 +128,4 @@ def test_failure_with_ssl_certificate end end -end \ No newline at end of file +end diff --git a/test/unit/posts_data_test.rb b/test/unit/posts_data_test.rb index 4f025162b4b..405abd30433 100644 --- a/test/unit/posts_data_test.rb +++ b/test/unit/posts_data_test.rb @@ -5,6 +5,8 @@ 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 @@ -12,7 +14,7 @@ def teardown 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, '') @@ -20,14 +22,26 @@ def test_single_successful_post 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 @@ -37,7 +51,7 @@ 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) @@ -45,4 +59,4 @@ def test_setting_timeouts @gateway.ssl_post(@url, '') end end -end \ No newline at end of file +end