Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only accept known locations in request.errors.add() (fixes #99) #374

Merged
merged 2 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cornice/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def add(self, location, name=None, description=None, **kw):
"""Registers a new error."""
allowed = ('body', 'querystring', 'url', 'header', 'path',
'cookies', 'method')
if location not in allowed:
if location != '' and location not in allowed:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want to accept empty location?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want to accept empty location?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what colander gives when you raise a validation error that is not related to a specific node (eg. provide param either via header or querystring)

raise ValueError('%r not in %s' % (location, allowed))

self.append(dict(
Expand Down
11 changes: 11 additions & 0 deletions cornice/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ class TestErrorsHelper(TestCase):
def setUp(self):
self.errors = Errors()

def test_add_to_supported_location(self):
self.errors.add('')
self.errors.add('body', description='!')
self.errors.add('querystring', name='field')
self.errors.add('url')
self.errors.add('header')
self.errors.add('path')
self.errors.add('cookies')
self.errors.add('method')
self.assertEqual(len(self.errors), 8)

def test_raises_an_exception_when_location_is_unsupported(self):
with self.assertRaises(ValueError):
self.errors.add('something')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests for each accepted parameters?