Refs #10188 -- Added tests for BadHeaderErrors when HTTP header with newlines cannot be encoded/decoded.

This commit is contained in:
Keryn Knight 2022-02-09 16:05:23 +00:00 committed by Mariusz Felisiak
parent d436554861
commit 9bde906fb2
1 changed files with 21 additions and 0 deletions

View File

@ -364,6 +364,27 @@ class HttpResponseTests(SimpleTestCase):
with self.assertRaises(BadHeaderError):
r.headers.__setitem__("test\nstr", "test")
def test_encoded_with_newlines_in_headers(self):
"""
Keys & values which throw a UnicodeError when encoding/decoding should
still be checked for newlines and re-raised as a BadHeaderError.
These specifically would still throw BadHeaderError after decoding
successfully, because the newlines are sandwiched in the middle of the
string and email.Header leaves those as they are.
"""
r = HttpResponse()
pairs = (
("\nother", "test"),
("test", "\nother"),
(b"\xe2\x80\xa0\nother", "test"),
("test", b"\xe2\x80\xa0\nother"),
)
msg = "Header values can't contain newlines"
for key, value in pairs:
with self.subTest(key=key, value=value):
with self.assertRaisesMessage(BadHeaderError, msg):
r[key] = value
def test_dict_behavior(self):
"""
Test for bug #14020: Make HttpResponse.get work like dict.get