Skip to content

Commit

Permalink
Validate duplicate routes
Browse files Browse the repository at this point in the history
You know will get:

```
$ chalice deploy
Error: Unable to import your app.py file: Duplicate route detected:
'/foo'
URL paths must be unique.
```

Fixes aws#79.
  • Loading branch information
jamesls committed Aug 1, 2016
1 parent 7d30cd3 commit b4f406b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Next Release
* Improve error validation when routes containing a
trailing ``/`` char
(`#65 <https://github.com/awslabs/chalice/issues/65>`__)
* Validate duplicate route entries
(`#79 <https://github.com/awslabs/chalice/issues/79>`__)
4 changes: 4 additions & 0 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def _add_route(self, path, view_func, **kwargs):
authorizer_id = kwargs.get('authorizer_id', None)
api_key_required = kwargs.get('api_key_required', None)

if path in self.routes:
raise ValueError(
"Duplicate route detected: '%s'\n"
"URL paths must be unique." % path)
self.routes[path] = RouteEntry(
view_func,
name,
Expand Down
9 changes: 8 additions & 1 deletion chalice/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ def load_chalice_app(project_dir):
with open(app_py) as f:
g = {}
contents = f.read()
exec contents in g
try:
exec contents in g
except Exception as e:
exception = click.ClickException(
"Unable to import your app.py file: %s" % e
)
exception.exit_code = 2
raise exception
return g['app']


Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ def index_view():

result = demo(event, context=None)
assert result == {'rawbody': '{"hello": "world"}'}


def test_error_on_duplicate_routes():
demo = app.Chalice('app-name')

@demo.route('/index', methods=['PUT'])
def index_view():
return {'foo': 'bar'}

with pytest.raises(ValueError):
@demo.route('/index', methods=['POST'])
def index_post():
return {'foo': 'bar'}

0 comments on commit b4f406b

Please sign in to comment.