Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(response): [NO-TICKET] parsed_response tests and json support #31

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 57 additions & 0 deletions spec/httpigeon/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,62 @@
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 a truly absurd json object',
response_body: '[{"foo":"bar"},1,"foobar",true,null,[{"inner":"object"},1,null,[]]]',
expected_parsed_response: [{ foo: "bar" }.with_indifferent_access, 1, "foobar", true, nil, [{ inner: "object" }.with_indifferent_access, 1, nil, []]]
},
{
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
Loading