mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #36446 -- Restored "q" in internal MediaType.params property.
The "q" key was removed while addressing ticket #36411. Despite
`MediaType.params` is undocumented and considered internal, it was used
in third-party projects (Zulip reported breakage), so this work restored
the `q` key in `params`.
Thanks Anders Kaseorg for the report.
Regression in c075508b4d.
This commit is contained in:
@@ -15,6 +15,10 @@ class MediaTypeTests(TestCase):
|
||||
def test_str(self):
|
||||
self.assertEqual(str(MediaType("*/*; q=0.8")), "*/*; q=0.8")
|
||||
self.assertEqual(str(MediaType("application/xml")), "application/xml")
|
||||
self.assertEqual(
|
||||
str(MediaType("application/xml;type=madeup;q=42")),
|
||||
"application/xml; type=madeup; q=42",
|
||||
)
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(MediaType("*/*; q=0.8")), "<MediaType: */*; q=0.8>")
|
||||
@@ -34,6 +38,7 @@ class MediaTypeTests(TestCase):
|
||||
(" application/xml ", "application/xml"),
|
||||
("application/xml", " application/xml "),
|
||||
("text/vcard; version=4.0", "text/vcard; version=4.0"),
|
||||
("text/vcard; version=4.0; q=0.7", "text/vcard; version=4.0"),
|
||||
("text/vcard; version=4.0", "text/vcard"),
|
||||
]
|
||||
for accepted_type, mime_type in tests:
|
||||
@@ -58,12 +63,64 @@ class MediaTypeTests(TestCase):
|
||||
# All main and sub types are defined, but there is no match.
|
||||
("application/xml", "application/html"),
|
||||
("text/vcard; version=4.0", "text/vcard; version=3.0"),
|
||||
("text/vcard; q=0.7", "text/vcard; version=3.0"),
|
||||
("text/vcard", "text/vcard; version=3.0"),
|
||||
]
|
||||
for accepted_type, mime_type in tests:
|
||||
with self.subTest(accepted_type, mime_type=mime_type):
|
||||
self.assertIs(MediaType(accepted_type).match(mime_type), False)
|
||||
|
||||
def test_params(self):
|
||||
tests = [
|
||||
("text/plain", {}, {}),
|
||||
("text/plain;q=0.7", {"q": "0.7"}, {}),
|
||||
("text/plain;q=1.5", {"q": "1.5"}, {}),
|
||||
("text/plain;q=xyz", {"q": "xyz"}, {}),
|
||||
("text/plain;q=0.1234", {"q": "0.1234"}, {}),
|
||||
("text/plain;version=2", {"version": "2"}, {"version": "2"}),
|
||||
(
|
||||
"text/plain;version=2;q=0.8",
|
||||
{"version": "2", "q": "0.8"},
|
||||
{"version": "2"},
|
||||
),
|
||||
(
|
||||
"text/plain;q=0.8;version=2",
|
||||
{"q": "0.8", "version": "2"},
|
||||
{"version": "2"},
|
||||
),
|
||||
(
|
||||
"text/plain; charset=UTF-8; q=0.3",
|
||||
{"charset": "UTF-8", "q": "0.3"},
|
||||
{"charset": "UTF-8"},
|
||||
),
|
||||
(
|
||||
"text/plain ; q = 0.5 ; version = 3.0",
|
||||
{"q": "0.5", "version": "3.0"},
|
||||
{"version": "3.0"},
|
||||
),
|
||||
("text/plain; format=flowed", {"format": "flowed"}, {"format": "flowed"}),
|
||||
(
|
||||
"text/plain; format=flowed; q=0.4",
|
||||
{"format": "flowed", "q": "0.4"},
|
||||
{"format": "flowed"},
|
||||
),
|
||||
("text/*;q=0.2", {"q": "0.2"}, {}),
|
||||
("*/json;q=0.9", {"q": "0.9"}, {}),
|
||||
("application/json;q=0.9999", {"q": "0.9999"}, {}),
|
||||
("text/html;q=0.0001", {"q": "0.0001"}, {}),
|
||||
("text/html;q=0", {"q": "0"}, {}),
|
||||
("text/html;q=0.", {"q": "0."}, {}),
|
||||
("text/html;q=.8", {"q": ".8"}, {}),
|
||||
("text/html;q= 0.9", {"q": "0.9"}, {}),
|
||||
('text/html ; q = "0.6"', {"q": "0.6"}, {}),
|
||||
]
|
||||
for accepted_type, params, range_params in tests:
|
||||
media_type = MediaType(accepted_type)
|
||||
with self.subTest(accepted_type, attr="params"):
|
||||
self.assertEqual(media_type.params, params)
|
||||
with self.subTest(accepted_type, attr="range_params"):
|
||||
self.assertEqual(media_type.range_params, range_params)
|
||||
|
||||
def test_quality(self):
|
||||
tests = [
|
||||
("*/*; q=0.8", 0.8),
|
||||
|
||||
Reference in New Issue
Block a user