Skip to content

Commit 3b466fa

Browse files
authored
Merge pull request #4973 from tomchristie/support-head-in-viewsets
Support HEAD in viewsets
2 parents e94011e + 43b3896 commit 3b466fa

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

rest_framework/viewsets.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def view(request, *args, **kwargs):
7979
handler = getattr(self, action)
8080
setattr(self, method, handler)
8181

82+
if hasattr(self, 'get') and not hasattr(self, 'head'):
83+
self.head = self.get
84+
8285
# And continue as usual
8386
return self.dispatch(request, *args, **kwargs)
8487

tests/test_generics.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ def test_get_root_view(self):
101101
assert response.status_code == status.HTTP_200_OK
102102
assert response.data == self.data
103103

104+
def test_head_root_view(self):
105+
"""
106+
HEAD requests to ListCreateAPIView should return 200.
107+
"""
108+
request = factory.head('/')
109+
with self.assertNumQueries(1):
110+
response = self.view(request).render()
111+
assert response.status_code == status.HTTP_200_OK
112+
104113
def test_post_root_view(self):
105114
"""
106115
POST requests to ListCreateAPIView should create a new object.

tests/test_viewsets.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def test_initialize_view_set_with_actions(self):
2424
assert response.status_code == status.HTTP_200_OK
2525
assert response.data == {'ACTION': 'LIST'}
2626

27+
def testhead_request_against_viewset(self):
28+
request = factory.head('/', '', content_type='application/json')
29+
my_view = BasicViewSet.as_view(actions={
30+
'get': 'list',
31+
})
32+
33+
response = my_view(request)
34+
assert response.status_code == status.HTTP_200_OK
35+
2736
def test_initialize_view_set_with_empty_actions(self):
2837
try:
2938
BasicViewSet.as_view()

0 commit comments

Comments
 (0)