diff --git a/lib/faraday/utils/headers.rb b/lib/faraday/utils/headers.rb index 90b8be24f..d4f80868a 100644 --- a/lib/faraday/utils/headers.rb +++ b/lib/faraday/utils/headers.rb @@ -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 diff --git a/spec/faraday/utils/headers_spec.rb b/spec/faraday/utils/headers_spec.rb index 66751f7f6..238bfd990 100644 --- a/spec/faraday/utils/headers_spec.rb +++ b/spec/faraday/utils/headers_spec.rb @@ -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 } + 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" }