mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #33779 -- Allowed customizing encoder class in django.utils.html.json_script().
This commit is contained in:
committed by
Mariusz Felisiak
parent
0ee03a439b
commit
72e41a0df6
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.test import SimpleTestCase
|
||||
from django.utils.functional import lazystr
|
||||
from django.utils.html import (
|
||||
@@ -211,6 +212,16 @@ class TestUtilsHtml(SimpleTestCase):
|
||||
with self.subTest(arg=arg):
|
||||
self.assertEqual(json_script(arg, "test_id"), expected)
|
||||
|
||||
def test_json_script_custom_encoder(self):
|
||||
class CustomDjangoJSONEncoder(DjangoJSONEncoder):
|
||||
def encode(self, o):
|
||||
return '{"hello": "world"}'
|
||||
|
||||
self.assertHTMLEqual(
|
||||
json_script({}, encoder=CustomDjangoJSONEncoder),
|
||||
'<script type="application/json">{"hello": "world"}</script>',
|
||||
)
|
||||
|
||||
def test_json_script_without_id(self):
|
||||
self.assertHTMLEqual(
|
||||
json_script({"key": "value"}),
|
||||
|
||||
Reference in New Issue
Block a user