Commits

Anonymous committed 96bda59 Merge

Allow HEAD method

Comments (0)

Files changed (4)

djangorestframework/compat.py

 # django.views.generic.View (Django >= 1.3)
 try:
     from django.views.generic import View
+    if not hasattr(View, 'head'):
+        # First implementation of Django class-based views did not include head method 
+        # in base View class - https://code.djangoproject.com/ticket/15688
+        class ViewPlusHead(View):
+            def head(self, request, *args, **kwargs):
+                return self.get(request, *args, **kwargs)
+        View = ViewPlusHead
+        
 except ImportError:
     from django import http
     from django.utils.functional import update_wrapper
             #)
             return http.HttpResponseNotAllowed(allowed_methods)
 
+        def head(self, request, *args, **kwargs):
+            return self.get(request, *args, **kwargs)
 
 try:
     import markdown
         return md.convert(text)
 
 except ImportError:
-    apply_markdown = None
+    apply_markdown = None

djangorestframework/tests/content.py

 from djangorestframework.mixins import RequestMixin
 from djangorestframework.parsers import FormParser, MultiPartParser, PlainTextParser
 
-
 class TestContentParsing(TestCase):
     def setUp(self):
         self.req = RequestFactory()
         view.request = self.req.get('/')
         self.assertEqual(view.DATA, None)
 
+    def ensure_determines_no_content_HEAD(self, view):
+        """Ensure view.DATA returns None for HEAD request."""
+        view.request = self.req.head('/')
+        self.assertEqual(view.DATA, None)
+
     def ensure_determines_form_content_POST(self, view):
         """Ensure view.DATA returns content for POST request with form content."""
         form_data = {'qwerty': 'uiop'}
         """Ensure view.DATA returns None for GET request with no content."""
         self.ensure_determines_no_content_GET(RequestMixin())
 
+    def test_standard_behaviour_determines_no_content_HEAD(self):
+        """Ensure view.DATA returns None for HEAD request."""
+        self.ensure_determines_no_content_HEAD(RequestMixin())
+
     def test_standard_behaviour_determines_form_content_POST(self):
         """Ensure view.DATA returns content for POST request with form content."""
         self.ensure_determines_form_content_POST(RequestMixin())

djangorestframework/tests/methods.py

         view = RequestMixin()
         view.request = self.req.post('/', {view._METHOD_PARAM: 'DELETE'})
         self.assertEqual(view.method, 'DELETE')
+
+    def test_HEAD_is_a_valid_method(self):
+        """HEAD requests identified"""
+        view = RequestMixin()
+        view.request = self.req.head('/')
+        self.assertEqual(view.method, 'HEAD')

djangorestframework/tests/renderers.py

         self.assertEquals(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
         self.assertEquals(resp.status_code, DUMMYSTATUS)
 
+    def test_head_method_serializes_no_content(self):
+        """No response must be included in HEAD requests."""
+        resp = self.client.head('/')
+        self.assertEquals(resp.status_code, DUMMYSTATUS)
+        self.assertEquals(resp['Content-Type'], RendererA.media_type)
+        self.assertEquals(resp.content, '')
+
     def test_default_renderer_serializes_content_on_accept_any(self):
         """If the Accept header is set to */* the default renderer should serialize the response."""
         resp = self.client.get('/', HTTP_ACCEPT='*/*')
         resp = self.client.get('/', HTTP_ACCEPT='foo/bar')
         self.assertEquals(resp.status_code, 406)
 
-
-
 _flat_repr = '{"foo": ["bar", "baz"]}'
 
 _indented_repr = """{