fix Utils::Headers

This commit is contained in:
Mislav Marohnić 2012-01-29 00:06:34 +01:00
parent 562fb16ff9
commit aec0723735
2 changed files with 36 additions and 3 deletions

View File

@ -9,7 +9,7 @@ module Faraday
def initialize(hash={})
super()
@names = {}
hash.each { |k, v| self[k] = v }
self.update hash
end
# symbol -> string mapper + cache
@ -30,12 +30,20 @@ module Faraday
def []=(k, v)
k = KeyMap[k]
@names[k.downcase] = k
k = (@names[k.downcase] ||= k)
# join multiple values with a comma
v = v.to_ary.join(', ') if v.respond_to? :to_ary
super k, v
end
def delete(k)
k = KeyMap[k]
if k = @names[k.downcase]
@names.delete k.downcase
super k
end
end
def include?(k)
@names.include? k.downcase
end
@ -57,7 +65,7 @@ module Faraday
def replace(other)
clear
other.each { |k, v| self[k] = v }
self.update other
self
end

View File

@ -83,6 +83,31 @@ class HeadersTest < Faraday::TestCase
@headers = Faraday::Utils::Headers.new
end
def test_normalizes_different_capitalizations
@headers['Content-Type'] = 'application/json'
assert_equal ['Content-Type'], @headers.keys
assert_equal 'application/json', @headers['Content-Type']
assert_equal 'application/json', @headers['CONTENT-TYPE']
assert_equal 'application/json', @headers['content-type']
assert @headers.include?('content-type')
@headers['content-type'] = 'application/xml'
assert_equal ['Content-Type'], @headers.keys
assert_equal 'application/xml', @headers['Content-Type']
assert_equal 'application/xml', @headers['CONTENT-TYPE']
assert_equal 'application/xml', @headers['content-type']
end
def test_delete_key
@headers['Content-Type'] = 'application/json'
assert_equal 1, @headers.size
assert @headers.include?('content-type')
assert_equal 'application/json', @headers.delete('content-type')
assert_equal 0, @headers.size
assert !@headers.include?('content-type')
assert_equal nil, @headers.delete('content-type')
end
def test_parse_response_headers_leaves_http_status_line_out
@headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
assert_equal %w(Content-Type), @headers.keys