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

Accept query parameters when running locally #197

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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 = dict(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': 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