Skip to content

Support HEAD in viewsets #4973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rest_framework/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def view(request, *args, **kwargs):
handler = getattr(self, action)
setattr(self, method, handler)

if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get

# And continue as usual
return self.dispatch(request, *args, **kwargs)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def test_get_root_view(self):
assert response.status_code == status.HTTP_200_OK
assert response.data == self.data

def test_head_root_view(self):
"""
HEAD requests to ListCreateAPIView should return 200.
"""
request = factory.head('/')
with self.assertNumQueries(1):
response = self.view(request).render()
assert response.status_code == status.HTTP_200_OK

def test_post_root_view(self):
"""
POST requests to ListCreateAPIView should create a new object.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def test_initialize_view_set_with_actions(self):
assert response.status_code == status.HTTP_200_OK
assert response.data == {'ACTION': 'LIST'}

def testhead_request_against_viewset(self):
request = factory.head('/', '', content_type='application/json')
my_view = BasicViewSet.as_view(actions={
'get': 'list',
})

response = my_view(request)
assert response.status_code == status.HTTP_200_OK

def test_initialize_view_set_with_empty_actions(self):
try:
BasicViewSet.as_view()
Expand Down