Skip to content

Commit eb6a4a6

Browse files
committed
Pin: Fix issue in json parse error handling
Previously, we had a fix that better handled a json parse error in the response body. It turns out there was more than one place a parse was happening and we just hit the 2nd place. Now we handle JSON::ParserError in all the places.
1 parent 439c3cf commit eb6a4a6

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/active_merchant/billing/gateways/pin.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,16 @@ def commit(method, action, params, options)
152152
body = parse(raw_response)
153153
rescue ResponseError => e
154154
body = parse(e.response.body)
155-
rescue JSON::ParserError
156-
return unparsable_response(raw_response)
157155
end
158156

159157
if body["response"]
160158
success_response(body)
161159
elsif body["error"]
162160
error_response(body)
163161
end
162+
163+
rescue JSON::ParserError
164+
return unparsable_response(raw_response)
164165
end
165166

166167
def success_response(body)

test/unit/gateways/pin_test.rb

+25-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,16 @@ def test_unsuccessful_request
8484
assert response.test?
8585
end
8686

87-
def test_unparsable_response
88-
@gateway.expects(:ssl_request).returns("This is not [ JSON")
87+
def test_unparsable_body_of_successful_response
88+
@gateway.stubs(:raw_ssl_request).returns(MockResponse.succeeded("This is not [ JSON"))
89+
90+
assert response = @gateway.purchase(@amount, @credit_card, @options)
91+
assert_failure response
92+
assert_match(/Invalid JSON response received/, response.message)
93+
end
94+
95+
def test_unparsable_body_of_failed_response
96+
@gateway.stubs(:raw_ssl_request).returns(MockResponse.failed("This is not [ JSON"))
8997

9098
assert response = @gateway.purchase(@amount, @credit_card, @options)
9199
assert_failure response
@@ -488,4 +496,19 @@ def successful_capture_response
488496
}
489497
}'
490498
end
499+
500+
class MockResponse
501+
attr_reader :code, :body
502+
def self.succeeded(body)
503+
MockResponse.new(200, body)
504+
end
505+
506+
def self.failed(body)
507+
MockResponse.new(422, body)
508+
end
509+
510+
def initialize(code, body)
511+
@code, @body = code, body
512+
end
513+
end
491514
end

0 commit comments

Comments
 (0)