def test_can_create_lambda_event_for_put_request(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) event = converter.create_lambda_event( method='PUT', path='/foo/other', headers={'content-type': 'application/json'}, body='{"foo": "bar"}', ) assert event == { 'requestContext': { 'httpMethod': 'PUT', 'resourcePath': '/foo/{capture}', 'path': '/foo/other', 'identity': { 'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP }, }, 'headers': { 'content-type': 'application/json' }, 'pathParameters': { 'capture': 'other' }, 'multiValueQueryStringParameters': None, 'body': '{"foo": "bar"}', 'stageVariables': {}, }
def test_can_create_lambda_event_for_post_with_formencoded_body(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) form_body = 'foo=bar&baz=qux' event = converter.create_lambda_event( method='POST', path='/foo/other', headers={'content-type': 'application/x-www-form-urlencoded'}, body=form_body, ) assert event == { 'requestContext': { 'httpMethod': 'POST', 'resourcePath': '/foo/{capture}', 'path': '/foo/other', 'identity': { 'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP }, }, 'headers': {'content-type': 'application/x-www-form-urlencoded'}, 'pathParameters': {'capture': 'other'}, 'queryStringParameters': None, 'body': form_body, 'stageVariables': {}, }
def test_can_create_lambda_event_for_post_with_formencoded_body(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) form_body = 'foo=bar&baz=qux' event = converter.create_lambda_event( method='POST', path='/foo/other', headers={'content-type': 'application/x-www-form-urlencoded'}, body=form_body, ) assert event == { 'claims': {}, 'context': { 'http-method': 'POST', 'resource-path': '/foo/{capture}', }, 'params': { 'header': { 'content-type': 'application/x-www-form-urlencoded' }, 'path': { 'capture': 'other' }, 'querystring': {}, }, 'body-json': {}, 'base64-body': form_body.encode('base64'), 'stage-variables': {}, }
def test_parse_query_string(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) event = converter.create_lambda_event( method='GET', path='/foo/other?a=1&b=&c=3', headers={'content-type': 'application/json'}) assert event == { 'requestContext': { 'httpMethod': 'GET', 'resourcePath': '/foo/{capture}', 'path': '/foo/other', 'identity': { 'sourceIp': local.LambdaEventConverter.LOCAL_SOURCE_IP }, }, 'headers': { 'content-type': 'application/json' }, 'pathParameters': { 'capture': 'other' }, 'multiValueQueryStringParameters': { 'a': ['1'], 'b': [''], 'c': ['3'] }, 'body': None, 'stageVariables': {}, }
def test_can_create_lambda_event_for_put_request(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) event = converter.create_lambda_event( method='PUT', path='/foo/other', headers={'content-type': 'application/json'}, body='{"foo": "bar"}', ) assert event == { 'claims': {}, 'context': { 'http-method': 'PUT', 'resource-path': '/foo/{capture}', }, 'params': { 'header': { 'content-type': 'application/json' }, 'path': { 'capture': 'other' }, 'querystring': {}, }, 'body-json': { 'foo': 'bar' }, 'base64-body': json.dumps({ 'foo': 'bar' }).encode("base64"), 'stage-variables': {}, }
def test_lambda_event_contains_source_ip(): converter = local.LambdaEventConverter(local.RouteMatcher(['/foo/bar'])) event = converter.create_lambda_event( method='GET', path='/foo/bar', headers={'content-type': 'application/json'}) source_ip = event.get('requestContext').get('identity').get('sourceIp') assert source_ip == local.LambdaEventConverter.LOCAL_SOURCE_IP
def test_can_match_exact_route(actual_url, matched_url): matcher = local.RouteMatcher([ '/foo', '/foo/{capture}', '/foo/bar', '/names/{capture}', '/a/{capture}/c', '/a/b/c' ]) if matched_url is not None: assert matcher.match_route(actual_url).route == matched_url else: with pytest.raises(ValueError): matcher.match_route(actual_url)
def test_lambda_event_deny_error_local_request_context(): converter = local.LambdaEventConverter(local.RouteMatcher(['/foo/bar'])) with pytest.raises(InvaldLocalConextHeader): converter.create_lambda_event( method='GET', path='/foo/bar', headers={ 'content-type': 'application/json', 'local-request-context': '{"identity":{"cognitoIdentityId":**wrongformat**"test"}}' })
def test_json_body_mapped_when_content_type_matches(content_type, is_json): converter = local.LambdaEventConverter(local.RouteMatcher(['/'])) json_body = '{"json": "body"}' event = converter.create_lambda_event( method='POST', path='/', headers={'content-type': content_type}, body=json_body ) if is_json: assert event['body-json'] == {'json': 'body'} else: assert event['body-json'] == {}
def test_lambda_event_contains_local_request_context(): converter = local.LambdaEventConverter(local.RouteMatcher(['/foo/bar'])) event = converter.create_lambda_event( method='GET', path='/foo/bar', headers={ 'content-type': 'application/json', 'local-request-context': '{"identity":{"cognitoIdentityId":"test"}}' }) cognito_identity_id = event.get('requestContext').\ get('identity', {}).get('cognitoIdentityId') assert cognito_identity_id == 'test'
def test_can_create_lambda_event(): converter = local.LambdaEventConverter( local.RouteMatcher(['/foo/bar', '/foo/{capture}'])) event = converter.create_lambda_event( method='GET', path='/foo/other', headers={'content-type': 'application/json'} ) assert event == { 'requestContext': { 'httpMethod': 'GET', 'resourcePath': '/foo/{capture}', }, 'headers': {'content-type': 'application/json'}, 'pathParameters': {'capture': 'other'}, 'queryStringParameters': {}, 'body': '{}', 'stageVariables': {}, }