@@ -173,6 +173,58 @@ def test_dispatch_decoration(self):
173173 """
174174 self .assertTrue (DecoratedDispatchView .as_view ().is_decorated )
175175
176+ def test_head_no_get (self ):
177+ """
178+ Test that a view class with no get responds to a HEAD request with HTTP
179+ 405.
180+ """
181+ request = self .rf .head ('/' )
182+ view = PostOnlyView .as_view ()
183+ self .assertEqual (405 , view (request ).status_code )
184+
185+ def test_options (self ):
186+ """
187+ Test that views respond to HTTP OPTIONS requests with an Allow header
188+ appropriate for the methods implemented by the view class.
189+ """
190+ request = self .rf .options ('/' )
191+ view = SimpleView .as_view ()
192+ response = view (request )
193+ self .assertEqual (200 , response .status_code )
194+ self .assertTrue (response ['Allow' ])
195+
196+ def test_options_for_get_view (self ):
197+ """
198+ Test that a view implementing GET allows GET and HEAD.
199+ """
200+ request = self .rf .options ('/' )
201+ view = SimpleView .as_view ()
202+ response = view (request )
203+ self ._assert_allows (response , 'GET' , 'HEAD' )
204+
205+ def test_options_for_get_and_post_view (self ):
206+ """
207+ Test that a view implementing GET and POST allows GET, HEAD, and POST.
208+ """
209+ request = self .rf .options ('/' )
210+ view = SimplePostView .as_view ()
211+ response = view (request )
212+ self ._assert_allows (response , 'GET' , 'HEAD' , 'POST' )
213+
214+ def test_options_for_post_view (self ):
215+ """
216+ Test that a view implementing POST allows POST.
217+ """
218+ request = self .rf .options ('/' )
219+ view = PostOnlyView .as_view ()
220+ response = view (request )
221+ self ._assert_allows (response , 'POST' )
222+
223+ def _assert_allows (self , response , * expected_methods ):
224+ "Assert allowed HTTP methods reported in the Allow response header"
225+ response_allows = set (response ['Allow' ].split (', ' ))
226+ self .assertEqual (set (expected_methods + ('OPTIONS' ,)), response_allows )
227+
176228
177229class TemplateViewTest (TestCase ):
178230 urls = 'regressiontests.generic_views.urls'
0 commit comments