Merge pull request #4973 from tomchristie/support-head-in-viewsets

Support HEAD in viewsets
This commit is contained in:
Tom Christie 2017-03-13 13:16:04 +00:00 committed by GitHub
commit 3b466fabe7
3 changed files with 21 additions and 0 deletions

View File

@ -79,6 +79,9 @@ class ViewSetMixin(object):
handler = getattr(self, action) handler = getattr(self, action)
setattr(self, method, handler) setattr(self, method, handler)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
# And continue as usual # And continue as usual
return self.dispatch(request, *args, **kwargs) return self.dispatch(request, *args, **kwargs)

View File

@ -101,6 +101,15 @@ class TestRootView(TestCase):
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert response.data == self.data 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): def test_post_root_view(self):
""" """
POST requests to ListCreateAPIView should create a new object. POST requests to ListCreateAPIView should create a new object.

View File

@ -24,6 +24,15 @@ class InitializeViewSetsTestCase(TestCase):
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert response.data == {'ACTION': 'LIST'} 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): def test_initialize_view_set_with_empty_actions(self):
try: try:
BasicViewSet.as_view() BasicViewSet.as_view()