From c6cf2cdc994dc40b1eb901002893fa57de366b33 Mon Sep 17 00:00:00 2001 From: Vitali Semenyuk Date: Mon, 1 Apr 2024 15:24:16 +0200 Subject: [PATCH] Make dig method case-insensitive in Faraday::Utils::Headers --- lib/faraday/utils/headers.rb | 6 ++++++ spec/faraday/utils/headers_spec.rb | 9 +++++++++ 2 files changed, 15 insertions(+) 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" }