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

allow private parse method in response middleware #1123

Merged
merged 5 commits into from
Feb 10, 2020
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
5 changes: 4 additions & 1 deletion lib/faraday/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def call(env)

# Override this to modify the environment after the response has finished.
# Calls the `parse` method if defined
# `parse` method can be defined as private, public and protected
def on_complete(env)
env.body = parse(env.body) if respond_to?(:parse) && env.parse_body?
return unless respond_to?(:parse, true) && env.parse_body?

env.body = parse(env.body)
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/faraday/response/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ def parse(body)
end
end

context 'with a custom ResponseMiddleware and private parse' do
let(:custom_middleware) do
Class.new(Faraday::Response::Middleware) do
private

def parse(body)
body.upcase
end
end
end

it 'parses the response' do
expect(conn.get('ok').body).to eq('<BODY></BODY>')
end
end

context 'with a custom ResponseMiddleware but empty response' do
let(:custom_middleware) do
Class.new(Faraday::Response::Middleware) do
Expand Down