From 2f1394c76dd586ceab3f86f5b6cb4473382c9af8 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 1 Feb 2017 15:48:53 -0500 Subject: [PATCH] Added a test for force_text()'s DjangoUnicodeDecodeError path. --- tests/utils_tests/test_encoding.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py index bd937bf960..b7ffd0afe7 100644 --- a/tests/utils_tests/test_encoding.py +++ b/tests/utils_tests/test_encoding.py @@ -2,14 +2,15 @@ import datetime import unittest from urllib.parse import quote_plus +from django.test import SimpleTestCase from django.utils.encoding import ( - escape_uri_path, filepath_to_uri, force_bytes, force_text, iri_to_uri, - smart_text, uri_to_iri, + DjangoUnicodeDecodeError, escape_uri_path, filepath_to_uri, force_bytes, + force_text, iri_to_uri, smart_text, uri_to_iri, ) from django.utils.functional import SimpleLazyObject -class TestEncodingUtils(unittest.TestCase): +class TestEncodingUtils(SimpleTestCase): def test_force_text_exception(self): """ Broken __str__ actually raises an error. @@ -26,6 +27,14 @@ class TestEncodingUtils(unittest.TestCase): s = SimpleLazyObject(lambda: 'x') self.assertTrue(type(force_text(s)), str) + def test_force_text_DjangoUnicodeDecodeError(self): + msg = ( + "'utf-8' codec can't decode byte 0xff in position 0: invalid " + "start byte. You passed in b'\\xff' ()" + ) + with self.assertRaisesMessage(DjangoUnicodeDecodeError, msg): + force_text(b'\xff') + def test_force_bytes_exception(self): """ force_bytes knows how to convert to bytes an exception