diff --git a/rest_framework/viewsets.py b/rest_framework/viewsets.py index a9dc632ff..70fe72f84 100644 --- a/rest_framework/viewsets.py +++ b/rest_framework/viewsets.py @@ -79,6 +79,9 @@ class ViewSetMixin(object): 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) diff --git a/tests/test_generics.py b/tests/test_generics.py index 59278572e..655bcf325 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -101,6 +101,15 @@ class TestRootView(TestCase): 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. diff --git a/tests/test_viewsets.py b/tests/test_viewsets.py index a3c4e6be5..78ad24dea 100644 --- a/tests/test_viewsets.py +++ b/tests/test_viewsets.py @@ -24,6 +24,15 @@ class InitializeViewSetsTestCase(TestCase): 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()