mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-05 00:05:35 -04:00
Make Headers#fetch case-insensitive
Matches the behavior of `[]` and `[]=` methods. Also accepts symbols: headers.fetch(:content_type)
This commit is contained in:
parent
3e6654667f
commit
7dff04854e
@ -45,6 +45,12 @@ module Faraday
|
||||
super k, v
|
||||
end
|
||||
|
||||
def fetch(k, *args, &block)
|
||||
k = KeyMap[k]
|
||||
key = @names.fetch(k.downcase, k)
|
||||
super(key, *args, &block)
|
||||
end
|
||||
|
||||
def delete(k)
|
||||
k = KeyMap[k]
|
||||
if k = @names[k.downcase]
|
||||
|
@ -98,6 +98,25 @@ class HeadersTest < Faraday::TestCase
|
||||
assert_equal 'application/xml', @headers['content-type']
|
||||
end
|
||||
|
||||
def test_fetch_key
|
||||
@headers['Content-Type'] = 'application/json'
|
||||
block_called = false
|
||||
assert_equal 'application/json', @headers.fetch('content-type') { block_called = true }
|
||||
assert_equal 'application/json', @headers.fetch('Content-Type')
|
||||
assert_equal 'application/json', @headers.fetch('CONTENT-TYPE')
|
||||
assert_equal 'application/json', @headers.fetch(:content_type)
|
||||
assert_equal false, block_called
|
||||
|
||||
assert_equal 'default', @headers.fetch('invalid', 'default')
|
||||
assert_equal false, @headers.fetch('invalid', false)
|
||||
assert_nil @headers.fetch('invalid', nil)
|
||||
|
||||
assert_equal 'Invalid key', @headers.fetch('Invalid') { |key| "#{key} key" }
|
||||
|
||||
expected_error = defined?(KeyError) ? KeyError : IndexError
|
||||
assert_raises(expected_error) { @headers.fetch('invalid') }
|
||||
end
|
||||
|
||||
def test_delete_key
|
||||
@headers['Content-Type'] = 'application/json'
|
||||
assert_equal 1, @headers.size
|
||||
|
Loading…
x
Reference in New Issue
Block a user