Skip to content

Commit

Permalink
Merge branch 'brianjbeach-master'
Browse files Browse the repository at this point in the history
PR #766
Issue #767

* brianjbeach-master:
  Add fix to changelog
  Fix parsing blank query strings in local mode
  • Loading branch information
jamesls committed Apr 10, 2018
2 parents a7e12d3 + 44e5e0a commit 19be95a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Next Release (TBD)

* Add CORS headers to error response
(`#715 <https://github.com/aws/chalice/pull/715>`__)
* Fix parsing empty query strings in local mode
(`#767 <https://github.com/aws/chalice/pull/767>`__)


1.2.0
=====
Expand Down
4 changes: 3 additions & 1 deletion chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def match_route(self, url):
"""
# Otherwise we need to check for param substitution
parsed_url = urlparse(url)
query_params = {k: v[0] for k, v in parse_qs(parsed_url.query).items()}
parsed_qs = parse_qs(parsed_url.query, keep_blank_values=True)
query_params = {k: v[0] for k, v in parsed_qs.items()}
path = parsed_url.path
# API Gateway removes the trailing slash if the route is not the root
# path. We do the same here so our route matching works the same way.
Expand Down Expand Up @@ -171,6 +172,7 @@ def create_lambda_event(self, method, path, headers, body=None):
'identity': {
'sourceIp': self.LOCAL_SOURCE_IP
},
'path': path.split('?')[0],
},
'headers': {k.lower(): v for k, v in headers.items()},
'pathParameters': view_route.captured,
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ def test_can_create_lambda_event():
'requestContext': {
'httpMethod': 'GET',
'resourcePath': '/foo/{capture}',
'path': '/foo/other',
'identity': {
'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP
},
Expand All @@ -546,6 +547,31 @@ def test_can_create_lambda_event():
}


def test_parse_query_string():
converter = local.LambdaEventConverter(
local.RouteMatcher(['/foo/bar', '/foo/{capture}']))
event = converter.create_lambda_event(
method='GET',
path='/foo/other?a=1&b=&c=3',
headers={'content-type': 'application/json'}
)
assert event == {
'requestContext': {
'httpMethod': 'GET',
'resourcePath': '/foo/{capture}',
'path': '/foo/other',
'identity': {
'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP
},
},
'headers': {'content-type': 'application/json'},
'pathParameters': {'capture': 'other'},
'queryStringParameters': {'a': '1', 'b': '', 'c': '3'},
'body': None,
'stageVariables': {},
}


def test_can_create_lambda_event_for_put_request():
converter = local.LambdaEventConverter(
local.RouteMatcher(['/foo/bar', '/foo/{capture}']))
Expand All @@ -559,6 +585,7 @@ def test_can_create_lambda_event_for_put_request():
'requestContext': {
'httpMethod': 'PUT',
'resourcePath': '/foo/{capture}',
'path': '/foo/other',
'identity': {
'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP
},
Expand All @@ -585,6 +612,7 @@ def test_can_create_lambda_event_for_post_with_formencoded_body():
'requestContext': {
'httpMethod': 'POST',
'resourcePath': '/foo/{capture}',
'path': '/foo/other',
'identity': {
'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP
},
Expand Down

0 comments on commit 19be95a

Please sign in to comment.