Skip to content

Commit

Permalink
feat(response): [NO-TICKET] parsed_response tests and json support
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-stebbins-dailypay committed Dec 22, 2023
1 parent 45e66d4 commit 3f7749b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/httpigeon/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ def run(method: :get, path: '/', payload: {})

def parse_response
parsed_body = response_body.is_a?(String) ? JSON.parse(response_body) : response_body
deep_with_indifferent_access(parsed_body)
rescue JSON::ParserError
response_body.presence
end

case parsed_body
def deep_with_indifferent_access(obj)
case obj
when Hash
parsed_body.with_indifferent_access
obj.transform_values do |value|
deep_with_indifferent_access(value)
end.with_indifferent_access
when Array
parsed_body.map(&:with_indifferent_access)
obj.map { |item| deep_with_indifferent_access(item) }
else
parsed_body
obj
end
rescue JSON::ParserError, NoMethodError
response_body.presence
end

def default_logger(event_type, log_filters)
Expand Down
52 changes: 52 additions & 0 deletions spec/httpigeon/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,57 @@
expect(run_request).to eq(JSON.parse(response_body).with_indifferent_access)
end
end

describe 'response parsing' do
let(:method) { :get }

test_cases = [
{
description: 'when the response is a json object',
response_body: '{ "response": "body" }',
expected_parsed_response: { response: 'body' }.with_indifferent_access
},
{
description: 'when the response is an array',
response_body: '["foo"]',
expected_parsed_response: ['foo']
},
{
description: 'when the response is nested array',
response_body: '[["foo"], ["bar"]]',
expected_parsed_response: [['foo'], ['bar']]
},
{
description: 'when the response is nested json objects',
response_body: '{ "response": { "inner": "object" } }',
expected_parsed_response: { response: { inner: 'object' }.with_indifferent_access }.with_indifferent_access
},
{
description: 'when the response is json objects inside an array',
response_body: '[{ "foo": "bar" }, { "baz": "qux" }]',
expected_parsed_response: [{ foo: 'bar' }.with_indifferent_access, { baz: 'qux' }.with_indifferent_access]
},
{
description: 'when the response is arrays inside a json object',
response_body: '{ "response": ["foo", "bar"] }',
expected_parsed_response: { response: ['foo', 'bar'] }.with_indifferent_access
},
{
description: 'when the response is invalid json',
response_body: 'invalid json',
expected_parsed_response: 'invalid json'
}
]

test_cases.each do |test_case|
context test_case[:description] do
let(:response_body) { test_case[:response_body] }

it 'parses the response appropriately' do
expect(run_request).to eq(test_case[:expected_parsed_response])
end
end
end
end
end
end

0 comments on commit 3f7749b

Please sign in to comment.