Debian Codegraph @

tests/requests_tests/test_accept_header.py

raw
1 from unittest import TestCase
2
3 from django.http import HttpRequest
4 from django.http.request import MediaType
5
6
7 class MediaTypeTests(TestCase):
8 def test_empty(self):
9 for empty_media_type in (None, "", " "):
10 with self.subTest(media_type=empty_media_type):
11 media_type = MediaType(empty_media_type)
12 self.assertEqual(str(media_type), "")
13 self.assertEqual(repr(media_type), "<MediaType: >")
14
15 def test_str(self):
16 self.assertEqual(str(MediaType("*/*; q=0.8")), "*/*; q=0.8")
17 self.assertEqual(str(MediaType("application/xml")), "application/xml")
18 self.assertEqual(
19 str(MediaType("application/xml;type=madeup;q=42")),
20 "application/xml; type=madeup; q=42",
21 )
22
23 def test_repr(self):
24 self.assertEqual(repr(MediaType("*/*; q=0.8")), "<MediaType: */*; q=0.8>")
25 self.assertEqual(
26 repr(MediaType("application/xml")),
27 "<MediaType: application/xml>",
28 )
29
30 def test_match(self):
31 tests = [
32 ("*/*; q=0.8", "*/*"),
33 ("*/*", "application/json"),
34 (" */* ", "application/json"),
35 ("application/*", "application/json"),
36 ("application/*", "application/*"),
37 ("application/xml", "application/xml"),
38 (" application/xml ", "application/xml"),
39 ("application/xml", " application/xml "),
40 ("text/vcard; version=4.0", "text/vcard; version=4.0"),
41 ("text/vcard; version=4.0; q=0.7", "text/vcard; version=4.0"),
42 ("text/vcard; version=4.0", "text/vcard"),
43 ]
44 for accepted_type, mime_type in tests:
45 with self.subTest(accepted_type, mime_type=mime_type):
46 self.assertIs(MediaType(accepted_type).match(mime_type), True)
47
48 def test_no_match(self):
49 tests = [
50 # other is falsey.
51 ("*/*", None),
52 ("*/*", ""),
53 # other is malformed.
54 ("*/*", "; q=0.8"),
55 # main_type is falsey.
56 ("/*", "*/*"),
57 # other.main_type is falsey.
58 ("*/*", "/*"),
59 # main sub_type is falsey.
60 ("application", "application/*"),
61 # other.sub_type is falsey.
62 ("application/*", "application"),
63 # All main and sub types are defined, but there is no match.
64 ("application/xml", "application/html"),
65 ("text/vcard; version=4.0", "text/vcard; version=3.0"),
66 ("text/vcard; q=0.7", "text/vcard; version=3.0"),
67 ("text/vcard", "text/vcard; version=3.0"),
68 ]
69 for accepted_type, mime_type in tests:
70 with self.subTest(accepted_type, mime_type=mime_type):
71 self.assertIs(MediaType(accepted_type).match(mime_type), False)
72
73 def test_params(self):
74 tests = [
75 ("text/plain", {}, {}),
76 ("text/plain;q=0.7", {"q": "0.7"}, {}),
77 ("text/plain;q=1.5", {"q": "1.5"}, {}),
78 ("text/plain;q=xyz", {"q": "xyz"}, {}),
79 ("text/plain;q=0.1234", {"q": "0.1234"}, {}),
80 ("text/plain;version=2", {"version": "2"}, {"version": "2"}),
81 (
82 "text/plain;version=2;q=0.8",
83 {"version": "2", "q": "0.8"},
84 {"version": "2"},
85 ),
86 (
87 "text/plain;q=0.8;version=2",
88 {"q": "0.8", "version": "2"},
89 {"version": "2"},
90 ),
91 (
92 "text/plain; charset=UTF-8; q=0.3",
93 {"charset": "UTF-8", "q": "0.3"},
94 {"charset": "UTF-8"},
95 ),
96 (
97 "text/plain ; q = 0.5 ; version = 3.0",
98 {"q": "0.5", "version": "3.0"},
99 {"version": "3.0"},
100 ),
101 ("text/plain; format=flowed", {"format": "flowed"}, {"format": "flowed"}),
102 (
103 "text/plain; format=flowed; q=0.4",
104 {"format": "flowed", "q": "0.4"},
105 {"format": "flowed"},
106 ),
107 ("text/*;q=0.2", {"q": "0.2"}, {}),
108 ("*/json;q=0.9", {"q": "0.9"}, {}),
109 ("application/json;q=0.9999", {"q": "0.9999"}, {}),
110 ("text/html;q=0.0001", {"q": "0.0001"}, {}),
111 ("text/html;q=0", {"q": "0"}, {}),
112 ("text/html;q=0.", {"q": "0."}, {}),
113 ("text/html;q=.8", {"q": ".8"}, {}),
114 ("text/html;q= 0.9", {"q": "0.9"}, {}),
115 ('text/html ; q = "0.6"', {"q": "0.6"}, {}),
116 ]
117 for accepted_type, params, range_params in tests:
118 media_type = MediaType(accepted_type)
119 with self.subTest(accepted_type, attr="params"):
120 self.assertEqual(media_type.params, params)
121 with self.subTest(accepted_type, attr="range_params"):
122 self.assertEqual(media_type.range_params, range_params)
123
124 def test_quality(self):
125 tests = [
126 ("*/*; q=0.8", 0.8),
127 ("*/*; q=0.0001", 0),
128 ("*/*; q=0.12345", 0.123),
129 ("*/*; q=0.1", 0.1),
130 ("*/*; q=-1", 1),
131 ("*/*; q=2", 1),
132 ("*/*; q=h", 1),
133 ("*/*; q=inf", 1),
134 ("*/*; q=0", 0),
135 ("*/*", 1),
136 ]
137 for accepted_type, quality in tests:
138 with self.subTest(accepted_type, quality=quality):
139 self.assertEqual(MediaType(accepted_type).quality, quality)
140
141 def test_specificity(self):
142 tests = [
143 ("*/*", 0),
144 ("*/*;q=0.5", 0),
145 ("text/*", 1),
146 ("text/*;q=0.5", 1),
147 ("text/html", 2),
148 ("text/html;q=1", 2),
149 ("text/html;q=0.5", 2),
150 ("text/html;version=5", 3),
151 ]
152 for accepted_type, specificity in tests:
153 with self.subTest(accepted_type, specificity=specificity):
154 self.assertEqual(MediaType(accepted_type).specificity, specificity)
155
156
157 class AcceptHeaderTests(TestCase):
158 def test_no_headers(self):
159 """Absence of Accept header defaults to '*/*'."""
160 request = HttpRequest()
161 self.assertEqual(
162 [str(accepted_type) for accepted_type in request.accepted_types],
163 ["*/*"],
164 )
165
166 def test_accept_headers(self):
167 request = HttpRequest()
168 request.META["HTTP_ACCEPT"] = (
169 "text/*,text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8,"
170 )
171 self.assertEqual(
172 [str(accepted_type) for accepted_type in request.accepted_types],
173 [
174 "text/html",
175 "application/xhtml+xml",
176 "text/*",
177 "application/xml; q=0.9",
178 "*/*; q=0.8",
179 ],
180 )
181 self.assertEqual(
182 [
183 str(accepted_type)
184 for accepted_type in request.accepted_types_by_precedence
185 ],
186 [
187 "text/html",
188 "application/xhtml+xml",
189 "application/xml; q=0.9",
190 "text/*",
191 "*/*; q=0.8",
192 ],
193 )
194
195 def test_zero_quality(self):
196 request = HttpRequest()
197 request.META["HTTP_ACCEPT"] = "text/*;q=0,text/html"
198 self.assertEqual(
199 [str(accepted_type) for accepted_type in request.accepted_types],
200 ["text/html"],
201 )
202
203 def test_precedence(self):
204 """
205 Taken from https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2.
206 """
207 request = HttpRequest()
208 request.META["HTTP_ACCEPT"] = (
209 "text/*, text/plain, text/plain;format=flowed, */*"
210 )
211 self.assertEqual(
212 [
213 str(accepted_type)
214 for accepted_type in request.accepted_types_by_precedence
215 ],
216 [
217 "text/plain; format=flowed",
218 "text/plain",
219 "text/*",
220 "*/*",
221 ],
222 )
223
224 def test_request_accepts_any(self):
225 request = HttpRequest()
226 request.META["HTTP_ACCEPT"] = "*/*"
227 self.assertIs(request.accepts("application/json"), True)
228 self.assertIsNone(request.get_preferred_type([]))
229 self.assertEqual(
230 request.get_preferred_type(["application/json", "text/plain"]),
231 "application/json",
232 )
233
234 def test_request_accepts_none(self):
235 request = HttpRequest()
236 request.META["HTTP_ACCEPT"] = ""
237 self.assertIs(request.accepts("application/json"), False)
238 self.assertEqual(request.accepted_types, [])
239 self.assertIsNone(
240 request.get_preferred_type(["application/json", "text/plain"])
241 )
242
243 def test_request_accepts_some(self):
244 request = HttpRequest()
245 request.META["HTTP_ACCEPT"] = (
246 "text/html,application/xhtml+xml,application/xml;q=0.9"
247 )
248 self.assertIs(request.accepts("text/html"), True)
249 self.assertIs(request.accepts("application/xhtml+xml"), True)
250 self.assertIs(request.accepts("application/xml"), True)
251 self.assertIs(request.accepts("application/json"), False)
252
253 def test_accept_header_priority(self):
254 request = HttpRequest()
255 request.META["HTTP_ACCEPT"] = (
256 "text/html,application/xml;q=0.9,*/*;q=0.1,text/*;q=0.5"
257 )
258
259 tests = [
260 (["text/html", "application/xml"], "text/html"),
261 (["application/xml", "application/json"], "application/xml"),
262 (["application/json"], "application/json"),
263 (["application/json", "text/plain"], "text/plain"),
264 ]
265 for types, preferred_type in tests:
266 with self.subTest(types, preferred_type=preferred_type):
267 self.assertEqual(str(request.get_preferred_type(types)), preferred_type)
268
269 def test_accept_header_priority_overlapping_mime(self):
270 request = HttpRequest()
271 request.META["HTTP_ACCEPT"] = "text/*;q=0.8,text/html;q=0.8"
272
273 self.assertEqual(
274 [str(accepted_type) for accepted_type in request.accepted_types],
275 [
276 "text/html; q=0.8",
277 "text/*; q=0.8",
278 ],
279 )
280 self.assertEqual(
281 [
282 str(accepted_type)
283 for accepted_type in request.accepted_types_by_precedence
284 ],
285 [
286 "text/html; q=0.8",
287 "text/*; q=0.8",
288 ],
289 )
290
291 def test_no_matching_accepted_type(self):
292 request = HttpRequest()
293 request.META["HTTP_ACCEPT"] = "text/html"
294
295 self.assertIsNone(
296 request.get_preferred_type(["application/json", "text/plain"])
297 )
298
299 def test_accept_with_param(self):
300 request = HttpRequest()
301 request.META["HTTP_ACCEPT"] = "text/vcard; version=3.0, text/html;q=0.5"
302
303 for media_types, expected in [
304 (
305 [
306 "text/vcard; version=4.0",
307 "text/vcard; version=3.0",
308 "text/vcard",
309 "text/directory",
310 ],
311 "text/vcard; version=3.0",
312 ),
313 (["text/vcard; version=4.0", "text/vcard", "text/directory"], None),
314 (["text/vcard; version=4.0", "text/html"], "text/html"),
315 ]:
316 self.assertEqual(request.get_preferred_type(media_types), expected)
317
318 def test_quality_for_media_type_rfc7231(self):
319 """
320 Taken from https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2.
321 """
322 request = HttpRequest()
323 request.META["HTTP_ACCEPT"] = (
324 "text/*;q=0.3,text/html;q=0.7,text/html;level=1,text/html;level=2;q=0.4,"
325 "*/*;q=0.5"
326 )
327
328 for media_type, quality in [
329 ("text/html;level=1", 1),
330 ("text/html", 0.7),
331 ("text/plain", 0.3),
332 ("image/jpeg", 0.5),
333 ("text/html;level=2", 0.4),
334 ("text/html;level=3", 0.7),
335 ]:
336 with self.subTest(media_type):
337 accepted_media_type = request.accepted_type(media_type)
338 self.assertIsNotNone(accepted_media_type)
339 self.assertEqual(accepted_media_type.quality, quality)
340
341 for media_types, expected in [
342 (["text/html", "text/html; level=1"], "text/html; level=1"),
343 (["text/html; level=2", "text/html; level=3"], "text/html; level=3"),
344 ]:
345 self.assertEqual(request.get_preferred_type(media_types), expected)
346
347 def test_quality_for_media_type_rfc9110(self):
348 """
349 Taken from https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.1-18.
350 """
351 request = HttpRequest()
352 request.META["HTTP_ACCEPT"] = (
353 "text/*;q=0.3, text/plain;q=0.7, text/plain;format=flowed, "
354 "text/plain;format=fixed;q=0.4, */*;q=0.5"
355 )
356
357 for media_type, quality in [
358 ("text/plain;format=flowed", 1),
359 ("text/plain", 0.7),
360 ("text/html", 0.3),
361 ("image/jpeg", 0.5),
362 ("text/plain;format=fixed", 0.4),
363 ("text/html;level=3", 0.3), # https://www.rfc-editor.org/errata/eid7138
364 ]:
365 with self.subTest(media_type):
366 accepted_media_type = request.accepted_type(media_type)
367 self.assertIsNotNone(accepted_media_type)
368 self.assertEqual(accepted_media_type.quality, quality)
369
370 for media_types, expected in [
371 (["text/plain", "text/plain; format=flowed"], "text/plain; format=flowed"),
372 (["text/html", "image/jpeg"], "image/jpeg"),
373 ]:
374 self.assertEqual(request.get_preferred_type(media_types), expected)
375
376 def test_quality_breaks_specificity(self):
377 """
378 With the same specificity, the quality breaks the tie.
379 """
380 request = HttpRequest()
381 request.META["HTTP_ACCEPT"] = "text/plain;q=0.5,text/html"
382
383 self.assertEqual(request.accepted_type("text/plain").quality, 0.5)
384 self.assertEqual(request.accepted_type("text/plain").specificity, 2)
385
386 self.assertEqual(request.accepted_type("text/html").quality, 1)
387 self.assertEqual(request.accepted_type("text/html").specificity, 2)
388
389 self.assertEqual(
390 request.get_preferred_type(["text/html", "text/plain"]), "text/html"
391 )
392
393 def test_quality_over_specificity(self):
394 """
395 For media types with the same quality, prefer the more specific type.
396 """
397 request = HttpRequest()
398 request.META["HTTP_ACCEPT"] = "text/*,image/jpeg"
399
400 self.assertEqual(request.accepted_type("text/plain").quality, 1)
401 self.assertEqual(request.accepted_type("text/plain").specificity, 1)
402
403 self.assertEqual(request.accepted_type("image/jpeg").quality, 1)
404 self.assertEqual(request.accepted_type("image/jpeg").specificity, 2)
405
406 self.assertEqual(
407 request.get_preferred_type(["text/plain", "image/jpeg"]), "image/jpeg"
408 )