From afa23688c6d625d21cf008eed778f485a5e3e838 Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Sun, 1 May 2011 19:39:13 -0300 Subject: [PATCH] fixed bug in scrapy.http.Headers: values weren't being encoded to str when passed as lists --- scrapy/http/headers.py | 10 ++++------ scrapy/tests/test_http_headers.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/scrapy/http/headers.py b/scrapy/http/headers.py index fb612a55f..5efeda29c 100644 --- a/scrapy/http/headers.py +++ b/scrapy/http/headers.py @@ -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: diff --git a/scrapy/tests/test_http_headers.py b/scrapy/tests/test_http_headers.py index 871a987ca..7433ed830 100644 --- a/scrapy/tests/test_http_headers.py +++ b/scrapy/tests/test_http_headers.py @@ -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()