Skip to content

Commit

Permalink
Merge branch 'JamieCressey-http_response_codes'
Browse files Browse the repository at this point in the history
Closes #37.

* JamieCressey-http_response_codes:
  Add test for new error codes
  Adding support for common RESTful HTTP response codes
  • Loading branch information
jamesls committed Jul 28, 2016
2 parents 11f6937 + 8960723 commit fef8f13
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 78 deletions.
28 changes: 26 additions & 2 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,39 @@ class BadRequestError(ChaliceViewError):
STATUS_CODE = 400


class UnauthorizedError(ChaliceViewError):
STATUS_CODE = 401


class ForbiddenError(ChaliceViewError):
STATUS_CODE = 403


class NotFoundError(ChaliceViewError):
STATUS_CODE = 404


class ConflictError(ChaliceViewError):
STATUS_CODE = 409


class TooManyRequestsError(ChaliceViewError):
STATUS_CODE = 429


ALL_ERRORS = [
ChaliceViewError, BadRequestError, NotFoundError
]
ChaliceViewError,
BadRequestError,
NotFoundError,
UnauthorizedError,
ForbiddenError,
ConflictError,
TooManyRequestsError]


class Request(object):
"""The current request from API gateway."""

def __init__(self, query_params, headers, uri_params, method, body,
base64_body, context, stage_vars):
self.query_params = query_params
Expand Down Expand Up @@ -66,6 +88,7 @@ def to_dict(self):


class RouteEntry(object):

def __init__(self, view_function, view_name, path, methods):
self.view_function = view_function
self.view_name = view_name
Expand Down Expand Up @@ -93,6 +116,7 @@ def __eq__(self, other):


class Chalice(object):

def __init__(self, app_name):
self.app_name = app_name
self.routes = {}
Expand Down
107 changes: 31 additions & 76 deletions tests/unit/test_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from chalice.deployer import APIGatewayResourceCreator
from chalice.deployer import FULL_PASSTHROUGH, ERROR_MAPPING
from chalice.deployer import ResourceQuery
from chalice.app import RouteEntry
from chalice.app import RouteEntry, ALL_ERRORS

from botocore.stub import Stubber
import botocore.session
Expand Down Expand Up @@ -93,6 +93,34 @@ def test_multiple_routes_on_single_spine():
)


def add_expected_calls_to_map_error(error_cls, gateway_stub):
gateway_stub.add_response(
'put_integration_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseTemplates': {'application/json': ERROR_MAPPING},
'restApiId': 'rest-api-id',
'selectionPattern': '%s.*' % error_cls.__name__,
'statusCode': str(error_cls.STATUS_CODE),
}
)
gateway_stub.add_response(
'put_method_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseModels': {
'application/json': 'Empty',
},
'restApiId': 'rest-api-id',
'statusCode': str(error_cls.STATUS_CODE),
}
)


def test_can_build_resource_routes_for_single_view(stubbed_api_gateway, stubbed_lambda):
route_trie = {
'name': '',
Expand Down Expand Up @@ -164,81 +192,8 @@ def test_can_build_resource_routes_for_single_view(stubbed_api_gateway, stubbed_
'statusCode': '200',
}
)
gateway_stub.add_response(
'put_integration_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseTemplates': {'application/json': ERROR_MAPPING},
'restApiId': 'rest-api-id',
'selectionPattern': 'ChaliceViewError.*',
'statusCode': '500'
}
)
gateway_stub.add_response(
'put_method_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseModels': {
'application/json': 'Empty',
},
'restApiId': 'rest-api-id',
'statusCode': '500',
}
)
gateway_stub.add_response(
'put_integration_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseTemplates': {'application/json': ERROR_MAPPING},
'restApiId': 'rest-api-id',
'selectionPattern': 'BadRequestError.*',
'statusCode': '400'
}
)
gateway_stub.add_response(
'put_method_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseModels': {
'application/json': 'Empty',
},
'restApiId': 'rest-api-id',
'statusCode': '400',
}
)
gateway_stub.add_response(
'put_integration_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseTemplates': {'application/json': ERROR_MAPPING},
'restApiId': 'rest-api-id',
'selectionPattern': 'NotFoundError.*',
'statusCode': '404',
}
)
gateway_stub.add_response(
'put_method_response',
service_response={},
expected_params={
'httpMethod': 'POST',
'resourceId': 'parent-id',
'responseModels': {
'application/json': 'Empty',
},
'restApiId': 'rest-api-id',
'statusCode': '404',
}
)
for error_cls in ALL_ERRORS:
add_expected_calls_to_map_error(error_cls, gateway_stub)
lambda_stub.add_response(
'add_permission',
service_response={},
Expand Down

0 comments on commit fef8f13

Please sign in to comment.