-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance
parse_response
Method to Improve Error Handling and Logging
- Loading branch information
1 parent
7ca3887
commit ef64c60
Showing
4 changed files
with
144 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe CheckoutSdk::ApiClient do | ||
let(:configuration) do | ||
double( | ||
'CheckoutConfiguration', | ||
http_client: Faraday.new, | ||
multipart_http_client: Faraday.new, | ||
logger: Logger.new(STDOUT) | ||
) | ||
end | ||
let(:api_client) { CheckoutSdk::ApiClient.new(configuration, 'https://api.sandbox.checkout.com') } | ||
|
||
describe '#parse_response' do | ||
context 'when the response is successful' do | ||
it 'parses the response correctly for valid JSON' do | ||
response = double('Response', status: 200, body: '{"key":"value"}') | ||
allow(CheckoutSdk::CheckoutUtils).to receive(:map_to_http_metadata).and_return({ metadata: 'test' }) | ||
|
||
parsed_response = api_client.send(:parse_response, response) | ||
|
||
expect(parsed_response.key).to eq('value') | ||
expect(parsed_response.http_metadata[:metadata]).to eq('test') | ||
end | ||
|
||
it 'returns an OpenStruct object with items for an array response' do | ||
response = double('Response', status: 200, body: '[{"item":"value"}]') | ||
allow(CheckoutSdk::CheckoutUtils).to receive(:map_to_http_metadata).and_return({ metadata: 'test' }) | ||
|
||
parsed_response = api_client.send(:parse_response, response) | ||
|
||
expect(parsed_response.items).to be_an(Array) | ||
expect(parsed_response.items.first['item']).to eq('value') | ||
expect(parsed_response.http_metadata[:metadata]).to eq('test') | ||
end | ||
end | ||
|
||
context 'when the response body is nil' do | ||
it 'returns an empty OpenStruct' do | ||
response = double('Response', status: 200, body: nil) | ||
allow(CheckoutSdk::CheckoutUtils).to receive(:map_to_http_metadata).and_return({ metadata: 'test' }) | ||
|
||
parsed_response = api_client.send(:parse_response, response) | ||
|
||
expect(parsed_response).to be_a(OpenStruct) | ||
expect(parsed_response.http_metadata[:metadata]).to eq('test') | ||
end | ||
end | ||
|
||
context 'when the response status is less than 200' do | ||
it 'raises a CheckoutApiException' do | ||
response = double('Response', status: 199, body: '{}') | ||
|
||
expect do | ||
api_client.send(:parse_response, response) | ||
end.to raise_error(CheckoutSdk::CheckoutApiException, /Invalid response status: 199/) | ||
end | ||
end | ||
|
||
context 'when the response status is 400 or higher' do | ||
it 'raises a CheckoutApiException for client errors (4xx)' do | ||
response = double('Response', status: 400, body: '{}') | ||
|
||
expect do | ||
api_client.send(:parse_response, response) | ||
end.to raise_error(CheckoutSdk::CheckoutApiException, /Invalid response status: 400/) | ||
end | ||
|
||
it 'raises a CheckoutApiException for server errors (5xx)' do | ||
response = double('Response', status: 500, body: '{}') | ||
|
||
expect do | ||
api_client.send(:parse_response, response) | ||
end.to raise_error(CheckoutSdk::CheckoutApiException, /Invalid response status: 500/) | ||
end | ||
end | ||
|
||
context 'when the response body is invalid JSON' do | ||
it 'raises a CheckoutApiException with JSON parsing error' do | ||
|
||
response = double('Response', status: 200, body: '{invalid_json}') | ||
|
||
allow(CheckoutSdk::CheckoutUtils).to receive(:map_to_http_metadata).with(response).and_return(OpenStruct.new(metadata: 'test')) | ||
|
||
expect do | ||
api_client.send(:parse_response, response) | ||
end.to raise_error(CheckoutSdk::CheckoutApiException, /Error parsing JSON/) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters