mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-05 00:05:35 -04:00
Support default json decoder even when nil responds to :load
(#1563)
In Rails (some versions at least), nil responds to `:load` (via ActiveSupport::Dependencies::Loadable mixin). Enable default json decoder to work in this case
This commit is contained in:
parent
6933e9b70f
commit
04515f38b3
@ -60,7 +60,8 @@ module Faraday
|
||||
@decoder_options =
|
||||
if @decoder_options.is_a?(Array) && @decoder_options.size >= 2
|
||||
@decoder_options.slice(0, 2)
|
||||
elsif @decoder_options.respond_to?(:load)
|
||||
elsif @decoder_options&.respond_to?(:load) # rubocop:disable Lint/RedundantSafeNavigation
|
||||
# In some versions of Rails, `nil` responds to `load` - hence the safe navigation check above
|
||||
[@decoder_options, :load]
|
||||
else
|
||||
[::JSON, :parse]
|
||||
|
@ -184,6 +184,23 @@ RSpec.describe Faraday::Response::Json, type: :response do
|
||||
response = process(body)
|
||||
expect(response.body).to eq(result)
|
||||
end
|
||||
|
||||
it 'passes relevant options to JSON parse even when nil responds to :load' do
|
||||
original_allow_message_expectations_on_nil = RSpec::Mocks.configuration.allow_message_expectations_on_nil
|
||||
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
|
||||
allow(nil).to receive(:respond_to?)
|
||||
.with(:load)
|
||||
.and_return(true)
|
||||
|
||||
expect(JSON).to receive(:parse)
|
||||
.with(body, { symbolize_names: true })
|
||||
.and_return(result)
|
||||
|
||||
response = process(body)
|
||||
expect(response.body).to eq(result)
|
||||
ensure
|
||||
RSpec::Mocks.configuration.allow_message_expectations_on_nil = original_allow_message_expectations_on_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user