When path arguments are incorrect for RestController, return HTTP 404, not 400.

When RestController encounters a mismatch between function signatures and
positional URL chunks (e.g., /authors/books vs /authors/1/books), it shouldn't
raise an HTTP 400; the issue is a nonexistant URL path, not the structure of
the request/body itself.

Change-Id: I6637f88b7da4f09497f905ebafa7bf1e3788151a
This commit is contained in:
Ryan Petrello
2014-09-24 13:54:23 -04:00
parent d4c230a2c5
commit 548ac35e3b
2 changed files with 12 additions and 10 deletions

View File

@@ -54,7 +54,10 @@ class RestController(object):
request.pecan.get('routing_args', [])
)
if len(remainder) < fixed_args:
abort(400)
# For controllers that are missing intermediate IDs
# (e.g., /authors/books vs /authors/1/books), return a 404 for an
# invalid path.
abort(404)
@expose()
def _route(self, args, request=None):

View File

@@ -727,11 +727,11 @@ class TestRestController(PecanTestCase):
assert r.status_int == 200
assert r.body == b_('4')
r = app.get('/foos/bars/', status=400)
assert r.status_int == 400
r = app.get('/foos/bars/', status=404)
assert r.status_int == 404
r = app.get('/foos/bars/1', status=400)
assert r.status_int == 400
r = app.get('/foos/bars/1', status=404)
assert r.status_int == 404
def test_nested_get_all_with_lookup(self):
@@ -783,10 +783,9 @@ class TestRestController(PecanTestCase):
assert r.status_int == 200
assert r.body == b_('4')
r = app.get('/foos/bars/', status=400)
assert r.status_int == 400
r = app.get('/foos/bars/', status=400)
r = app.get('/foos/bars/')
assert r.status_int == 302
assert r.headers['Location'].endswith('/lookup-hit/')
r = app.get('/foos/bars/1')
assert r.status_int == 302
@@ -893,7 +892,7 @@ class TestRestController(PecanTestCase):
self.assertEqual(r.body, b_(dumps(dict(items=BarsController.data[1]))))
r = app.get('/foos/bars', expect_errors=True)
self.assertEqual(r.status_int, 400)
self.assertEqual(r.status_int, 404)
def test_custom_with_trailing_slash(self):