test_accept_header.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from unittest import TestCase
  2. from django.http import HttpRequest
  3. from django.http.request import MediaType
  4. class MediaTypeTests(TestCase):
  5. def test_empty(self):
  6. for empty_media_type in (None, ''):
  7. with self.subTest(media_type=empty_media_type):
  8. media_type = MediaType(empty_media_type)
  9. self.assertIs(media_type.is_all_types, False)
  10. self.assertEqual(str(media_type), '')
  11. self.assertEqual(repr(media_type), '<MediaType: >')
  12. def test_str(self):
  13. self.assertEqual(str(MediaType('*/*; q=0.8')), '*/*; q=0.8')
  14. self.assertEqual(str(MediaType('application/xml')), 'application/xml')
  15. def test_repr(self):
  16. self.assertEqual(repr(MediaType('*/*; q=0.8')), '<MediaType: */*; q=0.8>')
  17. self.assertEqual(
  18. repr(MediaType('application/xml')),
  19. '<MediaType: application/xml>',
  20. )
  21. def test_is_all_types(self):
  22. self.assertIs(MediaType('*/*').is_all_types, True)
  23. self.assertIs(MediaType('*/*; q=0.8').is_all_types, True)
  24. self.assertIs(MediaType('text/*').is_all_types, False)
  25. self.assertIs(MediaType('application/xml').is_all_types, False)
  26. def test_match(self):
  27. tests = [
  28. ('*/*; q=0.8', '*/*'),
  29. ('*/*', 'application/json'),
  30. (' */* ', 'application/json'),
  31. ('application/*', 'application/json'),
  32. ('application/xml', 'application/xml'),
  33. (' application/xml ', 'application/xml'),
  34. ('application/xml', ' application/xml '),
  35. ]
  36. for accepted_type, mime_type in tests:
  37. with self.subTest(accepted_type, mime_type=mime_type):
  38. self.assertIs(MediaType(accepted_type).match(mime_type), True)
  39. def test_no_match(self):
  40. tests = [
  41. (None, '*/*'),
  42. ('', '*/*'),
  43. ('; q=0.8', '*/*'),
  44. ('application/xml', 'application/html'),
  45. ('application/xml', '*/*'),
  46. ]
  47. for accepted_type, mime_type in tests:
  48. with self.subTest(accepted_type, mime_type=mime_type):
  49. self.assertIs(MediaType(accepted_type).match(mime_type), False)
  50. class AcceptHeaderTests(TestCase):
  51. def test_no_headers(self):
  52. """Absence of Accept header defaults to '*/*'."""
  53. request = HttpRequest()
  54. self.assertEqual(
  55. [str(accepted_type) for accepted_type in request.accepted_types],
  56. ['*/*'],
  57. )
  58. def test_accept_headers(self):
  59. request = HttpRequest()
  60. request.META['HTTP_ACCEPT'] = (
  61. 'text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8'
  62. )
  63. self.assertEqual(
  64. [str(accepted_type) for accepted_type in request.accepted_types],
  65. [
  66. 'text/html',
  67. 'application/xhtml+xml',
  68. 'application/xml; q=0.9',
  69. '*/*; q=0.8',
  70. ],
  71. )
  72. def test_request_accepts_any(self):
  73. request = HttpRequest()
  74. request.META['HTTP_ACCEPT'] = '*/*'
  75. self.assertIs(request.accepts('application/json'), True)
  76. def test_request_accepts_none(self):
  77. request = HttpRequest()
  78. request.META['HTTP_ACCEPT'] = ''
  79. self.assertIs(request.accepts('application/json'), False)
  80. self.assertEqual(request.accepted_types, [])
  81. def test_request_accepts_some(self):
  82. request = HttpRequest()
  83. request.META['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;q=0.9'
  84. self.assertIs(request.accepts('text/html'), True)
  85. self.assertIs(request.accepts('application/xhtml+xml'), True)
  86. self.assertIs(request.accepts('application/xml'), True)
  87. self.assertIs(request.accepts('application/json'), False)