1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Added more tests for http decorators.

This commit is contained in:
th3nn3ss 2023-06-26 09:12:57 +02:00 committed by Mariusz Felisiak
parent 865a6650d0
commit cd4c4c1905

View File

@ -1,6 +1,27 @@
import datetime
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
from django.test import SimpleTestCase
from django.views.decorators.http import require_safe
from django.views.decorators.http import condition, require_http_methods, require_safe
class RequireHttpMethodsTest(SimpleTestCase):
def test_require_http_methods_methods(self):
@require_http_methods(["GET", "PUT"])
def my_view(request):
return HttpResponse("OK")
request = HttpRequest()
request.method = "GET"
self.assertIsInstance(my_view(request), HttpResponse)
request.method = "PUT"
self.assertIsInstance(my_view(request), HttpResponse)
request.method = "HEAD"
self.assertIsInstance(my_view(request), HttpResponseNotAllowed)
request.method = "POST"
self.assertIsInstance(my_view(request), HttpResponseNotAllowed)
request.method = "DELETE"
self.assertIsInstance(my_view(request), HttpResponseNotAllowed)
class RequireSafeDecoratorTest(SimpleTestCase):
@ -20,3 +41,30 @@ class RequireSafeDecoratorTest(SimpleTestCase):
self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
request.method = "DELETE"
self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
class ConditionDecoratorTest(SimpleTestCase):
def etag_func(request, *args, **kwargs):
return '"b4246ffc4f62314ca13147c9d4f76974"'
def latest_entry(request, *args, **kwargs):
return datetime.datetime(2023, 1, 2, 23, 21, 47)
def test_condition_decorator(self):
@condition(
etag_func=self.etag_func,
last_modified_func=self.latest_entry,
)
def my_view(request):
return HttpResponse()
request = HttpRequest()
request.method = "GET"
response = my_view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["ETag"], '"b4246ffc4f62314ca13147c9d4f76974"')
self.assertEqual(
response.headers["Last-Modified"],
"Mon, 02 Jan 2023 23:21:47 GMT",
)