mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #24836 -- Made force_text() resolve lazy objects.
This commit is contained in:
parent
10945ebeb8
commit
70be31bba7
4 changed files with 25 additions and 2 deletions
15
tests/csrf_tests/test_context_processor.py
Normal file
15
tests/csrf_tests/test_context_processor.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
import json
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.template.context_processors import csrf
|
||||
from django.test import SimpleTestCase
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
|
||||
class TestContextProcessor(SimpleTestCase):
|
||||
|
||||
def test_force_text_on_token(self):
|
||||
request = HttpRequest()
|
||||
request.META['CSRF_COOKIE'] = 'test-token'
|
||||
token = csrf(request).get('csrf_token')
|
||||
self.assertEqual(json.dumps(force_text(token)), '"test-token"')
|
|
@ -9,6 +9,7 @@ from django.utils.encoding import (
|
|||
escape_uri_path, filepath_to_uri, force_bytes, force_text, iri_to_uri,
|
||||
smart_text, uri_to_iri,
|
||||
)
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
from django.utils.http import urlquote_plus
|
||||
|
||||
|
||||
|
@ -28,6 +29,10 @@ class TestEncodingUtils(unittest.TestCase):
|
|||
exception = TypeError if six.PY3 else UnicodeError
|
||||
self.assertRaises(exception, force_text, MyString())
|
||||
|
||||
def test_force_text_lazy(self):
|
||||
s = SimpleLazyObject(lambda: 'x')
|
||||
self.assertTrue(issubclass(type(force_text(s)), six.text_type))
|
||||
|
||||
def test_force_bytes_exception(self):
|
||||
"""
|
||||
Test that force_bytes knows how to convert to bytes an exception
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue