mirror of https://github.com/scrapy/scrapy.git
fixed bug in scrapy.http.Headers: values weren't being encoded to str when passed as lists
This commit is contained in:
parent
7f97259ba7
commit
afa23688c6
|
|
@ -19,12 +19,10 @@ class Headers(CaselessDict):
|
|||
|
||||
def normvalue(self, value):
|
||||
"""Headers must not be unicode"""
|
||||
if isinstance(value, unicode):
|
||||
value = value.encode(self.encoding)
|
||||
|
||||
if isinstance(value, (list, tuple)):
|
||||
return list(value)
|
||||
return [value]
|
||||
if not hasattr(value, '__iter__'):
|
||||
value = [value]
|
||||
return [x.encode(self.encoding) if isinstance(x, unicode) else x \
|
||||
for x in value]
|
||||
|
||||
def __getitem__(self, key):
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import unittest
|
||||
import weakref
|
||||
import copy
|
||||
|
||||
from scrapy.http import Headers
|
||||
|
|
@ -34,6 +33,23 @@ class HeadersTest(unittest.TestCase):
|
|||
self.assertEqual(h.getlist('X-Forwarded-For'), hlist)
|
||||
assert h.getlist('X-Forwarded-For') is not hlist
|
||||
|
||||
def test_encode_utf8(self):
|
||||
h = Headers({u'key': u'\xa3'}, encoding='utf-8')
|
||||
key, val = dict(h).items()[0]
|
||||
assert isinstance(key, str), key
|
||||
assert isinstance(val[0], str), val[0]
|
||||
self.assertEqual(val[0], '\xc2\xa3')
|
||||
|
||||
def test_encode_latin1(self):
|
||||
h = Headers({u'key': u'\xa3'}, encoding='latin1')
|
||||
key, val = dict(h).items()[0]
|
||||
self.assertEqual(val[0], '\xa3')
|
||||
|
||||
def test_encode_multiple(self):
|
||||
h = Headers({u'key': [u'\xa3']}, encoding='utf-8')
|
||||
key, val = dict(h).items()[0]
|
||||
self.assertEqual(val[0], '\xc2\xa3')
|
||||
|
||||
def test_delete_and_contains(self):
|
||||
h = Headers()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue