diff --git a/composer/tests/test_middleware.py b/composer/tests/test_middleware.py index 0c1465a..a8bddfd 100644 --- a/composer/tests/test_middleware.py +++ b/composer/tests/test_middleware.py @@ -10,7 +10,7 @@ class MiddleWareTestCase(TestCase): @classmethod def setUpTestData(cls): super(MiddleWareTestCase, cls).setUpTestData() - cls.slot = Slot.objects.create(slot_name="content", url="/four-o-four/") + cls.slot = Slot.objects.create(slot_name="content", url="/not-a-four-o-four/") cls.slot.sites = Site.objects.all() cls.slot.save() cls.tile = Tile.objects.create( @@ -21,7 +21,7 @@ def setUpTestData(cls): cls.tile.save() def test_404(self): - response = self.client.get("/four-o-four/") + response = self.client.get("/not-a-four-o-four/") self.assertHTMLEqual(""" """ % self.tile.id, response.content) def test_404_no_slash(self): - response = self.client.get("/four-o-four") + response = self.client.get("/not-a-four-o-four") self.assertRedirects( response, - "/four-o-four/", + "/not-a-four-o-four/", status_code=301, target_status_code=200, fetch_redirect_response=True @@ -51,11 +51,54 @@ def test_404_no_slash(self): def test_404_no_slash_no_redirect(self): with self.settings(APPEND_SLASH=False): - response = self.client.get("/four-o-four") + response = self.client.get("/not-a-four-o-four") self.assertEqual(response.status_code, 404) - def test_request_method_options(self): - response = self.client.options("/four-o-four/") + def test_request_method_types(self): + # options + response = self.client.options("/not-a-four-o-four/") self.assertEqual(response.status_code, 200) response = self.client.options("/does-not-exist/") self.assertEqual(response.status_code, 404) + + # put + response = self.client.put("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.put("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + #patch + response = self.client.patch("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.patch("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # get + response = self.client.get("/does-not-exist/") + self.assertEqual(response.status_code, 404) + response = self.client.get("/not-a-four-o-four/") + self.assertEqual(response.status_code, 200) + + # post + response = self.client.post("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.post("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # delete + response = self.client.delete("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.delete("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # head + response = self.client.head("/does-not-exist/") + self.assertEqual(response.status_code, 404) + response = self.client.head("/not-a-four-o-four/") + self.assertEqual(response.status_code, 200) + + # trace + response = self.client.trace("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.trace("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) diff --git a/composer/views.py b/composer/views.py index 46ffa45..095b996 100644 --- a/composer/views.py +++ b/composer/views.py @@ -11,6 +11,19 @@ class SlotView(DetailView): model = Slot + def dispatch(self, request, *args, **kwargs): + # Always return the get method's response, except if this view manages + # to trigger the method not allowed code path. + handler = super(SlotView, self).dispatch(request, *args, **kwargs) + + # The method not allowed path will come back with a 405 "Method Not + # Allowed" status. This is checked for and should proceed as default + # implementation dictates. + if handler.status_code != 405: + return self.get(request, *args, **kwargs) + else: + return handler + def get_object(self): # Return the slot based on the path url = self.request.path_info @@ -19,9 +32,3 @@ def get_object(self): url=self.request.path_info, slot_name="content" ) - - def post(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - def options(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs)