mirror of
https://github.com/django/django.git
synced 2025-10-28 16:16:12 +00:00
Fixed #35631 -- Added HttpRequest.get_preferred_type().
This commit is contained in:
@@ -56,6 +56,35 @@ class MediaTypeTests(TestCase):
|
||||
with self.subTest(accepted_type, mime_type=mime_type):
|
||||
self.assertIs(MediaType(accepted_type).match(mime_type), False)
|
||||
|
||||
def test_quality(self):
|
||||
tests = [
|
||||
("*/*; q=0.8", 0.8),
|
||||
("*/*; q=0.0001", 0),
|
||||
("*/*; q=0.12345", 0.123),
|
||||
("*/*; q=0.1", 0.1),
|
||||
("*/*; q=-1", 1),
|
||||
("*/*; q=2", 1),
|
||||
("*/*; q=h", 1),
|
||||
("*/*", 1),
|
||||
]
|
||||
for accepted_type, quality in tests:
|
||||
with self.subTest(accepted_type, quality=quality):
|
||||
self.assertEqual(MediaType(accepted_type).quality, quality)
|
||||
|
||||
def test_specificity(self):
|
||||
tests = [
|
||||
("*/*", 0),
|
||||
("*/*;q=0.5", 0),
|
||||
("text/*", 1),
|
||||
("text/*;q=0.5", 1),
|
||||
("text/html", 2),
|
||||
("text/html;q=1", 2),
|
||||
("text/html;q=0.5", 3),
|
||||
]
|
||||
for accepted_type, specificity in tests:
|
||||
with self.subTest(accepted_type, specificity=specificity):
|
||||
self.assertEqual(MediaType(accepted_type).specificity, specificity)
|
||||
|
||||
|
||||
class AcceptHeaderTests(TestCase):
|
||||
def test_no_headers(self):
|
||||
@@ -69,13 +98,14 @@ class AcceptHeaderTests(TestCase):
|
||||
def test_accept_headers(self):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = (
|
||||
"text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8"
|
||||
"text/*,text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8,"
|
||||
)
|
||||
self.assertEqual(
|
||||
[str(accepted_type) for accepted_type in request.accepted_types],
|
||||
[
|
||||
"text/html",
|
||||
"application/xhtml+xml",
|
||||
"text/*",
|
||||
"application/xml; q=0.9",
|
||||
"*/*; q=0.8",
|
||||
],
|
||||
@@ -85,12 +115,20 @@ class AcceptHeaderTests(TestCase):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = "*/*"
|
||||
self.assertIs(request.accepts("application/json"), True)
|
||||
self.assertIsNone(request.get_preferred_type([]))
|
||||
self.assertEqual(
|
||||
request.get_preferred_type(["application/json", "text/plain"]),
|
||||
"application/json",
|
||||
)
|
||||
|
||||
def test_request_accepts_none(self):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = ""
|
||||
self.assertIs(request.accepts("application/json"), False)
|
||||
self.assertEqual(request.accepted_types, [])
|
||||
self.assertIsNone(
|
||||
request.get_preferred_type(["application/json", "text/plain"])
|
||||
)
|
||||
|
||||
def test_request_accepts_some(self):
|
||||
request = HttpRequest()
|
||||
@@ -101,3 +139,39 @@ class AcceptHeaderTests(TestCase):
|
||||
self.assertIs(request.accepts("application/xhtml+xml"), True)
|
||||
self.assertIs(request.accepts("application/xml"), True)
|
||||
self.assertIs(request.accepts("application/json"), False)
|
||||
|
||||
def test_accept_header_priority(self):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = (
|
||||
"text/html,application/xml;q=0.9,*/*;q=0.1,text/*;q=0.5"
|
||||
)
|
||||
|
||||
tests = [
|
||||
(["text/html", "application/xml"], "text/html"),
|
||||
(["application/xml", "application/json"], "application/xml"),
|
||||
(["application/json"], "application/json"),
|
||||
(["application/json", "text/plain"], "text/plain"),
|
||||
]
|
||||
for types, preferred_type in tests:
|
||||
with self.subTest(types, preferred_type=preferred_type):
|
||||
self.assertEqual(str(request.get_preferred_type(types)), preferred_type)
|
||||
|
||||
def test_accept_header_priority_overlapping_mime(self):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = "text/*;q=0.8,text/html;q=0.8"
|
||||
|
||||
self.assertEqual(
|
||||
[str(accepted_type) for accepted_type in request.accepted_types],
|
||||
[
|
||||
"text/html; q=0.8",
|
||||
"text/*; q=0.8",
|
||||
],
|
||||
)
|
||||
|
||||
def test_no_matching_accepted_type(self):
|
||||
request = HttpRequest()
|
||||
request.META["HTTP_ACCEPT"] = "text/html"
|
||||
|
||||
self.assertIsNone(
|
||||
request.get_preferred_type(["application/json", "text/plain"])
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user