Make Headers#fetch case-insensitive

Matches the behavior of `[]` and `[]=` methods. Also accepts symbols:

    headers.fetch(:content_type)
This commit is contained in:
Mislav Marohnić 2013-10-06 16:10:28 +02:00
parent 3e6654667f
commit 7dff04854e
2 changed files with 25 additions and 0 deletions

View File

@ -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]

View File

@ -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