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.assertIs(media_type.is_all_types, False)
    
  13.                 self.assertEqual(str(media_type), "")
    
  14.                 self.assertEqual(repr(media_type), "<MediaType: >")
    
  15. 
    
  16.     def test_str(self):
    
  17.         self.assertEqual(str(MediaType("*/*; q=0.8")), "*/*; q=0.8")
    
  18.         self.assertEqual(str(MediaType("application/xml")), "application/xml")
    
  19. 
    
  20.     def test_repr(self):
    
  21.         self.assertEqual(repr(MediaType("*/*; q=0.8")), "<MediaType: */*; q=0.8>")
    
  22.         self.assertEqual(
    
  23.             repr(MediaType("application/xml")),
    
  24.             "<MediaType: application/xml>",
    
  25.         )
    
  26. 
    
  27.     def test_is_all_types(self):
    
  28.         self.assertIs(MediaType("*/*").is_all_types, True)
    
  29.         self.assertIs(MediaType("*/*; q=0.8").is_all_types, True)
    
  30.         self.assertIs(MediaType("text/*").is_all_types, False)
    
  31.         self.assertIs(MediaType("application/xml").is_all_types, False)
    
  32. 
    
  33.     def test_match(self):
    
  34.         tests = [
    
  35.             ("*/*; q=0.8", "*/*"),
    
  36.             ("*/*", "application/json"),
    
  37.             (" */* ", "application/json"),
    
  38.             ("application/*", "application/json"),
    
  39.             ("application/xml", "application/xml"),
    
  40.             (" application/xml ", "application/xml"),
    
  41.             ("application/xml", " application/xml "),
    
  42.         ]
    
  43.         for accepted_type, mime_type in tests:
    
  44.             with self.subTest(accepted_type, mime_type=mime_type):
    
  45.                 self.assertIs(MediaType(accepted_type).match(mime_type), True)
    
  46. 
    
  47.     def test_no_match(self):
    
  48.         tests = [
    
  49.             (None, "*/*"),
    
  50.             ("", "*/*"),
    
  51.             ("; q=0.8", "*/*"),
    
  52.             ("application/xml", "application/html"),
    
  53.             ("application/xml", "*/*"),
    
  54.         ]
    
  55.         for accepted_type, mime_type in tests:
    
  56.             with self.subTest(accepted_type, mime_type=mime_type):
    
  57.                 self.assertIs(MediaType(accepted_type).match(mime_type), False)
    
  58. 
    
  59. 
    
  60. class AcceptHeaderTests(TestCase):
    
  61.     def test_no_headers(self):
    
  62.         """Absence of Accept header defaults to '*/*'."""
    
  63.         request = HttpRequest()
    
  64.         self.assertEqual(
    
  65.             [str(accepted_type) for accepted_type in request.accepted_types],
    
  66.             ["*/*"],
    
  67.         )
    
  68. 
    
  69.     def test_accept_headers(self):
    
  70.         request = HttpRequest()
    
  71.         request.META[
    
  72.             "HTTP_ACCEPT"
    
  73.         ] = "text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8"
    
  74.         self.assertEqual(
    
  75.             [str(accepted_type) for accepted_type in request.accepted_types],
    
  76.             [
    
  77.                 "text/html",
    
  78.                 "application/xhtml+xml",
    
  79.                 "application/xml; q=0.9",
    
  80.                 "*/*; q=0.8",
    
  81.             ],
    
  82.         )
    
  83. 
    
  84.     def test_request_accepts_any(self):
    
  85.         request = HttpRequest()
    
  86.         request.META["HTTP_ACCEPT"] = "*/*"
    
  87.         self.assertIs(request.accepts("application/json"), True)
    
  88. 
    
  89.     def test_request_accepts_none(self):
    
  90.         request = HttpRequest()
    
  91.         request.META["HTTP_ACCEPT"] = ""
    
  92.         self.assertIs(request.accepts("application/json"), False)
    
  93.         self.assertEqual(request.accepted_types, [])
    
  94. 
    
  95.     def test_request_accepts_some(self):
    
  96.         request = HttpRequest()
    
  97.         request.META[
    
  98.             "HTTP_ACCEPT"
    
  99.         ] = "text/html,application/xhtml+xml,application/xml;q=0.9"
    
  100.         self.assertIs(request.accepts("text/html"), True)
    
  101.         self.assertIs(request.accepts("application/xhtml+xml"), True)
    
  102.         self.assertIs(request.accepts("application/xml"), True)
    
  103.         self.assertIs(request.accepts("application/json"), False)