Skip to content

Commit

Permalink
Properly raise Http404 on http methods not handled. Basic tests of Re…
Browse files Browse the repository at this point in the history
…source error handling.
  • Loading branch information
ema committed Jul 15, 2010
1 parent 3b386d1 commit 3371a79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 1 addition & 2 deletions piston/resource.py
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_project/apps/testapp/handlers.py
Expand Up @@ -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):
Expand Down
16 changes: 15 additions & 1 deletion tests/test_project/apps/testapp/tests.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 3371a79

Please sign in to comment.