Best Python code snippet using localstack_python
34116_test_api_gateway.py
Source:34116_test_api_gateway.py  
...35# name of Kinesis stream connected to API Gateway36TEST_STREAM_KINESIS_API_GW = 'test-stream-api-gw'37TEST_STAGE_NAME = 'testing'38TEST_LAMBDA_PROXY_BACKEND = 'test_lambda_apigw_backend'39def connect_api_gateway_to_kinesis(gateway_name, kinesis_stream):40    resources = {}41    template = APIGATEWAY_DATA_INBOUND_TEMPLATE % (kinesis_stream)42    resource_path = API_PATH_DATA_INBOUND.replace('/', '')43    resources[resource_path] = [{44        'httpMethod': 'POST',45        'authorizationType': 'NONE',46        'integrations': [{47            'type': 'AWS',48            'uri': 'arn:aws:apigateway:%s:kinesis:action/PutRecords' % DEFAULT_REGION,49            'requestTemplates': {50                'application/json': template51            }52        }]53    }]54    return aws_stack.create_api_gateway(name=gateway_name, resources=resources,55        stage_name=TEST_STAGE_NAME)56def connect_api_gateway_to_http(gateway_name, target_url, methods=[], path=None):57    if not methods:58        methods = ['GET', 'POST']59    if not path:60        path = '/'61    resources = {}62    resource_path = path.replace('/', '')63    resources[resource_path] = []64    for method in methods:65        resources[resource_path].append({66            'httpMethod': method,67            'integrations': [{68                'type': 'HTTP',69                'uri': target_url70            }]71        })72    return aws_stack.create_api_gateway(name=gateway_name, resources=resources,73        stage_name=TEST_STAGE_NAME)74def connect_api_gateway_to_http_with_lambda_proxy(gateway_name, target_uri, methods=[], path=None):75    if not methods:76        methods = ['GET', 'POST']77    if not path:78        path = '/'79    resources = {}80    resource_path = path.lstrip('/')81    resources[resource_path] = []82    for method in methods:83        resources[resource_path].append({84            'httpMethod': method,85            'integrations': [{86                'type': 'AWS_PROXY',87                'uri': target_uri88            }]89        })90    return aws_stack.create_api_gateway(name=gateway_name, resources=resources,91        stage_name=TEST_STAGE_NAME)92def test_api_gateway_kinesis_integration():93    # create target Kinesis stream94    aws_stack.create_kinesis_stream(TEST_STREAM_KINESIS_API_GW)95    # create API Gateway and connect it to the target stream96    result = connect_api_gateway_to_kinesis('test_gateway1', TEST_STREAM_KINESIS_API_GW)97    # generate test data98    test_data = {'records': [99        {'data': '{"foo": "bar1"}'},100        {'data': '{"foo": "bar2"}'},101        {'data': '{"foo": "bar3"}'}102    ]}103    url = INBOUND_GATEWAY_URL_PATTERN.format(api_id=result['id'],104        stage_name=TEST_STAGE_NAME, path=API_PATH_DATA_INBOUND)105    result = requests.post(url, data=json.dumps(test_data))106    result = json.loads(to_str(result.content))107    assert result['FailedRecordCount'] == 0108    assert len(result['Records']) == len(test_data['records'])109def test_api_gateway_http_integration():110    test_port = 12123...test_api_gateway.py
Source:test_api_gateway.py  
...29API_PATH_HTTP_BACKEND = '/hello_world'30# name of Kinesis stream connected to API Gateway31TEST_STREAM_KINESIS_API_GW = 'test-stream-api-gw'32TEST_STAGE_NAME = 'testing'33def connect_api_gateway_to_kinesis(gateway_name, kinesis_stream):34    resources = {}35    template_data_inbound = APIGATEWAY_DATA_INBOUND_TEMPLATE % (kinesis_stream)36    resource_data_inbound = API_PATH_DATA_INBOUND.replace('/', '')37    resources[resource_data_inbound] = [{38        'httpMethod': 'POST',39        'authorizationType': 'NONE',40        'integrations': [{41            'type': 'AWS',42            'uri': 'arn:aws:apigateway:%s:kinesis:action/PutRecords' % DEFAULT_REGION,43            'requestTemplates': {44                'application/json': template_data_inbound45            }46        }]47    }]48    return aws_stack.create_api_gateway(name=gateway_name, resources=resources,49        stage_name=TEST_STAGE_NAME)50def connect_api_gateway_to_http(gateway_name, target_url, methods=[], path=None):51    if not methods:52        methods = ['GET', 'POST']53    if not path:54        path = '/'55    resources = {}56    resource_data_inbound = path.replace('/', '')57    resources[resource_data_inbound] = []58    for method in methods:59        resources[resource_data_inbound].append({60            'httpMethod': method,61            'integrations': [{62                'type': 'HTTP',63                'uri': target_url64            }]65        })66    return aws_stack.create_api_gateway(name=gateway_name, resources=resources,67        stage_name=TEST_STAGE_NAME)68def test_api_gateway_kinesis_integration():69    # create target Kinesis stream70    aws_stack.create_kinesis_stream(TEST_STREAM_KINESIS_API_GW)71    # create API Gateway and connect it to the target stream72    result = connect_api_gateway_to_kinesis('test_gateway1', TEST_STREAM_KINESIS_API_GW)73    # generate test data74    test_data = {'records': [75        {'data': '{"foo": "bar1"}'},76        {'data': '{"foo": "bar2"}'},77        {'data': '{"foo": "bar3"}'}78    ]}79    url = INBOUND_GATEWAY_URL_PATTERN.format(api_id=result['id'],80        stage_name=TEST_STAGE_NAME, path=API_PATH_DATA_INBOUND)81    result = requests.post(url, data=json.dumps(test_data))82    result = json.loads(to_str(result.content))83    assert result['FailedRecordCount'] == 084    assert len(result['Records']) == len(test_data['records'])85def test_api_gateway_http_integration():86    test_port = 12123...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
