Skip to content
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
3 changes: 2 additions & 1 deletion lib/faraday/http_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def create_response(env)
{
status: hash[:status],
body: hash[:body] || hash[:response_body],
response_headers: hash[:response_headers]
response_headers: hash[:response_headers],
reason_phrase: hash[:reason_phrase]
}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/faraday/http_cache/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def serializable_hash
{
status: @payload[:status],
body: @payload[:body],
response_headers: @payload[:response_headers]
response_headers: @payload[:response_headers],
reason_phrase: @payload[:reason_phrase]
}
end

Expand Down
6 changes: 5 additions & 1 deletion spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
end

describe 'response unboxing' do
subject { described_class.new(status: 200, response_headers: {}, body: 'Hi!') }
subject { described_class.new(status: 200, response_headers: {}, body: 'Hi!', reason_phrase: 'Success') }

let(:env) { { method: :get } }
let(:response) { subject.to_response(env) }
Expand All @@ -224,6 +224,10 @@
it 'merges the body' do
expect(response.body).to eq('Hi!')
end

it 'merges the reason phrase' do
expect(response.reason_phrase).to eq('Success') if response.respond_to?(:reason_phrase)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard is a bit of a smell, but is needed for the specs to pass with older versions of Faraday (~>0.8.0). Would love your input for if there is a better way to write this spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to understand it... In this case you're specifying the :reason_phrase in the spec's subject. Why it would break the spec in older versions of Faraday?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garaujodev ah, there are two separate types here. subject is a Faraday::HttpCache::Response and response is a Faraday::Response (and is built via subject.to_response(env)).

The issue this guard is solving for is that Faraday::Response#reason_phrase was added after Faraday 0.8.11 was released (docs for 0.8.11 here). So, when the specs are run in CI with Faraday ~> 0.8.0, a NoMethodError is raised.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for the PR and the explanation ❤️

end
end

describe 'remove age before caching and normalize max-age if non-zero age present' do
Expand Down