Skip to content

Instantly share code, notes, and snippets.

@soimafreak
Created January 9, 2018 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soimafreak/5f3aec2b68964a7344b953f2b53cb57b to your computer and use it in GitHub Desktop.
Save soimafreak/5f3aec2b68964a7344b953f2b53cb57b to your computer and use it in GitHub Desktop.
Hello Dynamic Paths
  1. make setup
  2. make test
  3. cd hello; chalice local
  4. curl http://localhost:8000/hello/world

I have left the static path in the app.py so it can be uncommented and the tests re-run to show it working / failing.

from chalice import Chalice
app = Chalice(app_name='hello')
@app.route('/{hello}')
def index(hello):
return {'hello': hello}
# @app.route('/world')
# def index():
# return {'hello': 'world'}
.PHONY: test setup clean clean-pyc clean-test run
setup:
python -m pip install -e .
clean: clean-pyc clean-test
clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +
test:
pytest -v tests.py
test-lint:
flake8 --count
import sys
from setuptools import setup
if sys.version_info < (3, 6):
sys.exit('Python < 3.6 is not supported')
setup(
name='hello',
packages=['hello'],
version='0.0.1',
include_package_data=True,
install_requires=[
'chalice',
'pytest'
]
)
from pytest import fixture
from hello.app import app
import json
# Module Methods
@fixture
def sample_app():
"""Create a clean application instance."""
return app
@fixture
def create_event():
def create_event_inner(
uri, method, path={}, body=""
):
print("uri: {} method: {}".format(uri, method))
return {
'requestContext': {
'httpMethod': method,
'resourcePath': uri,
},
'headers': {},
'pathParameters': path,
'queryStringParameters': {},
'body': body,
'stageVariables': {},
}
return create_event_inner
def assert_response_body_is(response, body):
assert json.loads(response['body']) == body
def test_index(sample_app, create_event):
event = create_event(
uri='/world',
method='GET'
)
response = sample_app(event, context=None)
assert_response_body_is(
response,
{"hello": "world"}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment