Ticket #21282: fix_ticket_21282_httpresponse_serialize.patch

File fix_ticket_21282_httpresponse_serialize.patch, 1.5 KB (added by Raphaël Barrois, 5 years ago)

Test and fix for HttpResponse.serialize_headers() bug.

  • django/http/response.py

    diff --git a/django/http/response.py b/django/http/response.py
    index f522c06..4198360 100644
    a b class HttpResponseBase(six.Iterator): 
    129129    def serialize_headers(self):
    130130        """HTTP headers as a bytestring."""
    131131        headers = [
    132             ('%s: %s' % (key, value)).encode('us-ascii')
     132            (b'%s: %s' % (key, value))
    133133            for key, value in self._headers.values()
    134134        ]
    135135        return b'\r\n'.join(headers)
  • tests/httpwrappers/tests.py

    diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py
    index 7eb11b1..e82a7e2 100644
    a b class HttpResponseTests(unittest.TestCase): 
    417417            self.assertRaises(SuspiciousOperation,
    418418                              HttpResponsePermanentRedirect, url)
    419419
     420    def test_header_value_encoding(self):
     421        """Test encoding of header values."""
     422        r = HttpResponse()
     423        r['ascii'] = "no_special_chars"
     424        r['latin1'] = "blé"  # latin1-compatible text
     425        r['utf8'] = "Føŧ"  # Non-latin1-compatible, will be mime-encoded
     426        serialized = r.serialize_headers()
     427        header_lines = serialized.split(b'\r\n')
     428        self.assertIn(b'ascii: no_special_chars', header_lines)
     429        self.assertIn(b'latin1: bl\xe9', header_lines)
     430        self.assertIn(b'utf8: =?utf-8?b?RsO4xac=?=', header_lines)
     431
     432
    420433class HttpResponseSubclassesTests(TestCase):
    421434    def test_redirect(self):
    422435        response = HttpResponseRedirect('/redirected/')
Back to Top