Emanuele Rocca committed 5cbbead

Properly raise Http404 on http methods not handled. Basic tests of Resource error handling.

Comments (0)

Files changed (3)


         if not rm in handler.allowed_methods:
             return HttpResponseNotAllowed(handler.allowed_methods)
-        meth = getattr(handler, self.callmap.get(rm), None)
+        meth = getattr(handler, self.callmap.get(rm, ''), None)
         if not meth:
             raise Http404


         return self.model()
 class EchoHandler(BaseHandler):
-    allowed_methods = ('GET', )
+    allowed_methods = ('GET', 'HEAD')
     @validate(EchoForm, 'GET')
     def read(self, request):


         self.assertEquals(resp.status_code, 200)
         self.assertEquals(resp.content, expect)
+class ErrorHandlingTests(MainTests):
+    """Test proper handling of errors by Resource"""
+    def test_response_not_allowed(self):
+        resp ='/api/echo')
+        self.assertEquals(resp.status_code, 405)
+        self.assertEquals(resp['Allow'], 'GET, HEAD')
+    def test_not_found_because_of_unexpected_http_method(self):
+        # not using self.client.head because it is not present in Django 1.0
+        resp = self.client.get('/api/echo', REQUEST_METHOD='HEAD')
+        self.assertEquals(resp.status_code, 404)
+        self.assertEquals(resp.content, '')
 class Issue58ModelTests(MainTests):
     This testcase addresses #58 in django-piston where if a model
         resp ='/api/issue58.json', outgoing, content_type='application/json',
         self.assertEquals(resp.status_code, 201)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.