charset names the character set to use in the RFC 2046 header. It
defaults to iso-8859-1.
"""
- # Return empty headers unchanged
+ # Return empty headers as an empty string.
if not header_bytes:
- return str(header_bytes)
+ return ''
# Iterate over every byte, encoding if necessary.
encoded = []
for octet in header_bytes:
if i == n:
decoded += eol
# Special case if original string did not end with eol
- if not encoded.endswith(eol) and decoded.endswith(eol):
+ if encoded[-1] not in '\r\n' and decoded.endswith(eol):
decoded = decoded[:-1]
return decoded
encoded_header = quoprimime.header_encode(header, charset)
self.assertEqual(encoded_header, expected_encoded_header)
+ def test_header_encode_null(self):
+ self._test_header_encode(b'', '')
+
def test_header_encode_one_word(self):
self._test_header_encode(b'hello', '=?iso-8859-1?q?hello?=')
def test_decode_one_line_lf(self):
self._test_decode('hello\n', 'hello\n')
+ def test_decode_one_line_cr(self):
+ self._test_decode('hello\r', 'hello\n')
+
+ def test_decode_one_line_nl(self):
+ self._test_decode('hello\n', 'helloX', eol='X')
+
+ def test_decode_one_line_crnl(self):
+ self._test_decode('hello\r\n', 'helloX', eol='X')
+
def test_decode_one_line_one_word(self):
self._test_decode('hello\r\nworld', 'hello\nworld')
def test_decode_two_lines(self):
self._test_decode('hello\r\nworld\r\n', 'hello\nworld\n')
+ def test_decode_two_lines_eol(self):
+ self._test_decode('hello\r\nworld\r\n', 'helloXworldX', eol='X')
+
def test_decode_one_long_line(self):
self._test_decode('Spam' * 250, 'Spam' * 250)