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

Make dig method case-insensitive in Faraday::Utils::Headers #1557

Merged
merged 2 commits into from
Apr 2, 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
6 changes: 6 additions & 0 deletions lib/faraday/utils/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def delete(key)
super(key)
end

def dig(key, *rest)
key = KeyMap[key]
key = @names.fetch(key.downcase, key)
super(key, *rest)
end

def include?(key)
@names.include? key.downcase
end
Expand Down
9 changes: 9 additions & 0 deletions spec/faraday/utils/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
it { expect(subject.delete('content-type')).to be_nil }
end

describe '#dig' do
before { subject['Content-Type'] = 'application/json' }

it { expect(subject&.dig('Content-Type')).to eq('application/json') }
it { expect(subject&.dig('CONTENT-TYPE')).to eq('application/json') }
it { expect(subject&.dig(:content_type)).to eq('application/json') }
it { expect(subject&.dig('invalid')).to be_nil }
Copy link
Member

Choose a reason for hiding this comment

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

The safe-navigation operator is not needed here.

(The use of the subject dynamic definition's superfluous, too, but make no changes to that in this PR.)

end

describe '#parse' do
context 'when response headers leave http status line out' do
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
Expand Down
Loading