test_common.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from asgiref.sync import iscoroutinefunction
  2. from django.http import HttpRequest, HttpResponse
  3. from django.test import SimpleTestCase
  4. from django.views.decorators.common import no_append_slash
  5. class NoAppendSlashTests(SimpleTestCase):
  6. def test_wrapped_sync_function_is_not_coroutine_function(self):
  7. def sync_view(request):
  8. return HttpResponse()
  9. wrapped_view = no_append_slash(sync_view)
  10. self.assertIs(iscoroutinefunction(wrapped_view), False)
  11. def test_wrapped_async_function_is_coroutine_function(self):
  12. async def async_view(request):
  13. return HttpResponse()
  14. wrapped_view = no_append_slash(async_view)
  15. self.assertIs(iscoroutinefunction(wrapped_view), True)
  16. def test_no_append_slash_decorator(self):
  17. @no_append_slash
  18. def sync_view(request):
  19. return HttpResponse()
  20. self.assertIs(sync_view.should_append_slash, False)
  21. self.assertIsInstance(sync_view(HttpRequest()), HttpResponse)
  22. async def test_no_append_slash_decorator_async_view(self):
  23. @no_append_slash
  24. async def async_view(request):
  25. return HttpResponse()
  26. self.assertIs(async_view.should_append_slash, False)
  27. self.assertIsInstance(await async_view(HttpRequest()), HttpResponse)