Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Properly raise Http404 on http methods not handled. Basic tests of Re…

…source error handling.
  • Loading branch information...
commit 3371a79cd0bf94e4549c262b3428f0e4db4e6632 1 parent 3b386d1
@ema ema authored
View
3  piston/resource.py
@@ -146,8 +146,7 @@ def __call__(self, request, *args, **kwargs):
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
View
2  tests/test_project/apps/testapp/handlers.py
@@ -70,7 +70,7 @@ def read(self, request):
return self.model()
class EchoHandler(BaseHandler):
- allowed_methods = ('GET', )
+ allowed_methods = ('GET', 'HEAD')
@validate(EchoForm, 'GET')
def read(self, request):
View
16 tests/test_project/apps/testapp/tests.py
@@ -423,6 +423,21 @@ def test_multiple_items(self):
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 = self.client.post('/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
@@ -458,4 +473,3 @@ def test_incoming_json(self):
resp = self.client.post('/api/issue58.json', outgoing, content_type='application/json',
HTTP_AUTHORIZATION=self.auth_string)
self.assertEquals(resp.status_code, 201)
-
Please sign in to comment.
Something went wrong with that request. Please try again.