Skip to content

Commit

Permalink
Accept query parameters when running locally
Browse files Browse the repository at this point in the history
  • Loading branch information
atomaka committed Dec 12, 2016
1 parent afc3457 commit 1b6a66c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import json
import functools
import urlparse
from collections import namedtuple
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
Expand Down Expand Up @@ -68,6 +69,12 @@ def __init__(self, route_matcher):

def create_lambda_event(self, method, path, headers, body=None):
# type: (str, str, Dict[str, str], str) -> EventType
if '?' in path:
path, querystring = path.split("?")
parsed_querystring = urlparse.parse_qsl(querystring)
else:
parsed_querystring = {}

view_route = self._route_matcher.match_route(path)
if body is None:
body = '{}'
Expand All @@ -84,7 +91,7 @@ def create_lambda_event(self, method, path, headers, body=None):
'params': {
'header': dict(headers),
'path': view_route.captured,
'querystring': {},
'querystring': dict(parsed_querystring),
},
'body-json': json_body,
'base64-body': base64_body,
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def sample_app():

@demo.route('/index', methods=['GET'])
def index():
return {'hello': 'world'}
name = demo.current_request.query_params.get('name')
if name is None:
return {'hello': 'world'}
else:
return {'hello': name}

@demo.route('/names/{name}', methods=['GET'])
def name(name):
Expand Down Expand Up @@ -96,6 +100,10 @@ def test_can_route_url_params(handler):
assert _get_body_from_response_stream(handler) == {
'provided-name': 'james'}

def test_can_route_with_query_string(handler):
set_current_request(handler, method='GET', path='/index?name=james')
handler.do_GET()
assert _get_body_from_response_stream(handler) == {'hello': 'james'}

def test_can_route_put_with_body(handler):
body = '{"foo": "bar"}'
Expand Down

0 comments on commit 1b6a66c

Please sign in to comment.