Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #31949 -- Made @vary_on_(cookie/headers) decorators work with as…
…ync functions.
- Loading branch information
1 parent
fb1c763
commit b7a17b0
Showing
5 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from asgiref.sync import iscoroutinefunction | ||
|
||
from django.http import HttpRequest, HttpResponse | ||
from django.test import SimpleTestCase | ||
from django.views.decorators.vary import vary_on_cookie, vary_on_headers | ||
|
||
|
||
class VaryOnHeadersTests(SimpleTestCase): | ||
def test_wrapped_sync_function_is_not_coroutine_function(self): | ||
def sync_view(request): | ||
return HttpResponse() | ||
|
||
wrapped_view = vary_on_headers()(sync_view) | ||
self.assertIs(iscoroutinefunction(wrapped_view), False) | ||
|
||
def test_wrapped_async_function_is_coroutine_function(self): | ||
async def async_view(request): | ||
return HttpResponse() | ||
|
||
wrapped_view = vary_on_headers()(async_view) | ||
self.assertIs(iscoroutinefunction(wrapped_view), True) | ||
|
||
def test_vary_on_headers_decorator(self): | ||
@vary_on_headers("Header", "Another-header") | ||
def sync_view(request): | ||
return HttpResponse() | ||
|
||
response = sync_view(HttpRequest()) | ||
self.assertEqual(response.get("Vary"), "Header, Another-header") | ||
|
||
async def test_vary_on_headers_decorator_async_view(self): | ||
@vary_on_headers("Header", "Another-header") | ||
async def async_view(request): | ||
return HttpResponse() | ||
|
||
response = await async_view(HttpRequest()) | ||
self.assertEqual(response.get("Vary"), "Header, Another-header") | ||
|
||
|
||
class VaryOnCookieTests(SimpleTestCase): | ||
def test_wrapped_sync_function_is_not_coroutine_function(self): | ||
def sync_view(request): | ||
return HttpResponse() | ||
|
||
wrapped_view = vary_on_cookie(sync_view) | ||
self.assertIs(iscoroutinefunction(wrapped_view), False) | ||
|
||
def test_wrapped_async_function_is_coroutine_function(self): | ||
async def async_view(request): | ||
return HttpResponse() | ||
|
||
wrapped_view = vary_on_cookie(async_view) | ||
self.assertIs(iscoroutinefunction(wrapped_view), True) | ||
|
||
def test_vary_on_cookie_decorator(self): | ||
@vary_on_cookie | ||
def sync_view(request): | ||
return HttpResponse() | ||
|
||
response = sync_view(HttpRequest()) | ||
self.assertEqual(response.get("Vary"), "Cookie") | ||
|
||
async def test_vary_on_cookie_decorator_async_view(self): | ||
@vary_on_cookie | ||
async def async_view(request): | ||
return HttpResponse() | ||
|
||
response = await async_view(HttpRequest()) | ||
self.assertEqual(response.get("Vary"), "Cookie") |