diff --git a/django/http/response.py b/django/http/response.py
index 5cdedec..0049125 100644
a
|
b
|
class HttpResponseBase(six.Iterator):
|
123 | 123 | |
124 | 124 | def serialize_headers(self): |
125 | 125 | """HTTP headers as a bytestring.""" |
| 126 | def force_bytes(val, encoding): |
| 127 | return val if isinstance(val, bytes) else val.encode(encoding) |
| 128 | |
126 | 129 | headers = [ |
127 | | ('%s: %s' % (key, value)).encode('us-ascii') |
| 130 | (b': '.join([force_bytes(key, 'ascii'), force_bytes(value, 'latin-1')])) |
128 | 131 | for key, value in self._headers.values() |
129 | 132 | ] |
130 | 133 | return b'\r\n'.join(headers) |
diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py
index 287d800..1e099d2 100644
a
|
b
|
class HttpResponseTests(unittest.TestCase):
|
254 | 254 | r['key'] = 'test'.encode('ascii') |
255 | 255 | self.assertEqual(r['key'], str('test')) |
256 | 256 | self.assertIsInstance(r['key'], str) |
| 257 | self.assertIn(b'test', r.serialize_headers()) |
257 | 258 | |
258 | 259 | # Latin-1 unicode or bytes values are also converted to native strings. |
259 | 260 | r['key'] = 'café' |
… |
… |
class HttpResponseTests(unittest.TestCase):
|
262 | 263 | r['key'] = 'café'.encode('latin-1') |
263 | 264 | self.assertEqual(r['key'], smart_str('café', 'latin-1')) |
264 | 265 | self.assertIsInstance(r['key'], str) |
| 266 | self.assertIn('café'.encode('latin-1'), r.serialize_headers()) |
265 | 267 | |
266 | 268 | # Other unicode values are MIME-encoded (there's no way to pass them as bytes). |
267 | 269 | r['key'] = '†' |
268 | 270 | self.assertEqual(r['key'], str('=?utf-8?b?4oCg?=')) |
269 | 271 | self.assertIsInstance(r['key'], str) |
| 272 | self.assertIn(b'=?utf-8?b?4oCg?=', r.serialize_headers()) |
270 | 273 | |
271 | 274 | # The response also converts unicode or bytes keys to strings, but requires |
272 | 275 | # them to contain ASCII |