Example #1
0
    def test_apigateway_with_lambda_integration(self):
        apigw_client = aws_stack.connect_to_service('apigateway')

        # create Lambda function
        lambda_name = 'apigw-lambda-%s' % short_uid()
        self.create_lambda_function(lambda_name)
        lambda_uri = aws_stack.lambda_function_arn(lambda_name)
        target_uri = aws_stack.apigateway_invocations_arn(lambda_uri)

        # create REST API
        api = apigw_client.create_rest_api(name='test-api', description='')
        api_id = api['id']
        root_res_id = apigw_client.get_resources(
            restApiId=api_id)['items'][0]['id']
        api_resource = apigw_client.create_resource(restApiId=api_id,
                                                    parentId=root_res_id,
                                                    pathPart='test')

        apigw_client.put_method(restApiId=api_id,
                                resourceId=api_resource['id'],
                                httpMethod='GET',
                                authorizationType='NONE')

        rs = apigw_client.put_integration(
            restApiId=api_id,
            resourceId=api_resource['id'],
            httpMethod='GET',
            integrationHttpMethod='POST',
            type='AWS',
            uri=target_uri,
            requestTemplates={
                'application/json': '{"param1": "$input.params(\'param1\')"}'
            })
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)
        for key in [
                'httpMethod', 'type', 'passthroughBehavior',
                'cacheKeyParameters', 'uri', 'cacheNamespace'
        ]:
            self.assertIn(key, rs)
        self.assertNotIn('responseTemplates', rs)

        apigw_client.create_deployment(restApiId=api_id,
                                       stageName=self.TEST_STAGE_NAME)

        rs = apigw_client.get_integration(restApiId=api_id,
                                          resourceId=api_resource['id'],
                                          httpMethod='GET')
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)
        self.assertEqual(rs['type'], 'AWS')
        self.assertEqual(rs['httpMethod'], 'POST')
        self.assertEqual(rs['uri'], target_uri)

        # invoke the gateway endpoint
        url = self.gateway_request_url(api_id=api_id,
                                       stage_name=self.TEST_STAGE_NAME,
                                       path='/test')
        response = requests.get('%s?param1=foobar' % url)
        self.assertLess(response.status_code, 400)
        content = json.loads(to_str(response.content))
        self.assertEqual(content.get('httpMethod'), 'POST')
        self.assertEqual(
            content.get('requestContext', {}).get('resourceId'),
            api_resource['id'])
        self.assertEqual(
            content.get('requestContext', {}).get('stage'),
            self.TEST_STAGE_NAME)
        self.assertEqual(content.get('body'), '{"param1": "foobar"}')

        # delete integration
        rs = apigw_client.delete_integration(
            restApiId=api_id,
            resourceId=api_resource['id'],
            httpMethod='GET',
        )
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)

        with self.assertRaises(ClientError) as ctx:
            # This call should not be successful as the integration is deleted
            apigw_client.get_integration(restApiId=api_id,
                                         resourceId=api_resource['id'],
                                         httpMethod='GET')
        self.assertEqual(ctx.exception.response['Error']['Code'],
                         'BadRequestException')

        # clean up
        lambda_client = aws_stack.connect_to_service('lambda')
        lambda_client.delete_function(FunctionName=lambda_name)
        apigw_client.delete_rest_api(restApiId=api_id)
Example #2
0
    def test_apigateway_with_lambda_integration(self):
        lambda_name = 'test-apigw-lambda-%s' % short_uid()
        self.create_lambda_function(lambda_name)

        lambda_uri = aws_stack.lambda_function_arn(lambda_name)
        target_uri = aws_stack.apigateway_invocations_arn(lambda_uri)

        apigw_client = aws_stack.connect_to_service('apigateway')

        api = apigw_client.create_rest_api(name='test-api', description='')

        api_id = api['id']
        root_res_id = apigw_client.get_resources(
            restApiId=api_id)['items'][0]['id']
        api_resource = apigw_client.create_resource(restApiId=api_id,
                                                    parentId=root_res_id,
                                                    pathPart='test')

        apigw_client.put_method(restApiId=api_id,
                                resourceId=api_resource['id'],
                                httpMethod='GET',
                                authorizationType='NONE')

        rs = apigw_client.put_integration(restApiId=api_id,
                                          resourceId=api_resource['id'],
                                          httpMethod='GET',
                                          integrationHttpMethod='GET',
                                          type='AWS',
                                          uri=target_uri)
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)
        for key in [
                'httpMethod', 'type', 'passthroughBehavior',
                'cacheKeyParameters', 'uri', 'cacheNamespace'
        ]:
            self.assertIn(key, rs)

        self.assertNotIn('responseTemplates', rs)

        rs = apigw_client.get_integration(restApiId=api_id,
                                          resourceId=api_resource['id'],
                                          httpMethod='GET')
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)

        self.assertEqual(rs['type'], 'AWS')
        self.assertEqual(rs['httpMethod'], 'GET')
        self.assertEqual(rs['uri'], target_uri)

        rs = apigw_client.delete_integration(
            restApiId=api_id,
            resourceId=api_resource['id'],
            httpMethod='GET',
        )
        self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)

        try:
            # Try to get deleted integration
            apigw_client.get_integration(restApiId=api_id,
                                         resourceId=api_resource['id'],
                                         httpMethod='GET')
            self.fail(
                'This call should not be successful as the integration is deleted'
            )

        except ClientError as e:
            self.assertEqual(e.response['Error']['Code'],
                             'BadRequestException')

        # clean up
        lambda_client = aws_stack.connect_to_service('lambda')
        lambda_client.delete_function(FunctionName=lambda_name)

        apigw_client.delete_rest_api(restApiId=api_id)