Skip to content

Commit

Permalink
Set raw_body to None if no HTTP body was sent
Browse files Browse the repository at this point in the history
This also includes an update the local.py so we get
consistent behavior when no HTTP body is provided.

Fixes aws#503.
  • Loading branch information
jamesls committed Aug 31, 2017
1 parent 0c2fa06 commit d7caed4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Next Release (TBD)
* Fix issue where ``api_gateway_stage`` was being
ignored when set in the ``config.json`` file
(`#495 <https://github.com/aws/chalice/issues/495>`__)
* Fix bug where ``raw_body`` would raise an exception if no HTTP
body was provided
(`#503 <https://github.com/aws/chalice/issues/503>`__)


1.0.1
Expand Down
2 changes: 1 addition & 1 deletion chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _base64decode(self, encoded):

@property
def raw_body(self):
if self._raw_body is None:
if self._raw_body is None and self._body is not None:
if self._is_base64_encoded:
self._raw_body = self._base64decode(self._body)
elif not isinstance(self._body, bytes):
Expand Down
4 changes: 1 addition & 3 deletions chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def create_lambda_event(self, method, path, headers, body=None):
'pathParameters': view_route.captured,
'stageVariables': {},
}
if body is None:
event['body'] = '{}'
elif self._is_binary(headers):
if self._is_binary(headers) and body is not None:
event['body'] = base64.b64encode(body).decode('ascii')
event['isBase64Encoded'] = True
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ def test_can_use_shared_auth(smoke_test_app):
assert context['authorizer']['foo'] == 'bar'


def test_empty_raw_body(smoke_test_app):
url = smoke_test_app.url + '/repr-raw-body'
response = requests.post(url)
response.raise_for_status()
assert response.json() == {'repr-raw-body': 'None'}


@pytest.mark.on_redeploy
def test_redeploy_no_change_view(smoke_test_app):
smoke_test_app.redeploy_once()
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/testapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,8 @@ def fake_profile_read_only():
methods=['POST'])
def fake_profile_post():
return {'success': True, 'context': app.current_request.context}


@app.route('/repr-raw-body', methods=['POST'])
def repr_raw_body():
return {'repr-raw-body': repr(app.current_request.raw_body)}
14 changes: 14 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,3 +1278,17 @@ def test_debug_mode_changes_log_level():
test_app.debug = True
assert test_app.debug is True
assert test_app.log.getEffectiveLevel() == logging.DEBUG


def test_raw_body_is_none_if_body_is_none():
misc_kwargs = {
'query_params': {},
'headers': {},
'uri_params': {},
'method': 'GET',
'context': {},
'stage_vars': {},
'is_base64_encoded': False,
}
request = app.Request(body=None, **misc_kwargs)
assert request.raw_body is None
2 changes: 1 addition & 1 deletion tests/unit/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_can_create_lambda_event():
'headers': {'content-type': 'application/json'},
'pathParameters': {'capture': 'other'},
'queryStringParameters': {},
'body': '{}',
'body': None,
'stageVariables': {},
}

Expand Down

0 comments on commit d7caed4

Please sign in to comment.